@tanstack/virtual-core 3.19.3 → 3.19.4

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.
package/dist/esm/index.js CHANGED
@@ -15,6 +15,22 @@ const getRect = (element) => {
15
15
  const { offsetWidth, offsetHeight } = element;
16
16
  return { width: offsetWidth, height: offsetHeight };
17
17
  };
18
+ const createFrameQueue = (targetWindow) => {
19
+ const ids = /* @__PURE__ */ new Set();
20
+ return {
21
+ schedule: (fn) => {
22
+ const id = targetWindow.requestAnimationFrame(() => {
23
+ ids.delete(id);
24
+ fn();
25
+ });
26
+ ids.add(id);
27
+ },
28
+ cancel: () => {
29
+ ids.forEach((id) => targetWindow.cancelAnimationFrame(id));
30
+ ids.clear();
31
+ }
32
+ };
33
+ };
18
34
  const defaultKeyExtractor = (index) => index;
19
35
  const defaultRangeExtractor = (range) => {
20
36
  const overscan = range.overscan * Math.max(range.lanes ?? 1, 1);
@@ -45,6 +61,7 @@ const observeElementRect = (instance, cb) => {
45
61
  return () => {
46
62
  };
47
63
  }
64
+ const frames = createFrameQueue(targetWindow);
48
65
  const observer = new targetWindow.ResizeObserver((entries) => {
49
66
  const run = () => {
50
67
  const entry = entries[0];
@@ -57,11 +74,12 @@ const observeElementRect = (instance, cb) => {
57
74
  }
58
75
  handler(getRect(element));
59
76
  };
60
- instance.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
77
+ instance.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
61
78
  });
62
79
  observer.observe(element, { box: "border-box" });
63
80
  return () => {
64
81
  observer.unobserve(element);
82
+ frames.cancel();
65
83
  };
66
84
  };
67
85
  const addEventListenerOptions = {
@@ -219,14 +237,17 @@ class Virtualizer {
219
237
  };
220
238
  this.observer = /* @__PURE__ */ (() => {
221
239
  let _ro = null;
240
+ let _frames = null;
222
241
  const get = () => {
223
242
  if (_ro) {
224
243
  return _ro;
225
244
  }
226
- if (!this.targetWindow || !this.targetWindow.ResizeObserver) {
245
+ const targetWindow = this.targetWindow;
246
+ if (!targetWindow || !targetWindow.ResizeObserver) {
227
247
  return null;
228
248
  }
229
- return _ro = new this.targetWindow.ResizeObserver((entries) => {
249
+ const frames = _frames = createFrameQueue(targetWindow);
250
+ return _ro = new targetWindow.ResizeObserver((entries) => {
230
251
  const run = () => {
231
252
  const batch = { sync: false };
232
253
  this._resizeBatch = batch;
@@ -240,7 +261,7 @@ class Virtualizer {
240
261
  }
241
262
  }
242
263
  };
243
- this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
264
+ this.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
244
265
  });
245
266
  };
246
267
  const measureEntry = (entry) => {
@@ -265,7 +286,9 @@ class Virtualizer {
265
286
  disconnect: () => {
266
287
  var _a;
267
288
  (_a = get()) == null ? void 0 : _a.disconnect();
289
+ _frames == null ? void 0 : _frames.cancel();
268
290
  _ro = null;
291
+ _frames = null;
269
292
  },
270
293
  observe: (target) => {
271
294
  var _a;