@time-provider/addon-compat
v0.2.1
Published
Time-Provider : Compatibility addon ~ Your single time interface for all your JavaScript / TypeScript projects.
Maintainers
Readme
 entry point and a deterministic one, so each import pulls in only the code it needs:
@time-provider/addon-compat- for a system (real time) Time-Provider created via@time-provider/core. Timers run on real native timers.@time-provider/addon-compat/deterministic- for a deterministic Time-Provider (fixed/manual/sequential) created via@time-provider/core/deterministic. Timers run against that runtime's own simulated clock.
Usage
import { createTimeProvider } from "@time-provider/core";
import { createTimeProvider as createDeterministicTimeProvider } from "@time-provider/core/deterministic";
import { plugin } from "@time-provider/plugin-native";
import { plugin as deterministicPlugin } from "@time-provider/plugin-native/deterministic";
import { addon } from "@time-provider/addon-compat";
import { addon as deterministicAddon } from "@time-provider/addon-compat/deterministic";
// System: runs on real native timers, in the runtime's local timezone.
const timeProvider = createTimeProvider.for(plugin).use(addon).create();
const handle = timeProvider.compat.setTimeout(() => {
console.info("Native setTimeout call style");
}, 500);
// ...
timeProvider.compat.clearTimeout(handle);
//same calls for setInterval/clearInterval...
timeProvider.compat.queueMicrotask(() => {
console.info("Native queueMicrotask call style");
});
// The performance members are there too, with their native signatures.
timeProvider.compat.mark("request-start");
console.info(timeProvider.compat.now(), timeProvider.compat.timeOrigin);
// Deterministic: runs against the runtime's own simulated clock.
const manual = createDeterministicTimeProvider
.for(deterministicPlugin)
.use(deterministicAddon)
.asManual()
.withInitialTime("2024-01-01T00:00:00.000Z")
.create();
const handle = manual.compat.setTimeout(() => {
console.info("Native setTimeout call style");
}, 500);
// ...
manual.compat.clearTimeout(handle);
//same calls for setInterval/clearInterval...Members other addons add
Composing @time-provider/addon-animation-frame or @time-provider/addon-idle after this addon adds their native-shaped aliases to the same .compat facade: requestAnimationFrame/cancelAnimationFrame and requestIdleCallback/cancelIdleCallback. Each cancel* takes the handle its request* returned. Compose this addon first - the facade has to exist by the time they are applied, and composed the other way round the aliases are simply absent.
License
MIT
