@tanstack/virtual-core 3.17.6 → 3.17.8

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.
@@ -62,10 +62,20 @@ function notUndefined(value, msg) {
62
62
  const approxEqual = (a, b) => Math.abs(a - b) < 1.01;
63
63
  const debounce = (targetWindow, fn, ms) => {
64
64
  let timeoutId;
65
- return function(...args) {
66
- targetWindow.clearTimeout(timeoutId);
67
- timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms);
68
- };
65
+ return Object.assign(
66
+ function(...args) {
67
+ targetWindow.clearTimeout(timeoutId);
68
+ timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms);
69
+ },
70
+ {
71
+ // The handle is closure-local, so a caller that has already
72
+ // unsubscribed has no way to stop a queued call. Teardown paths use
73
+ // this to drop the pending invocation instead of letting it land.
74
+ cancel: () => {
75
+ targetWindow.clearTimeout(timeoutId);
76
+ }
77
+ }
78
+ );
69
79
  };
70
80
  exports.approxEqual = approxEqual;
71
81
  exports.debounce = debounce;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["export type NoInfer<A extends any> = [A][A extends any ? 0 : never]\n\nexport type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>\n\nexport function memo<TDeps extends ReadonlyArray<any>, TResult>(\n getDeps: () => [...TDeps],\n fn: (...args: NoInfer<[...TDeps]>) => TResult,\n opts: {\n key: false | string\n debug?: () => boolean\n onChange?: (result: TResult) => void\n initialDeps?: TDeps\n skipInitialOnChange?: boolean\n },\n) {\n let deps = opts.initialDeps ?? []\n let result: TResult | undefined\n let isInitial = true\n\n function memoizedFunction(): TResult {\n // Debug-only timing. In production builds, `process.env.NODE_ENV !==\n // 'production'` is constant-folded to `false` by downstream minifiers\n // (Terser/esbuild/swc with `define`), which DCEs the entire block.\n const debugEnabled =\n process.env.NODE_ENV !== 'production' && !!opts.key && !!opts.debug?.()\n let depTime = 0\n if (debugEnabled) depTime = Date.now()\n\n const newDeps = getDeps()\n\n const depsChanged =\n newDeps.length !== deps.length ||\n newDeps.some((dep: any, index: number) => deps[index] !== dep)\n\n if (!depsChanged) {\n return result!\n }\n\n deps = newDeps\n\n let resultTime = 0\n if (debugEnabled) resultTime = Date.now()\n\n result = fn(...newDeps)\n\n if (debugEnabled) {\n const depEndTime = Math.round((Date.now() - depTime) * 100) / 100\n const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100\n const resultFpsPercentage = resultEndTime / 16\n\n const pad = (str: number | string, num: number) => {\n str = String(str)\n while (str.length < num) {\n str = ' ' + str\n }\n return str\n }\n\n console.info(\n `%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`,\n `\n font-size: .6rem;\n font-weight: bold;\n color: hsl(${Math.max(\n 0,\n Math.min(120 - 120 * resultFpsPercentage, 120),\n )}deg 100% 31%);`,\n opts?.key,\n )\n }\n\n if (opts?.onChange && !(isInitial && opts.skipInitialOnChange)) {\n opts.onChange(result)\n }\n\n isInitial = false\n\n return result\n }\n\n // Attach updateDeps to the function itself\n memoizedFunction.updateDeps = (newDeps: [...TDeps]) => {\n deps = newDeps\n }\n\n return memoizedFunction\n}\n\nexport function notUndefined<T>(value: T | undefined, msg?: string): T {\n if (value === undefined) {\n throw new Error(`Unexpected undefined${msg ? `: ${msg}` : ''}`)\n } else {\n return value\n }\n}\n\nexport const approxEqual = (a: number, b: number) => Math.abs(a - b) < 1.01\n\nexport const debounce = (\n targetWindow: Window & typeof globalThis,\n fn: Function,\n ms: number,\n) => {\n let timeoutId: number\n return function (this: any, ...args: Array<any>) {\n targetWindow.clearTimeout(timeoutId)\n timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)\n }\n}\n"],"names":[],"mappings":";;AAIO,SAAS,KACd,SACA,IACA,MAOA;AACA,MAAI,OAAO,KAAK,eAAe,CAAA;AAC/B,MAAI;AACJ,MAAI,YAAY;AAEhB,WAAS,mBA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ACrE,MAAI,UAAU,QAAW;AACvB,UAAM,IAAI,MAAM,uBAAuB,MAAM,KAAK,GAAG,KAAK,EAAE,EAAE;AAAA,EAChE,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEO,MAAM,cAAc,CAAC,GAAW,MAAc,KAAK,IAAI,IAAI,CAAC,IAAI;AAEhE,MAAM,WAAW,CACtB,cACA,IACA,OACG;AACH,MAAI;AACJ,SAAO,YAAwB,MAAkB;AAC/C,iBAAa,aAAa,SAAS;AACnC,gBAAY,aAAa,WAAW,MAAM,GAAG,MAAM,MAAM,IAAI,GAAG,EAAE;AAAA,EACpE;AACF;;;;;"}
