@z-cloud/virtual-vanilla
Version:
提供跨平台(浏览器,小程序)的虚拟列表公共基类
53 lines (52 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const canUseDom = !!(typeof window !== "undefined" && window.document && window.document.createElement);
function isEqual(next, previous) {
if (next === previous) {
return true;
}
if (Number.isNaN(next) && Number.isNaN(previous)) {
return true;
}
return false;
}
function compare(next, previous) {
if (next.length !== previous.length) {
return false;
}
for (let i = 0; i < next.length; i++) {
if (!isEqual(next[i], previous[i])) {
return false;
}
}
return true;
}
function memoFnResult(memoFn, isEqual2 = compare) {
let cache = null;
function memoized(...args) {
if (cache && cache.prevThis === this && isEqual2(args, cache.prevArgs)) {
return cache.prevResult;
}
const result = memoFn.apply(this, args);
cache = {
prevResult: result,
prevArgs: args,
prevThis: this
};
return result;
}
memoized.clear = function clear() {
cache = null;
};
return memoized;
}
const debounce = (fn, wait) => {
let timeoutId;
return function(targetWindow, ...args) {
targetWindow.clearTimeout(timeoutId);
timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), wait);
};
};
exports.canUseDom = canUseDom;
exports.debounce = debounce;
exports.memoFnResult = memoFnResult;