From 4801f21a8c5b2592f8ddd44944f4d6521ea35f35 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Thu, 15 Sep 2022 16:50:38 +0100 Subject: [PATCH 1/3] Fix that `addAction` wouldn't have any effect without a stop and start - noticed during live mode --- packages/rrweb/src/replay/timer.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/rrweb/src/replay/timer.ts b/packages/rrweb/src/replay/timer.ts index 6cfa95e0d6..60689a1422 100644 --- a/packages/rrweb/src/replay/timer.ts +++ b/packages/rrweb/src/replay/timer.ts @@ -41,22 +41,21 @@ export class Timer { public start() { this.timeOffset = 0; let lastTimestamp = performance.now(); - const { actions } = this; const check = () => { const time = performance.now(); this.timeOffset += (time - lastTimestamp) * this.speed; lastTimestamp = time; - while (actions.length) { - const action = actions[0]; + while (this.actions.length) { + const action = this.actions[0]; if (this.timeOffset >= action.delay) { - actions.shift(); + this.actions.shift(); action.doAction(); } else { break; } } - if (actions.length > 0 || this.liveMode) { + if (this.actions.length > 0 || this.liveMode) { this.raf = requestAnimationFrame(check); } }; From 9c570b68a784fd57a59af2ca8fec01acca4fe6f6 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Fri, 16 Sep 2022 10:44:58 +0100 Subject: [PATCH 2/3] Remove `addActions` as it has a bug-causing replacement of `this.actions` - refactor to reuse `addAction` and add a `push` fast-track to this function for the common case of adding actions in the correct order --- packages/rrweb/src/replay/machine.ts | 4 +--- packages/rrweb/src/replay/timer.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/rrweb/src/replay/machine.ts b/packages/rrweb/src/replay/machine.ts index 4409dd1151..fd4ad7bf68 100644 --- a/packages/rrweb/src/replay/machine.ts +++ b/packages/rrweb/src/replay/machine.ts @@ -188,7 +188,6 @@ export function createPlayerService( } const syncEvents = new Array(); - const actions = new Array(); for (const event of neededEvents) { if ( lastPlayedTimestamp && @@ -202,7 +201,7 @@ export function createPlayerService( syncEvents.push(event); } else { const castFn = getCastFn(event, false); - actions.push({ + timer.addAction({ doAction: () => { castFn(); }, @@ -212,7 +211,6 @@ export function createPlayerService( } applyEventsSynchronously(syncEvents); emitter.emit(ReplayerEvents.Flush); - timer.addActions(actions); timer.start(); }, pause(ctx) { diff --git a/packages/rrweb/src/replay/timer.ts b/packages/rrweb/src/replay/timer.ts index 60689a1422..76c08491da 100644 --- a/packages/rrweb/src/replay/timer.ts +++ b/packages/rrweb/src/replay/timer.ts @@ -25,18 +25,18 @@ export class Timer { this.liveMode = config.liveMode; } /** - * Add an action after the timer starts. + * Add an action, possibly after the timer starts. */ public addAction(action: actionWithDelay) { + if (!this.actions.length || this.actions[this.actions.length - 1].delay <= action.delay) { + // 'fast track' + this.actions.push(action); + return; + } + // binary search - events can arrive out of order in a realtime context const index = this.findActionIndex(action); this.actions.splice(index, 0, action); } - /** - * Add all actions before the timer starts - */ - public addActions(actions: actionWithDelay[]) { - this.actions = this.actions.concat(actions); - } public start() { this.timeOffset = 0; From 6b41521030a998b5222a9618f1f38b8839a8dcf9 Mon Sep 17 00:00:00 2001 From: eoghanmurray Date: Thu, 22 Sep 2022 09:02:01 +0000 Subject: [PATCH 3/3] Apply formatting changes --- packages/rrweb/src/replay/timer.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/rrweb/src/replay/timer.ts b/packages/rrweb/src/replay/timer.ts index 76c08491da..89d66d4bc3 100644 --- a/packages/rrweb/src/replay/timer.ts +++ b/packages/rrweb/src/replay/timer.ts @@ -28,7 +28,10 @@ export class Timer { * Add an action, possibly after the timer starts. */ public addAction(action: actionWithDelay) { - if (!this.actions.length || this.actions[this.actions.length - 1].delay <= action.delay) { + if ( + !this.actions.length || + this.actions[this.actions.length - 1].delay <= action.delay + ) { // 'fast track' this.actions.push(action); return;