1
+ {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["export type NoInfer<A extends any> = [A][A extends any ? 0 : never]\n\nexport type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>\n\nexport function memo<TDeps extends ReadonlyArray<any>, TResult>(\n getDeps: () => [...TDeps],\n fn: (...args: NoInfer<[...TDeps]>) => TResult,\n opts: {\n key: false | string\n debug?: () => boolean\n onChange?: (result: TResult) => void\n initialDeps?: TDeps\n skipInitialOnChange?: boolean\n },\n) {\n let deps = opts.initialDeps ?? []\n let result: TResult | undefined\n let isInitial = true\n\n function memoizedFunction(): TResult {\n // Debug-only timing. In production builds, `process.env.NODE_ENV !==\n // 'production'` is constant-folded to `false` by downstream minifiers\n // (Terser/esbuild/swc with `define`), which DCEs the entire block.\n const debugEnabled =\n process.env.NODE_ENV !== 'production' && !!opts.key && !!opts.debug?.()\n let depTime = 0\n if (debugEnabled) depTime = Date.now()\n\n const newDeps = getDeps()\n\n const depsChanged =\n newDeps.length !== deps.length ||\n newDeps.some((dep: any, index: number) => deps[index] !== dep)\n\n if (!depsChanged) {\n return result!\n }\n\n deps = newDeps\n\n let resultTime = 0\n if (debugEnabled) resultTime = Date.now()\n\n result = fn(...newDeps)\n\n if (debugEnabled) {\n const depEndTime = Math.round((Date.now() - depTime) * 100) / 100\n const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100\n const resultFpsPercentage = resultEndTime / 16\n\n const pad = (str: number | string, num: number) => {\n str = String(str)\n while (str.length < num) {\n str = ' ' + str\n }\n return str\n }\n\n console.info(\n `%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`,\n `\n font-size: .6rem;\n font-weight: bold;\n color: hsl(${Math.max(\n 0,\n Math.min(120 - 120 * resultFpsPercentage, 120),\n )}deg 100% 31%);`,\n opts?.key,\n )\n }\n\n if (opts?.onChange && !(isInitial && opts.skipInitialOnChange)) {\n opts.onChange(result)\n }\n\n isInitial = false\n\n return result\n }\n\n // Attach updateDeps to the function itself\n memoizedFunction.updateDeps = (newDeps: [...TDeps]) => {\n deps = newDeps\n }\n\n return memoizedFunction\n}\n\nexport function notUndefined<T>(value: T | undefined, msg?: string): T {\n if (value === undefined) {\n throw new Error(`Unexpected undefined${msg ? `: ${msg}` : ''}`)\n } else {\n return value\n }\n}\n\nexport const approxEqual = (a: number, b: number) => Math.abs(a - b) < 1.01\n\nexport const debounce = (\n targetWindow: Window & typeof globalThis,\n fn: Function,\n ms: number,\n) => {\n let timeoutId: number\n return Object.assign(\n function (this: any, ...args: Array<any>) {\n targetWindow.clearTimeout(timeoutId)\n timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)\n },\n {\n // The handle is closure-local, so a caller that has already\n // unsubscribed has no way to stop a queued call. Teardown paths use\n // this to drop the pending invocation instead of letting it land.\n cancel: () => {\n targetWindow.clearTimeout(timeoutId)\n },\n },\n )\n}\n"],"names":[],"mappings":";;AAIO,SAAS,KACd,SACA,IACA,MAOA;AACA,MAAI,OAAO,KAAK,eAAe,CAAA;AAC/B,MAAI;AACJ,MAAI,YAAY;AAEhB,WAAS,mBAA4B;;AAInC,UAAM,eACJ,QAAQ,IAAI,aAAa,gBAAgB,CAAC,CAAC,KAAK,OAAO,CAAC,GAAC,UAAK,UAAL;AAC3D,QAAI,UAAU;AACd,QAAI,aA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ACF;AAEO,MAAM,cAAc,CAAC,GAAW,MAAc,KAAK,IAAI,IAAI,CAAC,IAAI;AAEhE,MAAM,WAAW,CACtB,cACA,IACA,OACG;AACH,MAAI;AACJ,SAAO,OAAO;AAAA,IACZ,YAAwB,MAAkB;AACxC,mBAAa,aAAa,SAAS;AACnC,kBAAY,aAAa,WAAW,MAAM,GAAG,MAAM,MAAM,IAAI,GAAG,EAAE;AAAA,IACpE;AAAA,IACA;AAAA;AAAA;AAAA;AAAA,MAIE,QAAQ,MAAM;AACZ,qBAAa,aAAa,SAAS;AAAA,MACrC;AAAA,IAAA;AAAA,EACF;AAEJ;;;;;"}
@@ -12,4 +12,6 @@ export declare function memo<TDeps extends ReadonlyArray<any>, TResult>(getDeps:
12
12
  };
13
13
  export declare function notUndefined<T>(value: T | undefined, msg?: string): T;
14
14
  export declare const approxEqual: (a: number, b: number) => boolean;
