@amplitude/rrweb-player 2.0.0-alpha.37 → 2.0.0-alpha.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9832,7 +9832,8 @@ function buildNodeWithSN(n2, options) {
9832
9832
  skipChild = false,
9833
9833
  hackCss = true,
9834
9834
  afterAppend,
9835
- cache
9835
+ cache,
9836
+ scrollNodes
9836
9837
  } = options;
9837
9838
  if (mirror2.has(n2.id)) {
9838
9839
  const nodeInMirror = mirror2.getNode(n2.id);
@@ -9863,6 +9864,9 @@ function buildNodeWithSN(n2, options) {
9863
9864
  node2 = doc;
9864
9865
  }
9865
9866
  mirror2.add(node2, n2);
9867
+ if (scrollNodes && n2.type === NodeType$3.Element && n2.attributes && ("rr_scrollLeft" in n2.attributes || "rr_scrollTop" in n2.attributes)) {
9868
+ scrollNodes.push(node2);
9869
+ }
9866
9870
  if ((n2.type === NodeType$3.Document || n2.type === NodeType$3.Element) && !skipChild) {
9867
9871
  for (const childN of n2.childNodes) {
9868
9872
  const childNode = buildNodeWithSN(childN, {
@@ -9871,7 +9875,8 @@ function buildNodeWithSN(n2, options) {
9871
9875
  skipChild: false,
9872
9876
  hackCss,
9873
9877
  afterAppend,
9874
- cache
9878
+ cache,
9879
+ scrollNodes
9875
9880
  });
9876
9881
  if (!childNode) {
9877
9882
  console.warn("Failed to rebuild", childN);
@@ -9940,20 +9945,22 @@ function rebuild(n2, options) {
9940
9945
  cache,
9941
9946
  mirror: mirror2 = new Mirror()
9942
9947
  } = options;
9948
+ const scrollNodes = [];
9943
9949
  const node2 = buildNodeWithSN(n2, {
9944
9950
  doc,
9945
9951
  mirror: mirror2,
9946
9952
  skipChild: false,
9947
9953
  hackCss,
9948
9954
  afterAppend,
9949
- cache
9950
- });
9951
- visit(mirror2, (visitedNode) => {
9952
- if (onVisit) {
9953
- onVisit(visitedNode);
9954
- }
9955
- handleScroll(visitedNode, mirror2);
9955
+ cache,
9956
+ scrollNodes
9956
9957
  });
9958
+ if (onVisit) {
9959
+ visit(mirror2, onVisit);
9960
+ }
9961
+ for (const scrollNode of scrollNodes) {
9962
+ handleScroll(scrollNode, mirror2);
9963
+ }
9957
9964
  return node2;
9958
9965
  }
9959
9966
  var __defProp22 = Object.defineProperty;
@@ -15824,6 +15831,7 @@ class Timer {
15824
15831
  __publicField2(this, "timeOffset", 0);
15825
15832
  __publicField2(this, "speed");
15826
15833
  __publicField2(this, "actions");
15834
+ __publicField2(this, "actionIndex", 0);
15827
15835
  __publicField2(this, "raf", null);
15828
15836
  __publicField2(this, "lastTimestamp");
15829
15837
  this.actions = actions;
@@ -15834,7 +15842,7 @@ class Timer {
15834
15842
  */
15835
15843
  addAction(action) {
15836
15844
  const rafWasActive = this.raf === true;
15837
- if (!this.actions.length || this.actions[this.actions.length - 1].delay <= action.delay) {
15845
+ if (this.actionIndex >= this.actions.length || this.actions[this.actions.length - 1].delay <= action.delay) {
15838
15846
  this.actions.push(action);
15839
15847
  } else {
15840
15848
  const index2 = this.findActionIndex(action);
@@ -15853,16 +15861,16 @@ class Timer {
15853
15861
  const time = performance.now();
15854
15862
  this.timeOffset += (time - this.lastTimestamp) * this.speed;
15855
15863
  this.lastTimestamp = time;
15856
- while (this.actions.length) {
15857
- const action = this.actions[0];
15864
+ while (this.actionIndex < this.actions.length) {
15865
+ const action = this.actions[this.actionIndex];
15858
15866
  if (this.timeOffset >= action.delay) {
15859
- this.actions.shift();
15867
+ this.actionIndex++;
15860
15868
  action.doAction();
15861
15869
  } else {
15862
15870
  break;
15863
15871
  }
15864
15872
  }
15865
- if (this.actions.length > 0) {
15873
+ if (this.actionIndex < this.actions.length) {
15866
15874
  this.raf = requestAnimationFrame(this.rafCheck.bind(this));
15867
15875
  } else {
15868
15876
  this.raf = true;
@@ -15876,6 +15884,7 @@ class Timer {
15876
15884
  this.raf = null;
15877
15885
  }
15878
15886
  this.actions.length = 0;
15887
+ this.actionIndex = 0;
15879
15888
  }
15880
15889
  setSpeed(speed) {
15881
15890
  this.speed = speed;
@@ -15884,7 +15893,7 @@ class Timer {
15884
15893
  return this.raf !== null;
15885
15894
  }
15886
15895
  findActionIndex(action) {
15887
- let start = 0;
15896
+ let start = this.actionIndex;
15888
15897
  let end = this.actions.length - 1;
15889
15898
  while (start <= end) {
15890
15899
  const mid = Math.floor((start + end) / 2);
@@ -16053,34 +16062,44 @@ function v(t2) {
16053
16062
  } };
16054
16063
  return c2;
16055
16064
  }
16056
- function isDuplicateEvent(existing, incoming) {
16057
- if (existing === incoming) {
16058
- return true;
16059
- }
16060
- if (existing.timestamp === incoming.timestamp && existing.type === incoming.type) {
16061
- return JSON.stringify(existing) === JSON.stringify(incoming);
16065
+ function buildCheckpointIndex(events) {
16066
+ const checkpoints = [];
16067
+ for (let i2 = 0; i2 < events.length; i2++) {
16068
+ if (events[i2].type === EventType$1.Meta) {
16069
+ checkpoints.push({
16070
+ metaEventIndex: i2,
16071
+ timestamp: events[i2].timestamp
16072
+ });
16073
+ }
16062
16074
  }
16063
- return false;
16075
+ return checkpoints;
16064
16076
  }
16065
- function discardPriorSnapshots(events, baselineTime) {
16077
+ function findNearestCheckpoint(checkpoints, baselineTime) {
16078
+ if (checkpoints.length === 0) {
16079
+ return null;
16080
+ }
16066
16081
  let lo = 0;
16067
- let hi = events.length - 1;
16068
- let lastBeforeOrAt = -1;
16082
+ let hi = checkpoints.length - 1;
16083
+ let result2 = null;
16069
16084
  while (lo <= hi) {
16070
- const mid = lo + hi >>> 1;
16071
- if (events[mid].timestamp <= baselineTime) {
16072
- lastBeforeOrAt = mid;
16085
+ const mid = Math.floor((lo + hi) / 2);
16086
+ if (checkpoints[mid].timestamp <= baselineTime) {
16087
+ result2 = checkpoints[mid];
16073
16088
  lo = mid + 1;
16074
16089
  } else {
16075
16090
  hi = mid - 1;
16076
16091
  }
16077
16092
  }
16078
- for (let idx = lastBeforeOrAt; idx >= 0; idx--) {
16079
- if (events[idx].type === EventType$1.Meta) {
16080
- return events.slice(idx);
16081
- }
16093
+ return result2;
16094
+ }
16095
+ function isDuplicateEvent(existing, incoming) {
16096
+ if (existing === incoming) {
16097
+ return true;
16082
16098
  }
16083
- return events;
16099
+ if (existing.timestamp === incoming.timestamp && existing.type === incoming.type) {
16100
+ return JSON.stringify(existing) === JSON.stringify(incoming);
16101
+ }
16102
+ return false;
16084
16103
  }
16085
16104
  function createPlayerService(context, { getCastFn, applyEventsSynchronously, emitter }) {
16086
16105
  const playerMachine = s(
@@ -16166,9 +16185,19 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16166
16185
  }),
16167
16186
  play(ctx) {
16168
16187
  var _a22;
16169
- const { timer, events, baselineTime, lastPlayedEvent } = ctx;
16188
+ const {
16189
+ timer,
16190
+ events,
16191
+ baselineTime,
16192
+ lastPlayedEvent,
16193
+ checkpointIndex
16194
+ } = ctx;
16170
16195
  timer.clear();
16171
- const neededEvents = discardPriorSnapshots(events, baselineTime);
16196
+ const checkpoint = findNearestCheckpoint(
16197
+ checkpointIndex,
16198
+ baselineTime
16199
+ );
16200
+ const startIndex = checkpoint ? checkpoint.metaEventIndex : 0;
16172
16201
  let lastPlayedTimestamp = lastPlayedEvent == null ? void 0 : lastPlayedEvent.timestamp;
16173
16202
  if ((lastPlayedEvent == null ? void 0 : lastPlayedEvent.type) === EventType$1.IncrementalSnapshot && lastPlayedEvent.data.source === IncrementalSource.MouseMove) {
16174
16203
  lastPlayedTimestamp = lastPlayedEvent.timestamp + ((_a22 = lastPlayedEvent.data.positions[0]) == null ? void 0 : _a22.timeOffset);
@@ -16176,46 +16205,25 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16176
16205
  if (baselineTime < (lastPlayedTimestamp || 0)) {
16177
16206
  emitter.emit(ReplayerEvents.PlayBack);
16178
16207
  }
16179
- let syncEnd = neededEvents.length;
16180
- {
16181
- let lo = 0;
16182
- let hi = neededEvents.length;
16183
- while (lo < hi) {
16184
- const mid = lo + hi >>> 1;
16185
- if (neededEvents[mid].timestamp < baselineTime) lo = mid + 1;
16186
- else hi = mid;
16187
- }
16188
- syncEnd = lo;
16189
- }
16190
- let syncStart = 0;
16191
- if (lastPlayedTimestamp && lastPlayedTimestamp < baselineTime) {
16192
- let lo = 0;
16193
- let hi = syncEnd;
16194
- while (lo < hi) {
16195
- const mid = lo + hi >>> 1;
16196
- if (neededEvents[mid].timestamp <= lastPlayedTimestamp)
16197
- lo = mid + 1;
16198
- else hi = mid;
16208
+ const syncEvents = new Array();
16209
+ for (let i2 = startIndex; i2 < events.length; i2++) {
16210
+ const event2 = events[i2];
16211
+ if (lastPlayedTimestamp && lastPlayedTimestamp < baselineTime && (event2.timestamp <= lastPlayedTimestamp || event2 === lastPlayedEvent)) {
16212
+ continue;
16199
16213
  }
16200
- syncStart = lo;
16201
- if (syncStart < syncEnd && neededEvents[syncStart] === lastPlayedEvent) {
16202
- syncStart++;
16214
+ if (event2.timestamp < baselineTime) {
16215
+ syncEvents.push(event2);
16216
+ } else {
16217
+ addDelay(event2, baselineTime);
16218
+ const castFn = getCastFn(event2, false);
16219
+ timer.addAction({
16220
+ doAction: () => {
16221
+ castFn();
16222
+ },
16223
+ delay: event2.delay ?? 0
16224
+ });
16203
16225
  }
16204
16226
  }
16205
- for (let i2 = syncEnd; i2 < neededEvents.length; i2++) {
16206
- addDelay(neededEvents[i2], baselineTime);
16207
- }
16208
- const syncEvents = neededEvents.slice(syncStart, syncEnd);
16209
- for (let i2 = syncEnd; i2 < neededEvents.length; i2++) {
16210
- const event2 = neededEvents[i2];
16211
- const castFn = getCastFn(event2, false);
16212
- timer.addAction({
16213
- doAction: () => {
16214
- castFn();
16215
- },
16216
- delay: event2.delay ?? 0
16217
- });
16218
- }
16219
16227
  applyEventsSynchronously(syncEvents);
16220
16228
  emitter.emit(ReplayerEvents.Flush);
16221
16229
  timer.start();
@@ -16239,19 +16247,21 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16239
16247
  }
16240
16248
  }),
16241
16249
  addEvent: o((ctx, machineEvent) => {
16242
- const { baselineTime, timer, events } = ctx;
16250
+ const { baselineTime, timer, events, checkpointIndex } = ctx;
16243
16251
  if (machineEvent.type === "ADD_EVENT") {
16244
16252
  const { event: event2 } = machineEvent.payload;
16245
16253
  addDelay(event2, baselineTime);
16246
16254
  let end = events.length - 1;
16255
+ let insertionIndex;
16247
16256
  if (!events[end] || events[end].timestamp <= event2.timestamp) {
16248
16257
  if (events[end] && isDuplicateEvent(events[end], event2)) {
16249
16258
  return { ...ctx, events };
16250
16259
  }
16251
16260
  events.push(event2);
16261
+ insertionIndex = events.length - 1;
16252
16262
  } else {
16253
- let insertionIndex = -1;
16254
16263
  let start = 0;
16264
+ insertionIndex = 0;
16255
16265
  while (start <= end) {
16256
16266
  const mid = Math.floor((start + end) / 2);
16257
16267
  if (events[mid].timestamp <= event2.timestamp) {
@@ -16260,9 +16270,7 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16260
16270
  end = mid - 1;
16261
16271
  }
16262
16272
  }
16263
- if (insertionIndex === -1) {
16264
- insertionIndex = start;
16265
- }
16273
+ insertionIndex = start;
16266
16274
  for (let i2 = insertionIndex - 1; i2 >= 0 && events[i2].timestamp === event2.timestamp; i2--) {
16267
16275
  if (isDuplicateEvent(events[i2], event2)) {
16268
16276
  return { ...ctx, events };
@@ -16275,6 +16283,39 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16275
16283
  }
16276
16284
  events.splice(insertionIndex, 0, event2);
16277
16285
  }
16286
+ if (event2.type === EventType$1.Meta) {
16287
+ const newCheckpoint = {
16288
+ metaEventIndex: insertionIndex,
16289
+ timestamp: event2.timestamp
16290
+ };
16291
+ const lastCheckpoint = checkpointIndex[checkpointIndex.length - 1];
16292
+ if (!lastCheckpoint || lastCheckpoint.timestamp <= event2.timestamp) {
16293
+ checkpointIndex.push(newCheckpoint);
16294
+ } else {
16295
+ for (const checkpoint of checkpointIndex) {
16296
+ if (checkpoint.metaEventIndex >= insertionIndex) {
16297
+ checkpoint.metaEventIndex++;
16298
+ }
16299
+ }
16300
+ let searchLo = 0;
16301
+ let searchHi = checkpointIndex.length - 1;
16302
+ while (searchLo <= searchHi) {
16303
+ const mid = Math.floor((searchLo + searchHi) / 2);
16304
+ if (checkpointIndex[mid].timestamp <= event2.timestamp) {
16305
+ searchLo = mid + 1;
16306
+ } else {
16307
+ searchHi = mid - 1;
16308
+ }
16309
+ }
16310
+ checkpointIndex.splice(searchLo, 0, newCheckpoint);
16311
+ }
16312
+ } else if (insertionIndex < events.length - 1) {
16313
+ for (const checkpoint of checkpointIndex) {
16314
+ if (checkpoint.metaEventIndex >= insertionIndex) {
16315
+ checkpoint.metaEventIndex++;
16316
+ }
16317
+ }
16318
+ }
16278
16319
  const isSync = event2.timestamp < baselineTime;
16279
16320
  const castFn = getCastFn(event2, isSync);
16280
16321
  if (isSync) {
@@ -16288,7 +16329,7 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16288
16329
  });
16289
16330
  }
16290
16331
  }
16291
- return { ...ctx, events };
16332
+ return { ...ctx, events, checkpointIndex };
16292
16333
  })
16293
16334
  }
16294
16335
  }
@@ -16828,6 +16869,13 @@ class Replayer {
16828
16869
  });
16829
16870
  __publicField2(this, "applyEventsSynchronously", (events2) => {
16830
16871
  var _a22;
16872
+ let lastFullSnapshotIndex = -1;
16873
+ for (let i2 = events2.length - 1; i2 >= 0; i2--) {
16874
+ if (events2[i2].type === EventType$1.FullSnapshot) {
16875
+ lastFullSnapshotIndex = i2;
16876
+ break;
16877
+ }
16878
+ }
16831
16879
  let startIdx = 0;
16832
16880
  if (this.config.useSeekCache && events2.length > 0) {
16833
16881
  const fullSnapshotEvt = events2.find(
@@ -16873,10 +16921,14 @@ class Replayer {
16873
16921
  case EventType$1.Load:
16874
16922
  case EventType$1.Custom:
16875
16923
  continue;
16924
+ case EventType$1.IncrementalSnapshot:
16925
+ if (i2 < lastFullSnapshotIndex) {
16926
+ continue;
16927
+ }
16928
+ break;
16876
16929
  case EventType$1.FullSnapshot:
16877
16930
  case EventType$1.Meta:
16878
16931
  case EventType$1.Plugin:
16879
- case EventType$1.IncrementalSnapshot:
16880
16932
  break;
16881
16933
  }
16882
16934
  const castFn = this.getCastFn(event2, true);
@@ -17139,18 +17191,21 @@ class Replayer {
17139
17191
  const timer = new Timer([], {
17140
17192
  speed: this.config.speed
17141
17193
  });
17194
+ const sortedEvents = events.map((e2) => {
17195
+ if (config && config.unpackFn) {
17196
+ return config.unpackFn(e2);
17197
+ }
17198
+ return e2;
17199
+ }).sort((a1, a2) => a1.timestamp - a2.timestamp);
17200
+ const checkpointIndex = buildCheckpointIndex(sortedEvents);
17142
17201
  this.service = createPlayerService(
17143
17202
  {
17144
- events: events.map((e2) => {
17145
- if (config && config.unpackFn) {
17146
- return config.unpackFn(e2);
17147
- }
17148
- return e2;
17149
- }).sort((a1, a2) => a1.timestamp - a2.timestamp),
17203
+ events: sortedEvents,
17150
17204
  timer,
17151
17205
  timeOffset: 0,
17152
17206
  baselineTime: 0,
17153
- lastPlayedEvent: null
17207
+ lastPlayedEvent: null,
17208
+ checkpointIndex
17154
17209
  },
17155
17210
  {
17156
17211
  getCastFn: this.getCastFn,