@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
99 lines (98 loc) • 3.75 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/timeoutManager.ts
var timeoutManager_exports = {};
__export(timeoutManager_exports, {
TimeoutManager: () => TimeoutManager,
defaultTimeoutProvider: () => defaultTimeoutProvider,
systemSetTimeoutZero: () => systemSetTimeoutZero,
timeoutManager: () => timeoutManager
});
module.exports = __toCommonJS(timeoutManager_exports);
var defaultTimeoutProvider = {
// We need the wrapper function syntax below instead of direct references to
// global setTimeout etc.
//
// BAD: `setTimeout: setTimeout`
// GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)`
//
// If we use direct references here, then anything that wants to spy on or
// replace the global setTimeout (like tests) won't work since we'll already
// have a hard reference to the original implementation at the time when this
// file was imported.
setTimeout: (callback, delay) => setTimeout(callback, delay),
clearTimeout: (timeoutId) => clearTimeout(timeoutId),
setInterval: (callback, delay) => setInterval(callback, delay),
clearInterval: (intervalId) => clearInterval(intervalId)
};
var TimeoutManager = class {
// We cannot have TimeoutManager<T> as we must instantiate it with a concrete
// type at app boot; and if we leave that type, then any new timer provider
// would need to support the default provider's concrete timer ID, which is
// infeasible across environments.
//
// We settle for type safety for the TimeoutProvider type, and accept that
// this class is unsafe internally to allow for extension.
#provider = defaultTimeoutProvider;
#providerCalled = false;
setTimeoutProvider(provider) {
if (process.env.NODE_ENV !== "production") {
if (this.#providerCalled && provider !== this.#provider) {
console.error(
`[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`,
{ previous: this.#provider, provider }
);
}
}
this.#provider = provider;
if (process.env.NODE_ENV !== "production") {
this.#providerCalled = false;
}
}
setTimeout(callback, delay) {
if (process.env.NODE_ENV !== "production") {
this.#providerCalled = true;
}
return this.#provider.setTimeout(callback, delay);
}
clearTimeout(timeoutId) {
this.#provider.clearTimeout(timeoutId);
}
setInterval(callback, delay) {
if (process.env.NODE_ENV !== "production") {
this.#providerCalled = true;
}
return this.#provider.setInterval(callback, delay);
}
clearInterval(intervalId) {
this.#provider.clearInterval(intervalId);
}
};
var timeoutManager = new TimeoutManager();
function systemSetTimeoutZero(callback) {
setTimeout(callback, 0);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TimeoutManager,
defaultTimeoutProvider,
systemSetTimeoutZero,
timeoutManager
});
//# sourceMappingURL=timeoutManager.cjs.map