mirror of
https://github.com/hanzoai/agency.git
synced 2026-11-02 19:13:28 +00:00
70 lines
2.5 KiB
Plaintext
70 lines
2.5 KiB
Plaintext
// src/timeoutManager.ts
|
|
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 ReturnType<typeof setTimeout>, which is infeasible.
|
|
//
|
|
// 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);
|
|
}
|
|
export {
|
|
TimeoutManager,
|
|
defaultTimeoutProvider,
|
|
systemSetTimeoutZero,
|
|
timeoutManager
|
|
};
|
|
//# sourceMappingURL=timeoutManager.js.map |