15
- export declare const debounce: (targetWindow: Window & typeof globalThis, fn: Function, ms: number) => (this: any, ...args: Array<any>) => void;
15
+ export declare const debounce: (targetWindow: Window & typeof globalThis, fn: Function, ms: number) => ((this: any, ...args: Array<any>) => void) & {
16
+ cancel: () => void;
17
+ };
@@ -108,7 +108,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
108
108
  scrollRect: Rect | null;
109
109
  scrollOffset: number | null;
110
110
  scrollDirection: ScrollDirection | null;
111
- private scrollAdjustments;
111
+ scrollAdjustments: number;
112
112
  private _iosDeferredAdjustment;
113
113
  private _iosTouching;
114
114
  private _iosJustTouchEnded;
@@ -137,6 +137,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
137
137
  private getSize;
138
138
  private getScrollOffset;
139
139
  private getMeasurementOptions;
140
+ private isIndexInRange;
140
141
  private getMeasurements;
141
142
  calculateRange: {
142
143
  (): {
package/dist/esm/index.js CHANGED
@@ -113,6 +113,7 @@ const observeOffset = (instance, cb, readOffset) => {
113
113
  if (registerScrollendEvent) {
114
114
  element.removeEventListener("scrollend", endHandler);
115
115
  }
116
+ fallback == null ? void 0 : fallback.cancel();
116
117
  };
117
118
  };
118
119
  const observeElementOffset = (instance, cb) => observeOffset(instance, cb, (el) => {
@@ -216,6 +217,7 @@ class Virtualizer {
216
217
  }
217
218
  return;
218
219
  }
220
+ if (!this.isIndexInRange(index)) return;
219
221
  if (this.shouldMeasureDuringScroll(index)) {
220
222
  this.resizeItem(
221
223
  index,
@@ -376,6 +378,8 @@ class Virtualizer {
376
378
  this.rafId = null;
377
379
  }
378
380
  this.scrollState = null;
381
+ this.isScrolling = false;
382
+ this.scrollDirection = null;
379
383
  this._iosDeferredAdjustment = 0;
380
384
  this._iosTouching = false;
381
385
  this._iosJustTouchEnded = false;
@@ -568,6 +572,7 @@ class Virtualizer {
568
572
  key: false
569
573
  }
570
574
  );
575
+ this.isIndexInRange = (index) => index >= 0 && index < this.options.count;
571
576
  this.getMeasurements = memo(
572
577
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
573
578
  ({
@@ -814,6 +819,7 @@ class Virtualizer {
814
819
  return;
815
820
  }
816
821
  const index = this.indexFromElement(node);
822
+ if (!this.isIndexInRange(index)) return;
817
823
  const key = this.options.getItemKey(index);
818
824
  const prevNode = this.elementsCache.get(key);
819
825
  if (prevNode !== node) {
@@ -829,7 +835,7 @@ class Virtualizer {
829
835
  };
830
836
  this.resizeItem = (index, size) => {
831
837
  var _a, _b;
832
- if (index < 0 || index >= this.options.count) return;
838
+ if (!this.isIndexInRange(index)) return;
833
839
  let cachedSize;
834
840
  let itemStart;
835
841
  let key;
@@ -886,12 +892,15 @@ class Virtualizer {
886
892
  }
887
893
  this.itemSizeCache.set(key, size);
888
894
  this.itemSizeCacheVersion++;
895
+ let adjustedSync = false;
889
896
  if (wasAtEnd) {
890
- this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize);
897
+ adjustedSync = this.applyScrollAdjustment(
898
+ this.getTotalSize() - prevTotalSize
899
+ );
891
900
  } else if (shouldAdjustScroll) {
892
- this.applyScrollAdjustment(delta);
901
+ adjustedSync = this.applyScrollAdjustment(delta);
893
902
  }
894
- this.notify(false);
903
+ this.notify(adjustedSync);
895
904
  }
896
905
  };
897
906
  this.getVirtualItems = memo(
@@ -1114,13 +1123,18 @@ class Virtualizer {
1114
1123
  };
1115
1124
  this.setOptions(opts);
1116
1125
  }
1126
+ // Returns `true` when it performed a synchronous `scrollTop` write this
1127
+ // tick, `false` when the delta was zero or the write was deferred (iOS).
1128
+ // `resizeItem` uses that to decide whether the follow-up `notify` must be
1129
+ // synchronous so the grown transforms commit in the same paint (#1227).
1117
1130
  applyScrollAdjustment(delta, behavior) {
1118
- if (delta === 0) return;
1131
+ if (delta === 0) return false;
1119
1132
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1120
1133
  console.info("correction", delta);
1121
1134
  }
1122
1135
  if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1123
1136
  this._iosDeferredAdjustment += delta;
1137
+ return false;
1124
1138
  } else {
1125
1139
  this._scrollToOffset(this.getScrollOffset(), {
1126
1140
  adjustments: this.scrollAdjustments += delta,
@@ -1131,6 +1145,7 @@ class Virtualizer {
1131
1145
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1132
1146
  this.scrollAdjustments = 0;
1133
1147
  }
1148
+ return true;
1134
1149
  }
1135
1150
  }
1136
1151
  scheduleScrollReconcile() {