@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.
@@ -9826,7 +9826,8 @@ function buildNodeWithSN(n2, options) {
9826
9826
  skipChild = false,
9827
9827
  hackCss = true,
9828
9828
  afterAppend,
9829
- cache
9829
+ cache,
9830
+ scrollNodes
9830
9831
  } = options;
9831
9832
  if (mirror2.has(n2.id)) {
9832
9833
  const nodeInMirror = mirror2.getNode(n2.id);
@@ -9857,6 +9858,9 @@ function buildNodeWithSN(n2, options) {
9857
9858
  node2 = doc;
9858
9859
  }
9859
9860
  mirror2.add(node2, n2);
9861
+ if (scrollNodes && n2.type === NodeType$3.Element && n2.attributes && ("rr_scrollLeft" in n2.attributes || "rr_scrollTop" in n2.attributes)) {
9862
+ scrollNodes.push(node2);
9863
+ }
9860
9864
  if ((n2.type === NodeType$3.Document || n2.type === NodeType$3.Element) && !skipChild) {
9861
9865
  for (const childN of n2.childNodes) {
9862
9866
  const childNode = buildNodeWithSN(childN, {
@@ -9865,7 +9869,8 @@ function buildNodeWithSN(n2, options) {
9865
9869
  skipChild: false,
9866
9870
  hackCss,
9867
9871
  afterAppend,
9868
- cache
9872
+ cache,
9873
+ scrollNodes
9869
9874
  });
9870
9875
  if (!childNode) {
9871
9876
  console.warn("Failed to rebuild", childN);
@@ -9934,20 +9939,22 @@ function rebuild(n2, options) {
9934
9939
  cache,
9935
9940
  mirror: mirror2 = new Mirror()
9936
9941
  } = options;
9942
+ const scrollNodes = [];
9937
9943
  const node2 = buildNodeWithSN(n2, {
9938
9944
  doc,
9939
9945
  mirror: mirror2,
9940
9946
  skipChild: false,
9941
9947
  hackCss,
9942
9948
  afterAppend,
9943
- cache
9944
- });
9945
- visit(mirror2, (visitedNode) => {
9946
- if (onVisit) {
9947
- onVisit(visitedNode);
9948
- }
9949
- handleScroll(visitedNode, mirror2);
9949
+ cache,
9950
+ scrollNodes
9950
9951
  });
9952
+ if (onVisit) {
9953
+ visit(mirror2, onVisit);
9954
+ }
9955
+ for (const scrollNode of scrollNodes) {
9956
+ handleScroll(scrollNode, mirror2);
9957
+ }
9951
9958
  return node2;
9952
9959
  }
9953
9960
  var __defProp222 = Object.defineProperty;
@@ -15818,6 +15825,7 @@ class Timer {
15818
15825
  __publicField2(this, "timeOffset", 0);
15819
15826
  __publicField2(this, "speed");
15820
15827
  __publicField2(this, "actions");
15828
+ __publicField2(this, "actionIndex", 0);
15821
15829
  __publicField2(this, "raf", null);
15822
15830
  __publicField2(this, "lastTimestamp");
15823
15831
  this.actions = actions;
@@ -15828,7 +15836,7 @@ class Timer {
15828
15836
  */
15829
15837
  addAction(action) {
15830
15838
  const rafWasActive = this.raf === true;
15831
- if (!this.actions.length || this.actions[this.actions.length - 1].delay <= action.delay) {
15839
+ if (this.actionIndex >= this.actions.length || this.actions[this.actions.length - 1].delay <= action.delay) {
15832
15840
  this.actions.push(action);
15833
15841
  } else {
15834
15842
  const index2 = this.findActionIndex(action);
@@ -15847,16 +15855,16 @@ class Timer {
15847
15855
  const time = performance.now();
15848
15856
  this.timeOffset += (time - this.lastTimestamp) * this.speed;
15849
15857
  this.lastTimestamp = time;
15850
- while (this.actions.length) {
15851
- const action = this.actions[0];
15858
+ while (this.actionIndex < this.actions.length) {
15859
+ const action = this.actions[this.actionIndex];
15852
15860
  if (this.timeOffset >= action.delay) {
15853
- this.actions.shift();
15861
+ this.actionIndex++;
15854
15862
  action.doAction();
15855
15863
  } else {
15856
15864
  break;
15857
15865
  }
15858
15866
  }
15859
- if (this.actions.length > 0) {
15867
+ if (this.actionIndex < this.actions.length) {
15860
15868
  this.raf = requestAnimationFrame(this.rafCheck.bind(this));
15861
15869
  } else {
15862
15870
  this.raf = true;
@@ -15870,6 +15878,7 @@ class Timer {
15870
15878
  this.raf = null;
15871
15879
  }
15872
15880
  this.actions.length = 0;
15881
+ this.actionIndex = 0;
15873
15882
  }
15874
15883
  setSpeed(speed) {
15875
15884
  this.speed = speed;
@@ -15878,7 +15887,7 @@ class Timer {
15878
15887
  return this.raf !== null;
15879
15888
  }
15880
15889
  findActionIndex(action) {
15881
- let start = 0;
15890
+ let start = this.actionIndex;
15882
15891
  let end = this.actions.length - 1;
15883
15892
  while (start <= end) {
15884
15893
  const mid = Math.floor((start + end) / 2);
@@ -16047,34 +16056,44 @@ function v(t2) {
16047
16056
  } };
16048
16057
  return c2;
16049
16058
  }
16050
- function isDuplicateEvent(existing, incoming) {
16051
- if (existing === incoming) {
16052
- return true;
16053
- }
16054
- if (existing.timestamp === incoming.timestamp && existing.type === incoming.type) {
16055
- return JSON.stringify(existing) === JSON.stringify(incoming);
16059
+ function buildCheckpointIndex(events) {
16060
+ const checkpoints = [];
16061
+ for (let i2 = 0; i2 < events.length; i2++) {
16062
+ if (events[i2].type === EventType$1.Meta) {
16063
+ checkpoints.push({
16064
+ metaEventIndex: i2,
16065
+ timestamp: events[i2].timestamp
16066
+ });
16067
+ }
16056
16068
  }
16057
- return false;
16069
+ return checkpoints;
16058
16070
  }
16059
- function discardPriorSnapshots(events, baselineTime) {
16071
+ function findNearestCheckpoint(checkpoints, baselineTime) {
16072
+ if (checkpoints.length === 0) {
16073
+ return null;
16074
+ }
16060
16075
  let lo = 0;
16061
- let hi = events.length - 1;
16062
- let lastBeforeOrAt = -1;
16076
+ let hi = checkpoints.length - 1;
16077
+ let result2 = null;
16063
16078
  while (lo <= hi) {
16064
- const mid = lo + hi >>> 1;
16065
- if (events[mid].timestamp <= baselineTime) {
16066
- lastBeforeOrAt = mid;
16079
+ const mid = Math.floor((lo + hi) / 2);
16080
+ if (checkpoints[mid].timestamp <= baselineTime) {
16081
+ result2 = checkpoints[mid];
16067
16082
  lo = mid + 1;
16068
16083
  } else {
16069
16084
  hi = mid - 1;
16070
16085
  }
16071
16086
  }
16072
- for (let idx = lastBeforeOrAt; idx >= 0; idx--) {
16073
- if (events[idx].type === EventType$1.Meta) {
16074
- return events.slice(idx);
16075
- }
16087
+ return result2;
16088
+ }
16089
+ function isDuplicateEvent(existing, incoming) {
16090
+ if (existing === incoming) {
16091
+ return true;
16076
16092
  }
16077
- return events;
16093
+ if (existing.timestamp === incoming.timestamp && existing.type === incoming.type) {
16094
+ return JSON.stringify(existing) === JSON.stringify(incoming);
16095
+ }
16096
+ return false;
16078
16097
  }
16079
16098
  function createPlayerService(context, { getCastFn, applyEventsSynchronously, emitter }) {
16080
16099
  const playerMachine = s(
@@ -16160,9 +16179,19 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16160
16179
  play(ctx) {
16161
16180
  var _a4;
16162
16181
  var _a22;
16163
- const { timer, events, baselineTime, lastPlayedEvent } = ctx;
16182
+ const {
16183
+ timer,
16184
+ events,
16185
+ baselineTime,
16186
+ lastPlayedEvent,
16187
+ checkpointIndex
16188
+ } = ctx;
16164
16189
  timer.clear();
16165
- const neededEvents = discardPriorSnapshots(events, baselineTime);
16190
+ const checkpoint = findNearestCheckpoint(
16191
+ checkpointIndex,
16192
+ baselineTime
16193
+ );
16194
+ const startIndex = checkpoint ? checkpoint.metaEventIndex : 0;
16166
16195
  let lastPlayedTimestamp = lastPlayedEvent == null ? void 0 : lastPlayedEvent.timestamp;
16167
16196
  if ((lastPlayedEvent == null ? void 0 : lastPlayedEvent.type) === EventType$1.IncrementalSnapshot && lastPlayedEvent.data.source === IncrementalSource.MouseMove) {
16168
16197
  lastPlayedTimestamp = lastPlayedEvent.timestamp + ((_a22 = lastPlayedEvent.data.positions[0]) == null ? void 0 : _a22.timeOffset);
@@ -16170,46 +16199,25 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16170
16199
  if (baselineTime < (lastPlayedTimestamp || 0)) {
16171
16200
  emitter.emit(ReplayerEvents.PlayBack);
16172
16201
  }
16173
- let syncEnd = neededEvents.length;
16174
- {
16175
- let lo = 0;
16176
- let hi = neededEvents.length;
16177
- while (lo < hi) {
16178
- const mid = lo + hi >>> 1;
16179
- if (neededEvents[mid].timestamp < baselineTime) lo = mid + 1;
16180
- else hi = mid;
16181
- }
16182
- syncEnd = lo;
16183
- }
16184
- let syncStart = 0;
16185
- if (lastPlayedTimestamp && lastPlayedTimestamp < baselineTime) {
16186
- let lo = 0;
16187
- let hi = syncEnd;
16188
- while (lo < hi) {
16189
- const mid = lo + hi >>> 1;
16190
- if (neededEvents[mid].timestamp <= lastPlayedTimestamp)
16191
- lo = mid + 1;
16192
- else hi = mid;
16202
+ const syncEvents = new Array();
16203
+ for (let i2 = startIndex; i2 < events.length; i2++) {
16204
+ const event2 = events[i2];
16205
+ if (lastPlayedTimestamp && lastPlayedTimestamp < baselineTime && (event2.timestamp <= lastPlayedTimestamp || event2 === lastPlayedEvent)) {
16206
+ continue;
16193
16207
  }
16194
- syncStart = lo;
16195
- if (syncStart < syncEnd && neededEvents[syncStart] === lastPlayedEvent) {
16196
- syncStart++;
16208
+ if (event2.timestamp < baselineTime) {
16209
+ syncEvents.push(event2);
16210
+ } else {
16211
+ addDelay(event2, baselineTime);
16212
+ const castFn = getCastFn(event2, false);
16213
+ timer.addAction({
16214
+ doAction: () => {
16215
+ castFn();
16216
+ },
16217
+ delay: (_a4 = event2.delay) != null ? _a4 : 0
16218
+ });
16197
16219
  }
16198
16220
  }
16199
- for (let i2 = syncEnd; i2 < neededEvents.length; i2++) {
16200
- addDelay(neededEvents[i2], baselineTime);
16201
- }
16202
- const syncEvents = neededEvents.slice(syncStart, syncEnd);
16203
- for (let i2 = syncEnd; i2 < neededEvents.length; i2++) {
16204
- const event2 = neededEvents[i2];
16205
- const castFn = getCastFn(event2, false);
16206
- timer.addAction({
16207
- doAction: () => {
16208
- castFn();
16209
- },
16210
- delay: (_a4 = event2.delay) != null ? _a4 : 0
16211
- });
16212
- }
16213
16221
  applyEventsSynchronously(syncEvents);
16214
16222
  emitter.emit(ReplayerEvents.Flush);
16215
16223
  timer.start();
@@ -16233,19 +16241,21 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16233
16241
  }),
16234
16242
  addEvent: o((ctx, machineEvent) => {
16235
16243
  var _a4;
16236
- const { baselineTime, timer, events } = ctx;
16244
+ const { baselineTime, timer, events, checkpointIndex } = ctx;
16237
16245
  if (machineEvent.type === "ADD_EVENT") {
16238
16246
  const { event: event2 } = machineEvent.payload;
16239
16247
  addDelay(event2, baselineTime);
16240
16248
  let end = events.length - 1;
16249
+ let insertionIndex;
16241
16250
  if (!events[end] || events[end].timestamp <= event2.timestamp) {
16242
16251
  if (events[end] && isDuplicateEvent(events[end], event2)) {
16243
16252
  return __spreadProps(__spreadValues({}, ctx), { events });
16244
16253
  }
16245
16254
  events.push(event2);
16255
+ insertionIndex = events.length - 1;
16246
16256
  } else {
16247
- let insertionIndex = -1;
16248
16257
  let start = 0;
16258
+ insertionIndex = 0;
16249
16259
  while (start <= end) {
16250
16260
  const mid = Math.floor((start + end) / 2);
16251
16261
  if (events[mid].timestamp <= event2.timestamp) {
@@ -16254,9 +16264,7 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16254
16264
  end = mid - 1;
16255
16265
  }
16256
16266
  }
16257
- if (insertionIndex === -1) {
16258
- insertionIndex = start;
16259
- }
16267
+ insertionIndex = start;
16260
16268
  for (let i2 = insertionIndex - 1; i2 >= 0 && events[i2].timestamp === event2.timestamp; i2--) {
16261
16269
  if (isDuplicateEvent(events[i2], event2)) {
16262
16270
  return __spreadProps(__spreadValues({}, ctx), { events });
@@ -16269,6 +16277,39 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16269
16277
  }
16270
16278
  events.splice(insertionIndex, 0, event2);
16271
16279
  }
16280
+ if (event2.type === EventType$1.Meta) {
16281
+ const newCheckpoint = {
16282
+ metaEventIndex: insertionIndex,
16283
+ timestamp: event2.timestamp
16284
+ };
16285
+ const lastCheckpoint = checkpointIndex[checkpointIndex.length - 1];
16286
+ if (!lastCheckpoint || lastCheckpoint.timestamp <= event2.timestamp) {
16287
+ checkpointIndex.push(newCheckpoint);
16288
+ } else {
16289
+ for (const checkpoint of checkpointIndex) {
16290
+ if (checkpoint.metaEventIndex >= insertionIndex) {
16291
+ checkpoint.metaEventIndex++;
16292
+ }
16293
+ }
16294
+ let searchLo = 0;
16295
+ let searchHi = checkpointIndex.length - 1;
16296
+ while (searchLo <= searchHi) {
16297
+ const mid = Math.floor((searchLo + searchHi) / 2);
16298
+ if (checkpointIndex[mid].timestamp <= event2.timestamp) {
16299
+ searchLo = mid + 1;
16300
+ } else {
16301
+ searchHi = mid - 1;
16302
+ }
16303
+ }
16304
+ checkpointIndex.splice(searchLo, 0, newCheckpoint);
16305
+ }
16306
+ } else if (insertionIndex < events.length - 1) {
16307
+ for (const checkpoint of checkpointIndex) {
16308
+ if (checkpoint.metaEventIndex >= insertionIndex) {
16309
+ checkpoint.metaEventIndex++;
16310
+ }
16311
+ }
16312
+ }
16272
16313
  const isSync = event2.timestamp < baselineTime;
16273
16314
  const castFn = getCastFn(event2, isSync);
16274
16315
  if (isSync) {
@@ -16282,7 +16323,7 @@ function createPlayerService(context, { getCastFn, applyEventsSynchronously, emi
16282
16323
  });
16283
16324
  }
16284
16325
  }
16285
- return __spreadProps(__spreadValues({}, ctx), { events });
16326
+ return __spreadProps(__spreadValues({}, ctx), { events, checkpointIndex });
16286
16327
  })
16287
16328
  }
16288
16329
  }
@@ -16823,6 +16864,13 @@ class Replayer {
16823
16864
  });
16824
16865
  __publicField2(this, "applyEventsSynchronously", (events2) => {
16825
16866
  var _a22;
16867
+ let lastFullSnapshotIndex = -1;
16868
+ for (let i2 = events2.length - 1; i2 >= 0; i2--) {
16869
+ if (events2[i2].type === EventType$1.FullSnapshot) {
16870
+ lastFullSnapshotIndex = i2;
16871
+ break;
16872
+ }
16873
+ }
16826
16874
  let startIdx = 0;
16827
16875
  if (this.config.useSeekCache && events2.length > 0) {
16828
16876
  const fullSnapshotEvt = events2.find(
@@ -16868,10 +16916,14 @@ class Replayer {
16868
16916
  case EventType$1.Load:
16869
16917
  case EventType$1.Custom:
16870
16918
  continue;
16919
+ case EventType$1.IncrementalSnapshot:
16920
+ if (i2 < lastFullSnapshotIndex) {
16921
+ continue;
16922
+ }
16923
+ break;
16871
16924
  case EventType$1.FullSnapshot:
16872
16925
  case EventType$1.Meta:
16873
16926
  case EventType$1.Plugin:
16874
- case EventType$1.IncrementalSnapshot:
16875
16927
  break;
16876
16928
  }
16877
16929
  const castFn = this.getCastFn(event2, true);
@@ -17134,18 +17186,21 @@ class Replayer {
17134
17186
  const timer = new Timer([], {
17135
17187
  speed: this.config.speed
17136
17188
  });
17189
+ const sortedEvents = events.map((e2) => {
17190
+ if (config && config.unpackFn) {
17191
+ return config.unpackFn(e2);
17192
+ }
17193
+ return e2;
17194
+ }).sort((a1, a2) => a1.timestamp - a2.timestamp);
17195
+ const checkpointIndex = buildCheckpointIndex(sortedEvents);
17137
17196
  this.service = createPlayerService(
17138
17197
  {
17139
- events: events.map((e2) => {
17140
- if (config && config.unpackFn) {
17141
- return config.unpackFn(e2);
17142
- }
17143
- return e2;
17144
- }).sort((a1, a2) => a1.timestamp - a2.timestamp),
17198
+ events: sortedEvents,
17145
17199
  timer,
17146
17200
  timeOffset: 0,
17147
17201
  baselineTime: 0,
17148
- lastPlayedEvent: null
17202
+ lastPlayedEvent: null,
17203
+ checkpointIndex
17149
17204
  },
17150
17205
  {
17151
17206
  getCastFn: this.getCastFn,