@tanstack/virtual-core
Advanced tools
Comparing version 3.0.0-beta.40 to 3.0.0-beta.41
@@ -100,3 +100,3 @@ export * from './utils'; | ||
private getMeasurements; | ||
calculateRange: () => { | ||
calculateRange: (fArgs_0?: boolean | undefined) => { | ||
startIndex: number; | ||
@@ -109,3 +109,3 @@ endIndex: number; | ||
measureElement: (node: TItemElement | null) => void; | ||
getVirtualItems: () => VirtualItem[]; | ||
getVirtualItems: (...fArgs: readonly any[]) => VirtualItem[]; | ||
getOffsetForAlignment: (toOffset: number, align: ScrollAlignment) => number; | ||
@@ -112,0 +112,0 @@ scrollToOffset: (toOffset: number, { align, behavior }?: ScrollToOffsetOptions) => void; |
@@ -268,4 +268,2 @@ /** | ||
})); | ||
} else if (!_this.isScrolling) { | ||
_this.calculateRange(); | ||
} | ||
@@ -306,3 +304,5 @@ }; | ||
return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset]; | ||
}, function (measurements, outerSize, scrollOffset) { | ||
}, function (measurements, outerSize, scrollOffset, _ref4) { | ||
var _ref4$ = _ref4[0], | ||
flush = _ref4$ === void 0 ? true : _ref4$; | ||
var range = calculateRange({ | ||
@@ -315,3 +315,5 @@ measurements: measurements, | ||
_this.range = range; | ||
_this.notify(); | ||
if (flush) { | ||
_this.notify(); | ||
} | ||
} | ||
@@ -326,3 +328,3 @@ return _this.range; | ||
this.getIndexes = memo(function () { | ||
return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count]; | ||
return [_this.options.rangeExtractor, _this.calculateRange(false), _this.options.overscan, _this.options.count]; | ||
}, function (rangeExtractor, range, overscan, count) { | ||
@@ -441,6 +443,6 @@ return rangeExtractor(_extends({}, range, { | ||
this.scrollToOffset = function (toOffset, _temp) { | ||
var _ref4 = _temp === void 0 ? {} : _temp, | ||
_ref4$align = _ref4.align, | ||
align = _ref4$align === void 0 ? 'start' : _ref4$align, | ||
behavior = _ref4.behavior; | ||
var _ref5 = _temp === void 0 ? {} : _temp, | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'start' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
@@ -459,6 +461,6 @@ if (isDynamic && behavior === 'smooth') { | ||
this.scrollToIndex = function (index, _temp2) { | ||
var _ref5 = _temp2 === void 0 ? {} : _temp2, | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
var _ref6 = _temp2 === void 0 ? {} : _temp2, | ||
_ref6$align = _ref6.align, | ||
align = _ref6$align === void 0 ? 'auto' : _ref6$align, | ||
behavior = _ref6.behavior; | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
@@ -526,4 +528,4 @@ clearTimeout(_this.scrollToIndexTimeoutId); | ||
this.scrollBy = function (delta, _temp3) { | ||
var _ref6 = _temp3 === void 0 ? {} : _temp3, | ||
behavior = _ref6.behavior; | ||
var _ref7 = _temp3 === void 0 ? {} : _temp3, | ||
behavior = _ref7.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
@@ -543,5 +545,5 @@ if (isDynamic && behavior === 'smooth') { | ||
}; | ||
this._scrollToOffset = function (offset, _ref7) { | ||
var adjustments = _ref7.adjustments, | ||
behavior = _ref7.behavior; | ||
this._scrollToOffset = function (offset, _ref8) { | ||
var adjustments = _ref8.adjustments, | ||
behavior = _ref8.behavior; | ||
_this.options.scrollToFn(offset, { | ||
@@ -583,6 +585,6 @@ behavior: behavior, | ||
}; | ||
function calculateRange(_ref8) { | ||
var measurements = _ref8.measurements, | ||
outerSize = _ref8.outerSize, | ||
scrollOffset = _ref8.scrollOffset; | ||
function calculateRange(_ref9) { | ||
var measurements = _ref9.measurements, | ||
outerSize = _ref9.outerSize, | ||
scrollOffset = _ref9.scrollOffset; | ||
var count = measurements.length - 1; | ||
@@ -589,0 +591,0 @@ var getOffset = function getOffset(index) { |
@@ -271,4 +271,2 @@ /** | ||
})); | ||
} else if (!_this.isScrolling) { | ||
_this.calculateRange(); | ||
} | ||
@@ -309,3 +307,5 @@ }; | ||
return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset]; | ||
}, function (measurements, outerSize, scrollOffset) { | ||
}, function (measurements, outerSize, scrollOffset, _ref4) { | ||
var _ref4$ = _ref4[0], | ||
flush = _ref4$ === void 0 ? true : _ref4$; | ||
var range = calculateRange({ | ||
@@ -318,3 +318,5 @@ measurements: measurements, | ||
_this.range = range; | ||
_this.notify(); | ||
if (flush) { | ||
_this.notify(); | ||
} | ||
} | ||
@@ -329,3 +331,3 @@ return _this.range; | ||
this.getIndexes = utils.memo(function () { | ||
return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count]; | ||
return [_this.options.rangeExtractor, _this.calculateRange(false), _this.options.overscan, _this.options.count]; | ||
}, function (rangeExtractor, range, overscan, count) { | ||
@@ -444,6 +446,6 @@ return rangeExtractor(_rollupPluginBabelHelpers["extends"]({}, range, { | ||
this.scrollToOffset = function (toOffset, _temp) { | ||
var _ref4 = _temp === void 0 ? {} : _temp, | ||
_ref4$align = _ref4.align, | ||
align = _ref4$align === void 0 ? 'start' : _ref4$align, | ||
behavior = _ref4.behavior; | ||
var _ref5 = _temp === void 0 ? {} : _temp, | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'start' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
@@ -462,6 +464,6 @@ if (isDynamic && behavior === 'smooth') { | ||
this.scrollToIndex = function (index, _temp2) { | ||
var _ref5 = _temp2 === void 0 ? {} : _temp2, | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
var _ref6 = _temp2 === void 0 ? {} : _temp2, | ||
_ref6$align = _ref6.align, | ||
align = _ref6$align === void 0 ? 'auto' : _ref6$align, | ||
behavior = _ref6.behavior; | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
@@ -529,4 +531,4 @@ clearTimeout(_this.scrollToIndexTimeoutId); | ||
this.scrollBy = function (delta, _temp3) { | ||
var _ref6 = _temp3 === void 0 ? {} : _temp3, | ||
behavior = _ref6.behavior; | ||
var _ref7 = _temp3 === void 0 ? {} : _temp3, | ||
behavior = _ref7.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
@@ -546,5 +548,5 @@ if (isDynamic && behavior === 'smooth') { | ||
}; | ||
this._scrollToOffset = function (offset, _ref7) { | ||
var adjustments = _ref7.adjustments, | ||
behavior = _ref7.behavior; | ||
this._scrollToOffset = function (offset, _ref8) { | ||
var adjustments = _ref8.adjustments, | ||
behavior = _ref8.behavior; | ||
_this.options.scrollToFn(offset, { | ||
@@ -586,6 +588,6 @@ behavior: behavior, | ||
}; | ||
function calculateRange(_ref8) { | ||
var measurements = _ref8.measurements, | ||
outerSize = _ref8.outerSize, | ||
scrollOffset = _ref8.scrollOffset; | ||
function calculateRange(_ref9) { | ||
var measurements = _ref9.measurements, | ||
outerSize = _ref9.outerSize, | ||
scrollOffset = _ref9.scrollOffset; | ||
var count = measurements.length - 1; | ||
@@ -592,0 +594,0 @@ var getOffset = function getOffset(index) { |
export type NoInfer<A extends any> = [A][A extends any ? 0 : never]; | ||
export type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
export declare function memo<TDeps extends readonly any[], TResult>(getDeps: () => [...TDeps], fn: (...args: NoInfer<[...TDeps]>) => TResult, opts: { | ||
export declare function memo<TDeps extends readonly any[], TResult, FArgs extends readonly any[]>(getDeps: () => [...TDeps], fn: (...args: [...TDeps, FArgs]) => TResult, opts: { | ||
key: any; | ||
debug?: () => any; | ||
onChange?: (result: TResult) => void; | ||
}): () => TResult; | ||
}): (...fArgs: FArgs) => TResult; |
@@ -27,3 +27,6 @@ /** | ||
if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now(); | ||
result = fn.apply(void 0, newDeps); | ||
for (var _len = arguments.length, fArgs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
fArgs[_key] = arguments[_key]; | ||
} | ||
result = fn.apply(void 0, newDeps.concat([fArgs])); | ||
opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result); | ||
@@ -30,0 +33,0 @@ if (opts.key && opts.debug != null && opts.debug()) { |
@@ -31,3 +31,6 @@ /** | ||
if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now(); | ||
result = fn.apply(void 0, newDeps); | ||
for (var _len = arguments.length, fArgs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
fArgs[_key] = arguments[_key]; | ||
} | ||
result = fn.apply(void 0, newDeps.concat([fArgs])); | ||
opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result); | ||
@@ -34,0 +37,0 @@ if (opts.key && opts.debug != null && opts.debug()) { |
@@ -48,3 +48,6 @@ /** | ||
if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now(); | ||
result = fn.apply(void 0, newDeps); | ||
for (var _len = arguments.length, fArgs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
fArgs[_key] = arguments[_key]; | ||
} | ||
result = fn.apply(void 0, newDeps.concat([fArgs])); | ||
opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result); | ||
@@ -321,4 +324,2 @@ if (opts.key && opts.debug != null && opts.debug()) { | ||
})); | ||
} else if (!_this.isScrolling) { | ||
_this.calculateRange(); | ||
} | ||
@@ -359,3 +360,5 @@ }; | ||
return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset]; | ||
}, function (measurements, outerSize, scrollOffset) { | ||
}, function (measurements, outerSize, scrollOffset, _ref4) { | ||
var _ref4$ = _ref4[0], | ||
flush = _ref4$ === void 0 ? true : _ref4$; | ||
var range = calculateRange({ | ||
@@ -368,3 +371,5 @@ measurements: measurements, | ||
_this.range = range; | ||
_this.notify(); | ||
if (flush) { | ||
_this.notify(); | ||
} | ||
} | ||
@@ -379,3 +384,3 @@ return _this.range; | ||
this.getIndexes = memo(function () { | ||
return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count]; | ||
return [_this.options.rangeExtractor, _this.calculateRange(false), _this.options.overscan, _this.options.count]; | ||
}, function (rangeExtractor, range, overscan, count) { | ||
@@ -494,6 +499,6 @@ return rangeExtractor(_extends({}, range, { | ||
this.scrollToOffset = function (toOffset, _temp) { | ||
var _ref4 = _temp === void 0 ? {} : _temp, | ||
_ref4$align = _ref4.align, | ||
align = _ref4$align === void 0 ? 'start' : _ref4$align, | ||
behavior = _ref4.behavior; | ||
var _ref5 = _temp === void 0 ? {} : _temp, | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'start' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
@@ -512,6 +517,6 @@ if (isDynamic && behavior === 'smooth') { | ||
this.scrollToIndex = function (index, _temp2) { | ||
var _ref5 = _temp2 === void 0 ? {} : _temp2, | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
var _ref6 = _temp2 === void 0 ? {} : _temp2, | ||
_ref6$align = _ref6.align, | ||
align = _ref6$align === void 0 ? 'auto' : _ref6$align, | ||
behavior = _ref6.behavior; | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
@@ -579,4 +584,4 @@ clearTimeout(_this.scrollToIndexTimeoutId); | ||
this.scrollBy = function (delta, _temp3) { | ||
var _ref6 = _temp3 === void 0 ? {} : _temp3, | ||
behavior = _ref6.behavior; | ||
var _ref7 = _temp3 === void 0 ? {} : _temp3, | ||
behavior = _ref7.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
@@ -596,5 +601,5 @@ if (isDynamic && behavior === 'smooth') { | ||
}; | ||
this._scrollToOffset = function (offset, _ref7) { | ||
var adjustments = _ref7.adjustments, | ||
behavior = _ref7.behavior; | ||
this._scrollToOffset = function (offset, _ref8) { | ||
var adjustments = _ref8.adjustments, | ||
behavior = _ref8.behavior; | ||
_this.options.scrollToFn(offset, { | ||
@@ -636,6 +641,6 @@ behavior: behavior, | ||
}; | ||
function calculateRange(_ref8) { | ||
var measurements = _ref8.measurements, | ||
outerSize = _ref8.outerSize, | ||
scrollOffset = _ref8.scrollOffset; | ||
function calculateRange(_ref9) { | ||
var measurements = _ref9.measurements, | ||
outerSize = _ref9.outerSize, | ||
scrollOffset = _ref9.scrollOffset; | ||
var count = measurements.length - 1; | ||
@@ -642,0 +647,0 @@ var getOffset = function getOffset(index) { |
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VirtualCore={})}(this,(function(e){"use strict";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},t.apply(this,arguments)}function n(e,t,n){var o,r=[];return function(){var i;n.key&&null!=n.debug&&n.debug()&&(i=Date.now());var s,l=e();if(!(l.length!==r.length||l.some((function(e,t){return r[t]!==e}))))return o;if(r=l,n.key&&null!=n.debug&&n.debug()&&(s=Date.now()),o=t.apply(void 0,l),null==n||null==n.onChange||n.onChange(o),n.key&&null!=n.debug&&n.debug()){var a=Math.round(100*(Date.now()-i))/100,u=Math.round(100*(Date.now()-s))/100,c=u/16,d=function(e,t){for(e=String(e);e.length<t;)e=" "+e;return e};console.info("%c⏱ "+d(u,5)+" /"+d(a,5)+" ms","\n font-size: .6rem;\n font-weight: bold;\n color: hsl("+Math.max(0,Math.min(120-120*c,120))+"deg 100% 31%);",null==n?void 0:n.key)}return o}}var o=function(e){return e},r=function(e){for(var t=Math.max(e.startIndex-e.overscan,0),n=Math.min(e.endIndex+e.overscan,e.count-1),o=[],r=t;r<=n;r++)o.push(r);return o},i={element:["scrollLeft","scrollTop"],window:["scrollX","scrollY"]},s=function(e){return function(t,n){if(t.scrollElement){var o=i[e][0],r=i[e][1],s=t.scrollElement[o],l=t.scrollElement[r],a=function(){var e=t.scrollElement[t.options.horizontal?o:r];n(e)};a();var u=function(e){var n=e.currentTarget,i=n[o],u=n[r];(t.options.horizontal?s-i:l-u)&&a(),s=i,l=u};return t.scrollElement.addEventListener("scroll",u,{capture:!1,passive:!0}),function(){t.scrollElement.removeEventListener("scroll",u)}}}},l=s("element"),a=s("window"),u=function(e,t){return Math.round(e.getBoundingClientRect()[t.options.horizontal?"width":"height"])};e.Virtualizer=function(e){var i,s=this;this.unsubs=[],this.scrollElement=null,this.isScrolling=!1,this.isScrollingTimeoutId=null,this.scrollToIndexTimeoutId=null,this.measurementsCache=[],this.itemSizeCache={},this.pendingMeasuredCacheIndexes=[],this.scrollDirection=null,this.scrollAdjustments=0,this.measureElementCache={},this.getResizeObserver=(i=null,function(){return i||("undefined"!=typeof ResizeObserver?i=new ResizeObserver((function(e){e.forEach((function(e){s._measureElement(e.target,!1)}))})):null)}),this.range={startIndex:0,endIndex:0},this.setOptions=function(e){Object.entries(e).forEach((function(t){var n=t[0];void 0===t[1]&&delete e[n]})),s.options=t({debug:!1,initialOffset:0,overscan:1,paddingStart:0,paddingEnd:0,scrollPaddingStart:0,scrollPaddingEnd:0,horizontal:!1,getItemKey:o,rangeExtractor:r,onChange:function(){},measureElement:u,initialRect:{width:0,height:0},scrollMargin:0,scrollingDelay:150,indexAttribute:"data-index",initialMeasurementsCache:[]},e)},this.notify=function(){null==s.options.onChange||s.options.onChange(s)},this.cleanup=function(){s.unsubs.filter(Boolean).forEach((function(e){return e()})),s.unsubs=[],s.scrollElement=null},this._didMount=function(){var e=s.getResizeObserver();return Object.values(s.measureElementCache).forEach((function(t){return null==e?void 0:e.observe(t)})),function(){null==e||e.disconnect(),s.cleanup()}},this._willUpdate=function(){var e=s.options.getScrollElement();s.scrollElement!==e?(s.cleanup(),s.scrollElement=e,s._scrollToOffset(s.scrollOffset,{adjustments:void 0,behavior:void 0}),s.unsubs.push(s.options.observeElementRect(s,(function(e){s.scrollRect=e,s.calculateRange()}))),s.unsubs.push(s.options.observeElementOffset(s,(function(e){if(s.scrollAdjustments=0,s.scrollOffset!==e){null!==s.isScrollingTimeoutId&&(clearTimeout(s.isScrollingTimeoutId),s.isScrollingTimeoutId=null);var t=function(e){s.isScrolling!==e&&(s.isScrolling=e,s.notify())};s.scrollDirection=s.scrollOffset<e?"forward":"backward",s.scrollOffset=e,s.calculateRange(),t(!0),s.isScrollingTimeoutId=setTimeout((function(){s.isScrollingTimeoutId=null,s.scrollDirection=null,t(!1)}),s.options.scrollingDelay)}})))):s.isScrolling||s.calculateRange()},this.getSize=function(){return s.scrollRect[s.options.horizontal?"width":"height"]},this.getMeasurements=n((function(){return[s.options.count,s.options.paddingStart,s.options.scrollMargin,s.options.getItemKey,s.itemSizeCache]}),(function(e,t,n,o,r){var i=s.pendingMeasuredCacheIndexes.length>0?Math.min.apply(Math,s.pendingMeasuredCacheIndexes):0;s.pendingMeasuredCacheIndexes=[];for(var l=s.measurementsCache.slice(0,i),a=i;a<e;a++){var u=o(a),c=r[u],d=l[a-1]?l[a-1].end:t+n,f="number"==typeof c?c:s.options.estimateSize(a),h=d+f;l[a]={index:a,start:d,size:f,end:h,key:u}}return s.measurementsCache=l,l}),{key:!1,debug:function(){return s.options.debug}}),this.calculateRange=n((function(){return[s.getMeasurements(),s.getSize(),s.scrollOffset]}),(function(e,t,n){var o=function(e){var t=e.measurements,n=e.outerSize,o=e.scrollOffset,r=t.length-1,i=function(e,t,n,o){for(;e<=t;){var r=(e+t)/2|0,i=n(r);if(i<o)e=r+1;else{if(!(i>o))return r;t=r-1}}return e>0?e-1:0}(0,r,(function(e){return t[e].start}),o),s=i;for(;s<r&&t[s].end<o+n;)s++;return{startIndex:i,endIndex:s}}({measurements:e,outerSize:t,scrollOffset:n});return o.startIndex===s.range.startIndex&&o.endIndex===s.range.endIndex||(s.range=o,s.notify()),s.range}),{key:!1,debug:function(){return s.options.debug}}),this.getIndexes=n((function(){return[s.options.rangeExtractor,s.range,s.options.overscan,s.options.count]}),(function(e,n,o,r){return e(t({},n,{overscan:o,count:r}))}),{key:!1,debug:function(){return s.options.debug}}),this.indexFromElement=function(e){var t=s.options.indexAttribute,n=e.getAttribute(t);return n?parseInt(n,10):(console.warn("Missing attribute name '"+t+"={index}' on measured element."),-1)},this._measureElement=function(e,n){var o,r=s.indexFromElement(e),i=s.measurementsCache[r];if(i){var l=s.measureElementCache[i.key],a=s.getResizeObserver();if(!e.isConnected)return null==a||a.unobserve(e),void(e===l&&delete s.measureElementCache[i.key]);if(l!==e)l&&(null==a||a.unobserve(l)),null==a||a.observe(e),s.measureElementCache[i.key]=e;else if(!n&&!l.__virtualizerSkipFirstNotSync)return void(l.__virtualizerSkipFirstNotSync=!0);var u,c=s.options.measureElement(e,s),d=c-(null!=(o=s.itemSizeCache[i.key])?o:i.size);if(0!==d)i.start<s.scrollOffset&&s._scrollToOffset(s.scrollOffset,{adjustments:s.scrollAdjustments+=d,behavior:void 0}),s.pendingMeasuredCacheIndexes.push(r),s.itemSizeCache=t({},s.itemSizeCache,((u={})[i.key]=c,u)),s.notify()}},this.measureElement=function(e){e&&s._measureElement(e,!0)},this.getVirtualItems=n((function(){return[s.getIndexes(),s.getMeasurements()]}),(function(e,t){for(var n=[],o=0,r=e.length;o<r;o++){var i=t[e[o]];n.push(i)}return n}),{key:!1,debug:function(){return s.options.debug}}),this.getOffsetForAlignment=function(e,t){var n=s.getSize();"auto"===t&&(t=e<=s.scrollOffset?"start":e>=s.scrollOffset+n?"end":"start"),"start"===t||("end"===t?e-=n:"center"===t&&(e-=n/2));var o=s.options.horizontal?"scrollWidth":"scrollHeight",r=(s.scrollElement?"document"in s.scrollElement?s.scrollElement.document.documentElement[o]:s.scrollElement[o]:0)-s.getSize();return Math.max(Math.min(r,e),0)},this.scrollToOffset=function(e,t){var n=void 0===t?{}:t,o=n.align,r=void 0===o?"start":o,i=n.behavior;if(Object.keys(s.measureElementCache).length>0&&"smooth"===i)console.warn("The `smooth` scroll behavior is not supported with dynamic size.");else{var l={adjustments:void 0,behavior:i,sync:!1};s._scrollToOffset(s.getOffsetForAlignment(e,r),l)}},this.scrollToIndex=function(e,t){var n=void 0===t?{}:t,o=n.align,r=void 0===o?"auto":o,i=n.behavior;null!==s.scrollToIndexTimeoutId&&(clearTimeout(s.scrollToIndexTimeoutId),s.scrollToIndexTimeoutId=null);var l=Object.keys(s.measureElementCache).length>0;if(l&&"smooth"===i)console.warn("The `smooth` scroll behavior is not supported with dynamic size.");else{var a=function(){var t=s.getMeasurements()[Math.max(0,Math.min(e,s.options.count-1))];if(!t)throw new Error("VirtualItem not found for index = "+e);return t},u=a();if("auto"===r)if(u.end>=s.scrollOffset+s.getSize()-s.options.scrollPaddingEnd)r="end";else{if(!(u.start<=s.scrollOffset+s.options.scrollPaddingStart))return;r="start"}var c=function(e){var t="end"===r?e.end+s.options.scrollPaddingEnd:e.start-s.options.scrollPaddingStart;return s.getOffsetForAlignment(t,r)},d=c(u),f={adjustments:void 0,behavior:i};s._scrollToOffset(d,f);l&&(s.scrollToIndexTimeoutId=setTimeout((function(){var t,n;if(s.scrollToIndexTimeoutId=null,!!s.measureElementCache[s.options.getItemKey(e)]){var o=c(a());t=o,n=s.scrollOffset,Math.abs(t-n)<1||s.scrollToIndex(e,{align:r,behavior:i})}else s.scrollToIndex(e,{align:r,behavior:i})})))}},this.scrollBy=function(e,t){var n=(void 0===t?{}:t).behavior;Object.keys(s.measureElementCache).length>0&&"smooth"===n?console.warn("The `smooth` scroll behavior is not supported with dynamic size."):s._scrollToOffset(s.scrollOffset+e,{adjustments:void 0,behavior:n})},this.getTotalSize=function(){var e;return((null==(e=s.getMeasurements()[s.options.count-1])?void 0:e.end)||s.options.paddingStart)-s.options.scrollMargin+s.options.paddingEnd},this._scrollToOffset=function(e,t){var n=t.adjustments,o=t.behavior;s.options.scrollToFn(e,{behavior:o,adjustments:n},s)},this.measure=function(){s.itemSizeCache={},s.notify()},this.setOptions(e),this.scrollRect=this.options.initialRect,this.scrollOffset=this.options.initialOffset,this.measurementsCache=this.options.initialMeasurementsCache,this.measurementsCache.forEach((function(e){s.itemSizeCache[e.key]=e.size})),this.calculateRange()},e.defaultKeyExtractor=o,e.defaultRangeExtractor=r,e.elementScroll=function(e,t,n){var o,r,i=t.adjustments,s=void 0===i?0:i,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((r={})[n.options.horizontal?"left":"top"]=a,r.behavior=l,r))},e.measureElement=u,e.memo=n,e.observeElementOffset=l,e.observeElementRect=function(e,t){var n=new ResizeObserver((function(e){var n=e[0];if(n){var o=n.contentRect,r=o.width,i=o.height;t({width:Math.round(r),height:Math.round(i)})}else t({width:0,height:0})}));if(e.scrollElement)return t(e.scrollElement.getBoundingClientRect()),n.observe(e.scrollElement),function(){n.unobserve(e.scrollElement)}},e.observeWindowOffset=a,e.observeWindowRect=function(e,t){var n=function(e,t){var n={height:-1,width:-1};return function(o){(e.options.horizontal?o.width!==n.width:o.height!==n.height)&&t(o),n=o}}(e,t),o=function(){return n({width:e.scrollElement.innerWidth,height:e.scrollElement.innerHeight})};if(e.scrollElement)return o(),e.scrollElement.addEventListener("resize",o,{capture:!1,passive:!0}),function(){e.scrollElement.removeEventListener("resize",o)}},e.windowScroll=function(e,t,n){var o,r,i=t.adjustments,s=void 0===i?0:i,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((r={})[n.options.horizontal?"left":"top"]=a,r.behavior=l,r))},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VirtualCore={})}(this,(function(e){"use strict";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},t.apply(this,arguments)}function n(e,t,n){var o,r=[];return function(){var i;n.key&&null!=n.debug&&n.debug()&&(i=Date.now());var s,l=e(),a=l.length!==r.length||l.some((function(e,t){return r[t]!==e}));if(!a)return o;r=l,n.key&&null!=n.debug&&n.debug()&&(s=Date.now());for(var u=arguments.length,c=new Array(u),d=0;d<u;d++)c[d]=arguments[d];if(o=t.apply(void 0,l.concat([c])),null==n||null==n.onChange||n.onChange(o),n.key&&null!=n.debug&&n.debug()){var f=Math.round(100*(Date.now()-i))/100,h=Math.round(100*(Date.now()-s))/100,m=h/16,g=function(e,t){for(e=String(e);e.length<t;)e=" "+e;return e};console.info("%c⏱ "+g(h,5)+" /"+g(f,5)+" ms","\n font-size: .6rem;\n font-weight: bold;\n color: hsl("+Math.max(0,Math.min(120-120*m,120))+"deg 100% 31%);",null==n?void 0:n.key)}return o}}var o=function(e){return e},r=function(e){for(var t=Math.max(e.startIndex-e.overscan,0),n=Math.min(e.endIndex+e.overscan,e.count-1),o=[],r=t;r<=n;r++)o.push(r);return o},i={element:["scrollLeft","scrollTop"],window:["scrollX","scrollY"]},s=function(e){return function(t,n){if(t.scrollElement){var o=i[e][0],r=i[e][1],s=t.scrollElement[o],l=t.scrollElement[r],a=function(){var e=t.scrollElement[t.options.horizontal?o:r];n(e)};a();var u=function(e){var n=e.currentTarget,i=n[o],u=n[r];(t.options.horizontal?s-i:l-u)&&a(),s=i,l=u};return t.scrollElement.addEventListener("scroll",u,{capture:!1,passive:!0}),function(){t.scrollElement.removeEventListener("scroll",u)}}}},l=s("element"),a=s("window"),u=function(e,t){return Math.round(e.getBoundingClientRect()[t.options.horizontal?"width":"height"])};e.Virtualizer=function(e){var i,s=this;this.unsubs=[],this.scrollElement=null,this.isScrolling=!1,this.isScrollingTimeoutId=null,this.scrollToIndexTimeoutId=null,this.measurementsCache=[],this.itemSizeCache={},this.pendingMeasuredCacheIndexes=[],this.scrollDirection=null,this.scrollAdjustments=0,this.measureElementCache={},this.getResizeObserver=(i=null,function(){return i||("undefined"!=typeof ResizeObserver?i=new ResizeObserver((function(e){e.forEach((function(e){s._measureElement(e.target,!1)}))})):null)}),this.range={startIndex:0,endIndex:0},this.setOptions=function(e){Object.entries(e).forEach((function(t){var n=t[0];void 0===t[1]&&delete e[n]})),s.options=t({debug:!1,initialOffset:0,overscan:1,paddingStart:0,paddingEnd:0,scrollPaddingStart:0,scrollPaddingEnd:0,horizontal:!1,getItemKey:o,rangeExtractor:r,onChange:function(){},measureElement:u,initialRect:{width:0,height:0},scrollMargin:0,scrollingDelay:150,indexAttribute:"data-index",initialMeasurementsCache:[]},e)},this.notify=function(){null==s.options.onChange||s.options.onChange(s)},this.cleanup=function(){s.unsubs.filter(Boolean).forEach((function(e){return e()})),s.unsubs=[],s.scrollElement=null},this._didMount=function(){var e=s.getResizeObserver();return Object.values(s.measureElementCache).forEach((function(t){return null==e?void 0:e.observe(t)})),function(){null==e||e.disconnect(),s.cleanup()}},this._willUpdate=function(){var e=s.options.getScrollElement();s.scrollElement!==e&&(s.cleanup(),s.scrollElement=e,s._scrollToOffset(s.scrollOffset,{adjustments:void 0,behavior:void 0}),s.unsubs.push(s.options.observeElementRect(s,(function(e){s.scrollRect=e,s.calculateRange()}))),s.unsubs.push(s.options.observeElementOffset(s,(function(e){if(s.scrollAdjustments=0,s.scrollOffset!==e){null!==s.isScrollingTimeoutId&&(clearTimeout(s.isScrollingTimeoutId),s.isScrollingTimeoutId=null);var t=function(e){s.isScrolling!==e&&(s.isScrolling=e,s.notify())};s.scrollDirection=s.scrollOffset<e?"forward":"backward",s.scrollOffset=e,s.calculateRange(),t(!0),s.isScrollingTimeoutId=setTimeout((function(){s.isScrollingTimeoutId=null,s.scrollDirection=null,t(!1)}),s.options.scrollingDelay)}}))))},this.getSize=function(){return s.scrollRect[s.options.horizontal?"width":"height"]},this.getMeasurements=n((function(){return[s.options.count,s.options.paddingStart,s.options.scrollMargin,s.options.getItemKey,s.itemSizeCache]}),(function(e,t,n,o,r){var i=s.pendingMeasuredCacheIndexes.length>0?Math.min.apply(Math,s.pendingMeasuredCacheIndexes):0;s.pendingMeasuredCacheIndexes=[];for(var l=s.measurementsCache.slice(0,i),a=i;a<e;a++){var u=o(a),c=r[u],d=l[a-1]?l[a-1].end:t+n,f="number"==typeof c?c:s.options.estimateSize(a),h=d+f;l[a]={index:a,start:d,size:f,end:h,key:u}}return s.measurementsCache=l,l}),{key:!1,debug:function(){return s.options.debug}}),this.calculateRange=n((function(){return[s.getMeasurements(),s.getSize(),s.scrollOffset]}),(function(e,t,n,o){var r=o[0],i=void 0===r||r,l=function(e){var t=e.measurements,n=e.outerSize,o=e.scrollOffset,r=t.length-1,i=function(e,t,n,o){for(;e<=t;){var r=(e+t)/2|0,i=n(r);if(i<o)e=r+1;else{if(!(i>o))return r;t=r-1}}return e>0?e-1:0}(0,r,(function(e){return t[e].start}),o),s=i;for(;s<r&&t[s].end<o+n;)s++;return{startIndex:i,endIndex:s}}({measurements:e,outerSize:t,scrollOffset:n});return l.startIndex===s.range.startIndex&&l.endIndex===s.range.endIndex||(s.range=l,i&&s.notify()),s.range}),{key:!1,debug:function(){return s.options.debug}}),this.getIndexes=n((function(){return[s.options.rangeExtractor,s.calculateRange(!1),s.options.overscan,s.options.count]}),(function(e,n,o,r){return e(t({},n,{overscan:o,count:r}))}),{key:!1,debug:function(){return s.options.debug}}),this.indexFromElement=function(e){var t=s.options.indexAttribute,n=e.getAttribute(t);return n?parseInt(n,10):(console.warn("Missing attribute name '"+t+"={index}' on measured element."),-1)},this._measureElement=function(e,n){var o,r=s.indexFromElement(e),i=s.measurementsCache[r];if(i){var l=s.measureElementCache[i.key],a=s.getResizeObserver();if(!e.isConnected)return null==a||a.unobserve(e),void(e===l&&delete s.measureElementCache[i.key]);if(l!==e)l&&(null==a||a.unobserve(l)),null==a||a.observe(e),s.measureElementCache[i.key]=e;else if(!n&&!l.__virtualizerSkipFirstNotSync)return void(l.__virtualizerSkipFirstNotSync=!0);var u,c=s.options.measureElement(e,s),d=c-(null!=(o=s.itemSizeCache[i.key])?o:i.size);if(0!==d)i.start<s.scrollOffset&&s._scrollToOffset(s.scrollOffset,{adjustments:s.scrollAdjustments+=d,behavior:void 0}),s.pendingMeasuredCacheIndexes.push(r),s.itemSizeCache=t({},s.itemSizeCache,((u={})[i.key]=c,u)),s.notify()}},this.measureElement=function(e){e&&s._measureElement(e,!0)},this.getVirtualItems=n((function(){return[s.getIndexes(),s.getMeasurements()]}),(function(e,t){for(var n=[],o=0,r=e.length;o<r;o++){var i=t[e[o]];n.push(i)}return n}),{key:!1,debug:function(){return s.options.debug}}),this.getOffsetForAlignment=function(e,t){var n=s.getSize();"auto"===t&&(t=e<=s.scrollOffset?"start":e>=s.scrollOffset+n?"end":"start"),"start"===t||("end"===t?e-=n:"center"===t&&(e-=n/2));var o=s.options.horizontal?"scrollWidth":"scrollHeight",r=(s.scrollElement?"document"in s.scrollElement?s.scrollElement.document.documentElement[o]:s.scrollElement[o]:0)-s.getSize();return Math.max(Math.min(r,e),0)},this.scrollToOffset=function(e,t){var n=void 0===t?{}:t,o=n.align,r=void 0===o?"start":o,i=n.behavior;if(Object.keys(s.measureElementCache).length>0&&"smooth"===i)console.warn("The `smooth` scroll behavior is not supported with dynamic size.");else{var l={adjustments:void 0,behavior:i,sync:!1};s._scrollToOffset(s.getOffsetForAlignment(e,r),l)}},this.scrollToIndex=function(e,t){var n=void 0===t?{}:t,o=n.align,r=void 0===o?"auto":o,i=n.behavior;null!==s.scrollToIndexTimeoutId&&(clearTimeout(s.scrollToIndexTimeoutId),s.scrollToIndexTimeoutId=null);var l=Object.keys(s.measureElementCache).length>0;if(l&&"smooth"===i)console.warn("The `smooth` scroll behavior is not supported with dynamic size.");else{var a=function(){var t=s.getMeasurements()[Math.max(0,Math.min(e,s.options.count-1))];if(!t)throw new Error("VirtualItem not found for index = "+e);return t},u=a();if("auto"===r)if(u.end>=s.scrollOffset+s.getSize()-s.options.scrollPaddingEnd)r="end";else{if(!(u.start<=s.scrollOffset+s.options.scrollPaddingStart))return;r="start"}var c=function(e){var t="end"===r?e.end+s.options.scrollPaddingEnd:e.start-s.options.scrollPaddingStart;return s.getOffsetForAlignment(t,r)},d=c(u),f={adjustments:void 0,behavior:i};s._scrollToOffset(d,f);l&&(s.scrollToIndexTimeoutId=setTimeout((function(){var t,n;if(s.scrollToIndexTimeoutId=null,!!s.measureElementCache[s.options.getItemKey(e)]){var o=c(a());t=o,n=s.scrollOffset,Math.abs(t-n)<1||s.scrollToIndex(e,{align:r,behavior:i})}else s.scrollToIndex(e,{align:r,behavior:i})})))}},this.scrollBy=function(e,t){var n=(void 0===t?{}:t).behavior;Object.keys(s.measureElementCache).length>0&&"smooth"===n?console.warn("The `smooth` scroll behavior is not supported with dynamic size."):s._scrollToOffset(s.scrollOffset+e,{adjustments:void 0,behavior:n})},this.getTotalSize=function(){var e;return((null==(e=s.getMeasurements()[s.options.count-1])?void 0:e.end)||s.options.paddingStart)-s.options.scrollMargin+s.options.paddingEnd},this._scrollToOffset=function(e,t){var n=t.adjustments,o=t.behavior;s.options.scrollToFn(e,{behavior:o,adjustments:n},s)},this.measure=function(){s.itemSizeCache={},s.notify()},this.setOptions(e),this.scrollRect=this.options.initialRect,this.scrollOffset=this.options.initialOffset,this.measurementsCache=this.options.initialMeasurementsCache,this.measurementsCache.forEach((function(e){s.itemSizeCache[e.key]=e.size})),this.calculateRange()},e.defaultKeyExtractor=o,e.defaultRangeExtractor=r,e.elementScroll=function(e,t,n){var o,r,i=t.adjustments,s=void 0===i?0:i,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((r={})[n.options.horizontal?"left":"top"]=a,r.behavior=l,r))},e.measureElement=u,e.memo=n,e.observeElementOffset=l,e.observeElementRect=function(e,t){var n=new ResizeObserver((function(e){var n=e[0];if(n){var o=n.contentRect,r=o.width,i=o.height;t({width:Math.round(r),height:Math.round(i)})}else t({width:0,height:0})}));if(e.scrollElement)return t(e.scrollElement.getBoundingClientRect()),n.observe(e.scrollElement),function(){n.unobserve(e.scrollElement)}},e.observeWindowOffset=a,e.observeWindowRect=function(e,t){var n=function(e,t){var n={height:-1,width:-1};return function(o){(e.options.horizontal?o.width!==n.width:o.height!==n.height)&&t(o),n=o}}(e,t),o=function(){return n({width:e.scrollElement.innerWidth,height:e.scrollElement.innerHeight})};if(e.scrollElement)return o(),e.scrollElement.addEventListener("resize",o,{capture:!1,passive:!0}),function(){e.scrollElement.removeEventListener("resize",o)}},e.windowScroll=function(e,t,n){var o,r,i=t.adjustments,s=void 0===i?0:i,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((r={})[n.options.horizontal?"left":"top"]=a,r.behavior=l,r))},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/virtual-core", | ||
"author": "Tanner Linsley", | ||
"version": "3.0.0-beta.40", | ||
"version": "3.0.0-beta.41", | ||
"description": "Headless UI for virtualizing scrollable elements in TS/JS + Frameworks", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -441,4 +441,2 @@ import { memo } from './utils' | ||
) | ||
} else if (!this.isScrolling) { | ||
this.calculateRange() | ||
} | ||
@@ -494,3 +492,3 @@ } | ||
() => [this.getMeasurements(), this.getSize(), this.scrollOffset], | ||
(measurements, outerSize, scrollOffset) => { | ||
(measurements, outerSize, scrollOffset, [flush = true]: [boolean?]) => { | ||
const range = calculateRange({ | ||
@@ -506,3 +504,5 @@ measurements, | ||
this.range = range | ||
this.notify() | ||
if (flush) { | ||
this.notify() | ||
} | ||
} | ||
@@ -520,3 +520,3 @@ return this.range | ||
this.options.rangeExtractor, | ||
this.range, | ||
this.calculateRange(false), | ||
this.options.overscan, | ||
@@ -523,0 +523,0 @@ this.options.count, |
@@ -5,5 +5,9 @@ export type NoInfer<A extends any> = [A][A extends any ? 0 : never] | ||
export function memo<TDeps extends readonly any[], TResult>( | ||
export function memo< | ||
TDeps extends readonly any[], | ||
TResult, | ||
FArgs extends readonly any[], | ||
>( | ||
getDeps: () => [...TDeps], | ||
fn: (...args: NoInfer<[...TDeps]>) => TResult, | ||
fn: (...args: [...TDeps, FArgs]) => TResult, | ||
opts: { | ||
@@ -14,7 +18,7 @@ key: any | ||
}, | ||
): () => TResult { | ||
) { | ||
let deps: any[] = [] | ||
let result: TResult | undefined | ||
return () => { | ||
return (...fArgs: FArgs): TResult => { | ||
let depTime: number | ||
@@ -38,3 +42,3 @@ if (opts.key && opts.debug?.()) depTime = Date.now() | ||
result = fn(...newDeps) | ||
result = fn(...newDeps, fArgs) | ||
opts?.onChange?.(result) | ||
@@ -41,0 +45,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
389276
3643