Skip to content

Commit

Permalink
another perf improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
salazarm committed Dec 9, 2024
1 parent 116ab63 commit 31f8f50
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class LiveDataThread<T> {
this.startFetchLoop();
}

private _scheduledStopFetchLoop = false;
public unsubscribe(key: string) {
if (!this.listenersCount[key]) {
return;
Expand All @@ -53,22 +54,37 @@ export class LiveDataThread<T> {
if (this.listenersCount[key] === 0) {
delete this.listenersCount[key];
}
if (this.getObservedKeys().length === 0) {
this.stopFetchLoop();

if (!this._scheduledStopFetchLoop) {
this._scheduledStopFetchLoop = true;
setTimeout(() => {
if (this.getObservedKeys().length === 0) {
this.stopFetchLoop();
}
this._scheduledStopFetchLoop = false;
}, 100);
}
}

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

private _scheduledStartFetchLoop = false;
public startFetchLoop() {
if (this.activeFetches !== this.parallelFetches) {
requestAnimationFrame(this._batchedQueryKeys);
}
if (this.intervals.length !== this.parallelFetches) {
this.intervals.push(setInterval(this._batchedQueryKeys, 5000));
if (this._scheduledStartFetchLoop) {
return;
}
this._scheduledStartFetchLoop = true;
setTimeout(() => {
if (this.activeFetches !== this.parallelFetches) {
requestAnimationFrame(this._batchedQueryKeys);
}
if (this.intervals.length !== this.parallelFetches) {
this.intervals.push(setInterval(this._batchedQueryKeys, 5000));
}
this._scheduledStartFetchLoop = false;
}, 50);
}

public stopFetchLoop() {
Expand Down

0 comments on commit 31f8f50

Please sign in to comment.