Skip to content

What tscache is ​

tscache caches timestamp-aligned time series in the browser: candles, metrics, sensor readings, event counts. It is built for a chart that scrolls through history and follows a live tail, in several tabs at once.

  • One cache for every tab. The cache runs in a SharedWorker, so every same-origin tab reads the same data. Where there is no SharedWorker (Samsung Internet, Chrome for Android before 148), it falls back to a dedicated Worker, then to the page itself; those give each tab a cache of its own, shared with no other tab.
  • The worker fetches. You give it a fetcher module, and a get fetches only the ranges its cache neither holds nor already has in flight, widened by one interval where a miss meets cached data so the series joins up (that edge point is fetched again). Under the SharedWorker, concurrent reads of a range from several tabs wait on one fetch, and finalized history one tab fetched is not fetched again by another. Points at or after the finalizedUntil watermark are provisional: every read that includes them fetches them again.
  • Known gaps are first-class. The cache records what it knows (coverage) separately from the points that exist. A weekend without candles, or an hour a sensor was offline, is a confirmed gap: it is never fetched again, and it is not confused with "not loaded yet".
  • Columns, not objects. A read returns one typed array per field, which chart libraries take as they are, and moves them from the worker to the page without copying.
  • Honest freshness. A watermark marks the provisional tail that is always fetched again; a dataset version clears the cache when the backend says history changed; invalidate forgets a range on your own clock. There is no TTL.
Two tabs of one origin. Tab 1 opens with an empty cache, and its SharedWorker fetches the history in one fetch; when its live feed opens, one more fetch reads the newest bars. Tab 2 opens on the same SharedWorker and draws the same chart at once from the shared cache; the backend serves it only the newest bars, the one forming now and the one that just closed. Recorded headless with Playwright from the demo hub's vanilla reference client; the counters above the page are its mock backend's. The two tabs are two frames of one page, which share the SharedWorker as two tabs do.

A first look ​

ts
const MINUTE = 60_000;
const candles = await client.cache({
  id: "candles/ACME",
  interval: MINUTE, // required: there is no default
  fields: { open: "f64", high: "f64", low: "f64", close: "f64", volume: "f64" },
});
ts
// The bar forming now: reading to Date.now() would also ask for the next one.
const now = Math.floor(Date.now() / MINUTE) * MINUTE;
const result = await candles.get({ start: now - 6 * 60 * MINUTE, end: now });

// Present points only: equal-length columns, no holes.
const { timestamps, fields } = result;
console.log(timestamps.length, fields.close?.at(-1));

// What is known, and what is not.
for (const range of result.coverage) {
  console.log("authoritative", range.start, range.end);
}
for (const miss of result.misses) {
  console.log("not loaded", miss.range, miss.reason, miss.error?.message);
}

Getting started sets up the worker and the fetcher; Coverage and gaps explains the result.

What it leaves out ​

Version 1 has no aggregation or resampling (a series at one minute and at one hour are two caches), and no irregular timestamps. Change events (cacheChanged) arrived in 0.2.0 (Change events). Memory has no default limit; from 0.3.0 (in preparation) an opt-in memory budget evicts the least recently used pages above it, and memoryStats() reports what the caches hold (Memory). The roadmap lists what is planned.

Known limits ​

The limits of 0.2.0 that an application has to design around:

  • Memory is bounded only by a budget you set. In 0.2.0 nothing bounds or reports it: a cache keeps everything it was asked for until its worker ends, and a SharedWorker runs while any tab of the origin uses it. A year of one-minute OHLCV bars is about 21 MB per series. From 0.3.0 (in preparation) memoryBudget bounds it by evicting the least recently used pages, and memoryStats() reports it; there is still no default budget. Memory says how to keep it in check.
  • A deploy can leave tabs on two versions. In 0.2.0, with a hashed worker URL, a deploy that changes the worker gives old and new tabs separate SharedWorkers that share no memory, silently, and both write the origin's IndexedDB database; a deploy that changes only the fetcher is refused with ConfigError in every new tab until the old tabs close. From 0.3.0 (in preparation) each such deploy has a worker of its own, the older worker stops persisting and its tabs hear superseded, and a logout's clearAll reaches every worker of 0.3.0 or later, within the logout limits. Stale scripts after a deploy has the details.
  • Change events reach other tabs only through a SharedWorker. Since 0.2.0 cacheChanged tells a view where a read's result may have changed (Change events). With a dedicated worker or in-process, each tab has a cache of its own and hears only its own changes: another tab's data must still be sent to it (Redrawing when the cache changes).

Status ​

tscache is at 0.x and follows semantic versioning within it: a minor release may break the API, a patch release never does. Pin a minor range (~0.2.0) to take fixes without breaking changes. The design is recorded in the architecture document; Architecture for contributors summarises it.