Skip to content

Code tour ​

This page reads the source of packages/tscache/src/ in the order a request travels: from the package entries, through the client in the page and the RPC layer, into the worker, where the orchestrator plans fetches in front of the engine, down to the segments and the coverage index, out to IndexedDB, and finally to the Vite plugin that serves the fetcher module at build time.

Each stop lists the symbols to open, as path#symbol (A.b is the member b of A), says what to notice in them, and names a scenario of the demo hub (bun run demo) where the code is at work. bun run docs:check fails when an anchor on this page stops resolving.

1. The entries ​

Read:

What to notice:

  • Four of the five entries only re-export. entries/worker.ts is the one that runs code when it loads: it builds the worker's server and attaches it (stop 4).
  • . re-exports createClient, the errors, encodeSnapshot and decodeSnapshot, DTYPES and types; the CacheHandle and TscacheClient are exported as types only. ./engine exports the Engine class itself, for server code and tests.
  • ./fetcher re-exports from errors.ts alone, plus type-only exports of the fetcher types.
  • Package map shows what each entry's imports reach.

In the demo hub: every example page imports the worker entry as a URL (@anishbenji/tscache/worker?worker&url) and passes it to createClient as workerUrl; for instance Two tabs, one SharedWorker (examples/react-lwcharts/src/client.ts).

2. The client ​

Read:

What to notice:

  • createClient validates its options before it creates anything: workerUrl is required unless the chain starts at 'in-process', and handshakeTimeoutMs (default 5000) must be a finite positive number.
  • connect walks CHAIN from options.mode. Only a HostingError steps down to the next hosting; any other error, a ProtocolMismatchError included, rejects createClient. Each step is kept and emitted as modeFallback on the next macrotask, once the caller has had a chance to subscribe.
  • handshake arms its timer only until hello arrives; loading the fetcher module, which happens before init-ok, has no timeout. A worker error event before then aborts the handshake.
  • openShared creates the SharedWorker with type: 'module', extendedLifetime: true and the generation key as its name, and on hello queues on the worker's lifetime lock. A grant before init-ok fails the hosting; a grant afterwards aborts the connected port client, which Client.#lose turns into one workerLost event. openDedicated learns of its worker's failure from the worker's error event instead.
  • openInProcess loads client/in-process.ts with import(), so a page served by a worker never downloads the engine; a chunk that fails to load is a HostingError.
  • CacheHandle sends one request per method with its cache id filled in (on only subscribes, and sends nothing). put transfers the batch's buffers, so the caller's arrays are detached afterwards; hydrate sends its snapshot without a transfer list, so the snapshot stays usable.
  • Client disposes itself on the page's pagehide, unless the page enters the back/forward cache (persisted), because the worker would otherwise never learn that the tab went.
  • ClientEmitter.receive maps wire events onto ClientEvents and ignores names it does not know.

In the demo hub: Two tabs, one SharedWorker: crash the tab that opened first (it hosts the SharedWorker): the other tabs get workerLost and build a new client. The server-rendered entries (Server-rendered candles, Server-rendered sensor) create their server-side client with mode: 'in-process'.

3. The RPC layer ​

Read:

What to notice:

  • The server speaks first. RpcServer.attach gives each port a client id (c1, c2, …) and posts hello with the protocol, the library version and, in a SharedWorker, the lifetime lock's name. The client answers init, which carries the fetcher config.
  • RpcServer.#init refuses another protocol with init-err, then loads the fetcher module before it answers init-ok, so a module that fails to load rejects createClient at startup.
  • Every request is { t: 'req', id, op, params }, with id counted per port client. The server names a request clientId:id, the request id that request-scoped events carry.
  • RpcServer.#handlers sends get, invalidate and updateAuth to the orchestrator and put, clear, setFinalizedUntil and hydrate to the engine. When the hosting has a persistence, every op that names a cache runs through Persistence.durably, clearAll goes to Persistence.clearAll, and cache of a cache with persist: true answers only once Persistence.open has settled.
  • RpcServer.#request transfers a get result's buffers (transferablesOf) instead of copying them, and answers a dispose before it drops the port.
  • Errors cross the port as plain WireError objects. toWireError names a library error by its class, found with instanceof, because a minifier may rename the constructor (other errors keep their name); fromWireError rebuilds the class and turns an unknown name into a TscacheError.
  • Events fan out to every ready port. A port the server drops on its own initiative gets a bye first, because a browser MessagePort fires no close event; when the last connection goes, the server flushes persistence.
  • holdLifetimeLock takes an exclusive Web Lock that is never released while the worker runs, and watchLifetimeLock queues on it in shared mode: the grant means the holder is gone.

