diff --git a/src/replay/index.ts b/src/replay/index.ts index fc5dddd1..61c7559c 100644 --- a/src/replay/index.ts +++ b/src/replay/index.ts @@ -193,6 +193,10 @@ export class Replayer { this.applyInput(d); } }); + this.emitter.on(ReplayerEvents.PlayBack, () => { + this.firstPlayedEvent = null; + mirror.reset(); + }); const timer = new Timer([], config?.speed || defaultConfig.speed); this.service = createPlayerService( diff --git a/src/replay/machine.ts b/src/replay/machine.ts index 61dbb58b..2dbab190 100644 --- a/src/replay/machine.ts +++ b/src/replay/machine.ts @@ -173,17 +173,21 @@ export function createPlayerService( } const neededEvents = discardPriorSnapshots(events, baselineTime); + let lastPlayedTimestamp = lastPlayedEvent?.timestamp; + if ( + lastPlayedEvent?.type === EventType.IncrementalSnapshot && + lastPlayedEvent.data.source === IncrementalSource.MouseMove + ) { + lastPlayedTimestamp = + lastPlayedEvent.timestamp + + lastPlayedEvent.data.positions[0]?.timeOffset; + } + if (baselineTime < (lastPlayedTimestamp || 0)) { + emitter.emit(ReplayerEvents.PlayBack); + } + const actions = new Array(); for (const event of neededEvents) { - let lastPlayedTimestamp = lastPlayedEvent?.timestamp; - if ( - lastPlayedEvent?.type === EventType.IncrementalSnapshot && - lastPlayedEvent.data.source === IncrementalSource.MouseMove - ) { - lastPlayedTimestamp = - lastPlayedEvent.timestamp + - lastPlayedEvent.data.positions[0]?.timeOffset; - } if ( lastPlayedTimestamp && lastPlayedTimestamp < baselineTime && diff --git a/src/types.ts b/src/types.ts index a7b3d7c9..ed283b39 100644 --- a/src/types.ts +++ b/src/types.ts @@ -489,6 +489,7 @@ export type Mirror = { getNode: (id: number) => INode | null; removeNodeFromMap: (n: INode) => void; has: (id: number) => boolean; + reset: () => void; }; export type throttleOptions = { @@ -577,6 +578,7 @@ export enum ReplayerEvents { CustomEvent = 'custom-event', Flush = 'flush', StateChange = 'state-change', + PlayBack = 'play-back', } export type MaskInputFn = (text: string) => string; diff --git a/src/utils.ts b/src/utils.ts index 29ee09d7..ba7bdcde 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -59,6 +59,9 @@ export const mirror: Mirror = { has(id) { return mirror.map.hasOwnProperty(id); }, + reset() { + mirror.map = {}; + }, }; // copy from underscore and modified