UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

39 lines (38 loc) • 1.22 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); function throttleStrategy(options) { const leading = options.leading === true || options.leading === void 0 && options.trailing !== true; const trailing = options.trailing !== false; let nextAllowedAt = Number.NEGATIVE_INFINITY; let trailingTimeout; let pendingCallback; return { _type: `throttle`, options, execute: (fn) => { const now = Date.now(); if (leading && trailingTimeout === void 0 && now >= nextAllowedAt) { nextAllowedAt = now + options.wait; fn(); return; } if (!trailing) return false; pendingCallback = fn; if (trailingTimeout === void 0) { const delay = leading ? Math.max(0, nextAllowedAt - now) : options.wait; trailingTimeout = setTimeout(() => { trailingTimeout = void 0; nextAllowedAt = Date.now() + options.wait; const callback = pendingCallback; pendingCallback = void 0; callback?.(); }, delay); } return; }, cleanup: () => { } }; } exports.throttleStrategy = throttleStrategy; //# sourceMappingURL=throttleStrategy.cjs.map