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