In the demo hub: Two tabs, one SharedWorker, or any reference client: every tab is one port of the same server, so a second tab draws from what the first one fetched.

4. The worker host ​

Read:

What to notice:

  • The entry tells the two worker kinds apart by their globals: both have importScripts, and only a SharedWorker's global has onconnect.
  • In a dedicated worker, the global scope itself is the port: one server is attached to it at load.
  • In a SharedWorker, the connect listener goes on at load, so no connection is missed, but ports wait until the worker holds its lifetime lock; every hello can then name the lock. One server serves every port.
  • createServer builds one Engine and one RpcServer around it, with the persistence the entry asks for: the SharedWorker passes an IndexedDB opener and gets a HostedPersistence, the dedicated worker passes clearOnly and gets a ClearOnlyPersistence, and where lazyIdbStore finds no IndexedDB there is no opener and no persistence.
  • connectInProcess builds the same pair, an Engine and an RpcServer, in the page, and connects to it over a MessageChannel: the in-process hosting runs the same protocol as the workers.

In the demo hub: Two tabs, one SharedWorker: every tab you open reads mode shared, and a new tab draws three days at once and fetches only the forming bar.

5. The orchestrator ​

Read:

What to notice:

  • Without a fetcher, or with cacheOnly: true, Orchestrator.get is one Engine.get.
  • Otherwise a round, Orchestrator.#round, runs in one synchronous step from reading the coverage to listing its new fetches, so two rounds never interleave, whichever tabs they serve. It reads the coverage one slot beyond the request at each end, so a miss at the edge can extend into it.
  • planRound takes the request's slots that are neither covered nor blocked, joins the fetches in flight that meet them, and fetches only the rest. coalesce merges adjacent misses and extends a miss by one slot into coverage next to it.
  • A fetch in flight can be joined only while its cache generation (cleared count) and auth generation are current and no invalidate has fenced it.
  • Orchestrator.#run passes the fetcher copies of the range and a clone of the auth context. Once the fetcher settles, the outcome is decided in one step: a response that started before a clear of the cache is dropped, any other is applied by Orchestrator.#apply as a put with the fetched range as its authority, and the fetch leaves the in-flight list in that same step.
  • Orchestrator.#rounds runs a second round only when the cache was cleared while the first waited, minus the ranges whose fetch failed or came back auth-pending; then it reads the engine once. annotate relabels the misses those fetches leave as 'fetch-failed' or 'auth-pending'.
  • Orchestrator.#refused recognises an expired credential with isAuthInvalidError, and only a failure under the current auth generation flips AuthState, which broadcasts authInvalid once per flip. While auth is invalid, or while updateAuth is swapping credentials, a round fetches and joins nothing and its misses come back 'auth-pending' at once.
  • Orchestrator.updateAuth applies updates one at a time: the fetcher's updateAuth hook hears a clone of the new context, then the context and the auth generation switch together.
  • Orchestrator.load imports the fetcher module once per server, so once per SharedWorker, once an import has succeeded; a failed import leaves the slot free for the next connection to retry. A connection naming another module gets a ConfigError; one bringing a context that is not the identical value goes through updateAuth, except a context that sameMaterial finds equal to the context the last refusal named (AuthState.wasRefused, checked when its turn comes and again after the hook), which changes nothing, whatever the auth state.

In the demo hub: Candles with weekend gaps: load an earlier fortnight; the page asks for the whole range and only what the cache does not cover is fetched: the older fortnight and the provisional slot at the right edge. Two tabs, one SharedWorker: expire the access token; one tab refreshes it, the others carry on, and the backend counts one refresh.

6. The engine ​

Read:

