This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/services/screen-track-test.js
Krzysztof Kotlarek 9610aea189
FEATURE: cache last post number (#15772)
Instead of relaying on /timings request, we should cache last read post number. That should protect from having incorrect unread counter when going back to topic list.

This additional cache is very temporary as once /timings request is finished, serializer will have a correct result.

Simplified flow is:
1. Store in cache information about last seen post number before /timings request is sent
2. When getting back to topic list compare value of last seen post number returned by /latest request and information in cache. If cache number is higher, than use it instead of information returned by /latest. In addition delete cache item as there is high chance that `/timings` request already finished.
3. Optionally, delete cache when timings request is done and topic list was not yet visited.

Keeping cache reasonably small should not affect performance.
2022-02-10 13:09:28 +11:00

29 lines
923 B
JavaScript

import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
discourseModule("Unit | Service | screen-track", function () {
test("consolidateTimings", function (assert) {
const tracker = this.container.lookup("service:screen-track");
tracker.consolidateTimings({ 1: 10, 2: 5 }, 10, 1);
tracker.consolidateTimings({ 1: 5, 3: 1 }, 3, 1);
const consolidated = tracker.consolidateTimings({ 1: 5, 3: 1, 4: 5 }, 3, 2);
assert.deepEqual(
consolidated,
[
{ timings: { 1: 15, 2: 5, 3: 1 }, topicTime: 13, topicId: 1 },
{ timings: { 1: 5, 3: 1, 4: 5 }, topicTime: 3, topicId: 2 },
],
"expecting consolidated timings to match correctly"
);
tracker.sendNextConsolidatedTiming();
assert.equal(
tracker.highestReadFromCache(2),
4,
"caches highest read post number for second topic"
);
});
});