Skip to content

Commit

Permalink
update more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salazarm committed Dec 9, 2024
1 parent af7e6a2 commit 28b99af
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ jest.mock('../../live-data-provider/util', () => {
};
});

jest.mock('../../live-data-provider/LiveDataScheduler', () => {
return {
LiveDataScheduler: class LiveDataScheduler {
scheduleStartFetchLoop(doStart: () => void) {
doStart();
}
scheduleStopFetchLoop(doStop: () => void) {
doStop();
}
},
};
});

function Test({
mocks,
hooks,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type {LiveDataThread} from './LiveDataThread';

/**
* This exists as a separate class to allow Jest to mock it
*/
export class LiveDataScheduler<T> {
private thread: LiveDataThread<T>;

constructor(thread: LiveDataThread<T>) {
this.thread = thread;
}
private _started = new WeakSet();
private _stopped = new WeakSet();

public scheduleStartFetchLoop(doStart: () => void) {
if (this._started.has(this.thread)) {
return;
}
setTimeout(() => {
doStart();
this._stopped.delete(this.thread);
}, 50);
}

public scheduleStopFetchLoop(doStop: () => void) {
if (this._stopped.has(this.thread)) {
return;
}
setTimeout(() => {
doStop();
this._stopped.delete(this.thread);
}, 50);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {LiveDataScheduler} from './LiveDataScheduler';
import {LiveDataThreadManager} from './LiveDataThreadManager';
import {BATCH_PARALLEL_FETCHES, BATCH_SIZE, threadIDToLimits} from './util';

Expand All @@ -19,6 +20,8 @@ export class LiveDataThread<T> {
return {};
}

private _scheduler: LiveDataScheduler<T>;

constructor(
id: string,
manager: LiveDataThreadManager<T>,
Expand All @@ -33,6 +36,7 @@ export class LiveDataThread<T> {
this.listenersCount = {};
this.manager = manager;
this.intervals = [];
this._scheduler = new LiveDataScheduler(this);
}

public setPollRate(pollRate: number) {
Expand All @@ -53,29 +57,33 @@ export class LiveDataThread<T> {
if (this.listenersCount[key] === 0) {
delete this.listenersCount[key];
}
if (this.getObservedKeys().length === 0) {
this.stopFetchLoop();
}
this.stopFetchLoop(false);
}

public getObservedKeys() {
return Object.keys(this.listenersCount);
}

public startFetchLoop() {
if (this.activeFetches !== this.parallelFetches) {
requestAnimationFrame(this._batchedQueryKeys);
}
if (this.intervals.length !== this.parallelFetches) {
this.intervals.push(setInterval(this._batchedQueryKeys, 5000));
}
this._scheduler.scheduleStartFetchLoop(() => {
if (this.activeFetches !== this.parallelFetches) {
requestAnimationFrame(this._batchedQueryKeys);
}
if (this.intervals.length !== this.parallelFetches) {
this.intervals.push(setInterval(this._batchedQueryKeys, 5000));
}
});
}

public stopFetchLoop() {
this.intervals.forEach((id) => {
clearInterval(id);
public stopFetchLoop(force: boolean) {
this._scheduler.scheduleStopFetchLoop(() => {
if (force || this.getObservedKeys().length === 0) {
this.intervals.forEach((id) => {
clearInterval(id);
});
this.intervals = [];
}
});
this.intervals = [];
}

private _batchedQueryKeys = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class LiveDataThreadManager<T> {
private pause() {
this.isPaused = true;
Object.values(this.threads).forEach((thread) => {
thread.stopFetchLoop();
thread.stopFetchLoop(true);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export function generateObjectHashStream(
obj: any,
replacer?: (key: string, value: any) => any,
): string {
console.time('generateObjectHash');
console.log('generateObjectHash', obj);
const hash = new SparkMD5.ArrayBuffer();
const encoder = new TextEncoder();

Expand Down

0 comments on commit 28b99af

Please sign in to comment.