What to notice:

  • Engine.cache is get-or-create. A second config for the same id must agree on every field in STRUCTURAL, or it throws ConfigError; its version and finalizedUntil are ignored.
  • The engine is synchronous and has no timers: every method runs to completion inside the call, and Engine.clearAll empties every cache but keeps the caches and their configs.
  • CacheState.put validates the whole batch before it changes anything (validateBatch: alignment, strictly ascending timestamps, field lengths, the put's range), copying the values into fresh arrays of the schema's dtypes.
  • CacheState.#putValid then runs in a fixed order: a differing version clears the cache (CacheState.#applyVersion); the points are written; the watermark advances; the put's claim, minus the provisional slots, is recorded as coverage; and only then is the version clear reported.
  • CacheState.#setWatermark withdraws coverage from the new watermark slot on whenever the provisional region grows, so those points are fetched again.
  • CacheState.hydrate writes a snapshot only into the parts of its coverage this cache neither covers nor holds as provisional, and refuses a snapshot of another dataset version.
  • read returns every present point in the request with the covered sub-ranges and the uncovered ones as 'uncached' misses; this is where slots become milliseconds again.
  • Data model draws the state these calls change.

In the demo hub: Live server metrics and sparse events: the log shows one ten-minute fetch, then short fetches of the provisional tail only; Restate history makes the backend answer with a new dataset version, and the cache clears itself and fetches the window again.

7. Segments and coverage ​

Read:

What to notice:

  • grid.ts is the only module that converts between milliseconds and slots; everything below it does range arithmetic on integer slots. A read or an invalidation snaps outward (snapOut), a put's authority inward (snapIn).
  • SegmentStore.put first removes the stored points inside the put's authority (SegmentStore.#carve), then writes the batch one write run at a time, in ascending slot order. SegmentStore.#together cuts the batch into write runs (N70): consecutive batch points on the same page with at most gapSplitK slots between them, counted on the batch alone. The layout rule is the file comment at the top of merge.ts: two present points share a segment when they lie on the same page and at most gapSplitK slots between them hold no point.
  • A DenseSegment stores no timestamps: a slot's time follows from its position. It keeps one typed array per field and one presence bit per slot, so a point has all of its fields or none.
  • DenseSegment.mergeFrom with an authority leaves exactly the given points inside it; without one it only adds or overwrites.
  • CoverageIndex.add merges overlapping and adjacent ranges; CoverageIndex.subtract may split one range in two; CoverageIndex.gaps gives the uncovered sub-ranges that become a read's misses.

In the demo hub: Greenhouse sensor with dropouts: the three charts break at exactly the same places, because presence is per point. Live server metrics and sparse events, events page: zooming inside the loaded week asks the backend for nothing, because empty slots are covered, not missing.

8. Persistence ​

Read:

What to notice:

  • persist/ imports the engine; the engine imports nothing from persist/. The persister reaches a cache's state through internal functions that no package entry exports, and installs restore's implementation with installRestore when it is constructed.
  • HostedPersistence loads persist/lazy.ts, the persister and the IndexedDB adapter, with import() at the first persisted cache or the first clearAll. Meanwhile the opens, the ops on a cache whose open waits, and, once one op waits, every later op and clearAll wait and reach the persister in arrival order; ops on other caches run at once. A chunk that fails to load, or does not load in time, turns persistence off for the worker's life.
  • Persister.open loads a cache's records under one deadline (timeoutMs, default 5000 ms). Writes are behind, except removals, which request a flush at once: a flush is due debounceMs (1000) after the latest change and no later than maxDelayMs (5000) after the first, and is planned from memory alone (Persister.#plan) without reading the disk.
  • durably holds an op's answer when the op, or another op meanwhile, recorded a removal: the op answers once the removal is durable or, after the cache's persistence has stopped, once the persister releases it (architecture §4.10.1; What can outlive a removal says what the answer guarantees after a stop).
  • A QuotaExceededError stops the cache's persistence at once rather than retrying (Persister.#rejected); the stop reaches every tab as persistStopped.
  • openIdbStore opens one database per origin, tscache at version 1, with a caches and a pages object store; a PageRecord holds a snapshot of one page's covered ranges.
  • ClearOnlyPersistence, in the dedicated-worker and in-process hostings where IndexedDB exists, persists nothing; its clearAll empties the database too.

In the demo hub: no scenario persists a cache. Every example app and reference client builds its worker with worker: { format: 'es' } (for instance examples/vanilla-uplot/vite.config.ts), so this chunk stays a file of its own, which a worker loads only at its first persisted cache or its first clearAll. Persistence shows how to turn it on.

9. The Vite plugin ​

Read:

What to notice:

  • The worker loads the fetcher with import() and needs its default export. Vite would bundle a ?worker&url import without its exports, so import url from "./fetcher.ts?fetcher" resolves instead to a module whose default export is the fetcher's URL.
  • In a build, the plugin's load hook emits the fetcher as a chunk of its own with its signature preserved (preserveSignature: 'strict'). In dev it serves the file from source: the browser gets an @fs URL under the base (devUrl), server code the file's absolute path. In a server build the URL is a file URL built at run time (serverFileUrl).
  • generateBundle fails the build when the fetcher's chunk, or any chunk it reaches, contains Vite's module-preload helper, which needs document and window, which a worker does not have.
  • buildStart warns once when a browser build makes workers as IIFE, which cannot split off the worker's persistence chunk; the plugin never changes the format.
  • The plugin imports nothing from Vite, not even its types: it declares the shapes of the context it uses.

In the demo hub: every example app and reference client lists fetcherUrl() in its vite.config.ts and imports its fetcher with ?fetcher; for instance Two tabs, one SharedWorker (examples/react-lwcharts/src/client.ts).