A cache
client.cache(config) resolves with a CacheHandle. Its methods are one message each to the worker, with the cache id filled in; cache.id is that id.
Config
const cache = await client.cache({
id: "sensor/42",
interval: MINUTE, // required
alignmentOffset: 0, // timestamps satisfy (t - offset) % interval === 0
fields: { temperature: "f32", errors: "u16" },
gapSplitK: 4, // default 4
segmentSlotCap: 32_768, // default 32 768
version: "2026-10", // opt-in dataset version
warnOnOverlapDiff: false, // default false
});| Field | Default | Meaning |
|---|---|---|
id | required | any non-empty string; tabs that use the same id share the cache |
interval | required | milliseconds between grid points, or a calendar interval |
alignmentOffset | 0 | shifts a fixed grid; normalised into [0, interval); must be absent or 0 with a calendar interval |
fields | required | field name → dtype, at least one (dtypes) |
gapSplitK | 4 | a positive safe integer; segments split where more than K slots in a row are empty |
segmentSlotCap | 32768 | most slots per segment, at most 2^31 − 1 |
version | none | dataset version; a different one reported by a put clears the cache |
finalizedUntil | none | initial watermark; points at or after it are provisional |
warnOnOverlapDiff | false | report writes that change held values (events) |
persist | false | keep the cache in IndexedDB across sessions, in the SharedWorker hosting only (Persistence) |
An invalid config rejects with ConfigError naming the field.
get(range, options?)
const hour = { start: t0, end: t0 + 59 * MINUTE };
const result = await cache.get(hour, { cacheOnly: true });
console.log(result.timestamps.length, result.coverage, result.misses);Resolves with every present point in the range, its coverage and its misses (Coverage and gaps). With a fetcher, it first awaits the fetches for what the cache lacks.
| Option | Default | Meaning |
|---|---|---|
cacheOnly | false | do not fetch; answer from the cache as it is |
snapshot | false | also return a snapshot of the range, from the same state (Snapshots) |
put(batch, options?)
const t0 = Date.UTC(2026, 9, 30, 9, 0);
// Without a range: adds or overwrites these points, claims their own span.
await cache.put({
timestamps: [t0, t0 + MINUTE, t0 + 2 * MINUTE],
fields: { temperature: [21.5, 21.7, Number.NaN], errors: [0, 0, 1] },
});
// With a range: the answer for 09:00–09:59, so the slots without a point
// become confirmed gaps, and points the batch omits are removed.
const { warnings } = await cache.put(
{
timestamps: new Float64Array([t0, t0 + MINUTE]),
fields: {
temperature: new Float32Array([21.5, 21.8]),
errors: new Uint16Array([0, 0]),
},
},
{ range: { start: t0, end: t0 + 59 * MINUTE } },
);
console.log(warnings); // non-empty only with warnOnOverlapDiff
// The typed arrays above were transferred to the worker: they are now
// detached. Pass copies if you still need them.Writes points. batch.timestamps is a number[] or Float64Array, aligned, strictly ascending; batch.fields has one array per field; batch.meta may carry version and finalizedUntil, as a fetch response does. options.range claims a range (Claiming a range). Resolves with { warnings }, empty unless warnOnOverlapDiff is on.
The batch's typed arrays are transferred to the worker and are detached afterwards; pass copies if you still need them. number[] arrays are copied.
An invalid batch rejects as a whole with PutError, before anything changes:
try {
await cache.put({
timestamps: [t0 + 30_000],
fields: { temperature: [20], errors: [0] },
});
} catch (error) {
if (error instanceof PutError) {
console.log(error.code, error.offenderIndex, error.expected);
// "misaligned" 0 "t ≡ 0 (mod 60000)"
}
}invalidate(range)
// Forget what is known about the range; the next get fetches it again.
// The points stay readable until then.
await cache.invalidate(hour);clear()
Empties this cache, for every tab sharing the worker; see Invalidation and clearing.
setFinalizedUntil(t)
// Points at or after 10:00 are provisional: stored, never covered.
await cache.setFinalizedUntil(t0 + 60 * MINUTE);Sets the watermark exactly, either way (Versions and watermarks).
hydrate(snapshot)
Applies a snapshot where the cache knows nothing; see Snapshots.
on(event, fn)
client.on filtered to this cache, for cacheCleared, mergeWarning and persistStopped. Returns the unsubscribe.
const unsubscribe = cache.on("cacheCleared", ({ reason }) => {
console.log("cleared", reason); // "manual", "clear-all" or "version-mismatch"
});
await cache.clear(); // with a SharedWorker, every tab's view of this cache
unsubscribe();