Skip to content

Commit 4132d53

Browse files
committed
Ensure a minimum frequency for event processing even if the UI slows
Previously we relied just on requestAnimationFrame, which should stay fairly proportional to UI rendering perf I think. Unfortunately though this is paused completely in the background (not just throttled) and could theoretically reduce processing to very slow low levels if something else starves UI updates. To help with that, we now use a setTimeout backup as well.
1 parent e82e2ff commit 4132d53

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/model/events/events-store.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ export class EventsStore {
195195
private queueEventFlush() {
196196
if (!this.isFlushQueued) {
197197
this.isFlushQueued = true;
198+
199+
// We use both setTimeout _and_ requestAnimationFrame. This aims to
200+
// ensure a minimum update frequency (even if throttled or the UI is slow)
201+
// but provide a very fast update frequency the rest of the time.
202+
setTimeout(this.flushQueuedUpdates, 500);
198203
requestAnimationFrame(this.flushQueuedUpdates);
199204
}
200205
}
@@ -212,11 +217,12 @@ export class EventsStore {
212217

213218
@action.bound
214219
private flushQueuedUpdates() {
220+
// N.b. this is called twice for each flush, but that's fine - very cheap to do so.
215221
this.isFlushQueued = false;
216222

217-
// We batch request updates until here. This runs in a mobx transaction and
218-
// on request animation frame, so batches get larger and cheaper if
219-
// the frame rate starts to drop.
223+
// We batch request updates until here. This runs in a mobx transaction and on
224+
// animation frame + slow setTimeout. The goal being that as rendering blocks
225+
// for longer, batches will get larger and cheaper.
220226

221227
if (this.eventQueue.length > LARGE_QUEUE_BATCH_SIZE) {
222228
// If there's a lot of events in the queue (only ever likely to happen

0 commit comments

Comments
 (0)