Skip to content

Services Track

Eugene Lazutkin edited this page Jul 1, 2026 · 2 revisions

Track

io.track deduplicates in-flight requests at the run level: concurrent identical GETs share a single decoded envelope instead of each hitting the network and reparsing the body.

import io from 'double-meh';

const [a, b] = await Promise.all([
  io.get('https://example.com/dup'),
  io.get('https://example.com/dup')
]);
// one network call; a and b are the same decoded result

Decoded-envelope dedup

The first caller of a key marks it in-flight and runs the request; later callers awaiting the same key receive the same resolved envelope — decoded once, not cloned and reparsed. Dedup is keyed by method + URL, so different keys never share.

const [a, b] = await Promise.all([
  io.full.get('https://example.com/shared'),
  io.full.get('https://example.com/shared')
]);
a === b; // true — one envelope object, decoded at the run level

By default only GET is tracked. Set track: true to force it, track: false to skip it. Requests with a custom transport are not tracked.

Skips streaming

stream: true requests are never deduped — each streaming caller gets its own response and body.

await Promise.all([
  io.full.get('https://example.com/s', null, {stream: true}),
  io.full.get('https://example.com/s', null, {stream: true})
]); // two independent responses

wait: true

wait: true registers interest in a key without firing a request. The returned promise stays pending until a later real request (or io.adopt) for the same key lands, then resolves every waiter with that result.

const waiter = io.get({url: 'https://example.com/w', wait: true});
// nothing has been sent yet
const real = io.get('https://example.com/w');       // the real request fires
const [w, r] = await Promise.all([waiter, real]);   // both resolve to the landed data

fly, flyByKey, isFlying

These expose the in-flight registry directly:

  • io.track.fly(target) — get (creating if needed) the deferred for a target.
  • io.track.flyByKey(key) — same, addressed by a raw key from io.makeKey.
  • io.track.isFlying(target) — the deferred if the key is currently in flight, else undefined.
io.track.fly('https://example.com/soon');       // register interest by target
const pending = io.track.isFlying('https://example.com/soon');
if (pending) await pending.promise;

Relationship to io.adopt

io.adopt(target, source) fulfills a key from an externally issued response (a Promise<Response> or Response) instead of the network — it marks the key flying so a later real request does not also fire, resolves all waiters, and (when the cache is active) saves the GET. This is the basis of the code-forward handoff: a page can pre-register in-flight keys via fly and later feed their responses via adopt, so client hydration reuses server-issued data.

io.adopt('https://example.com/me', json({from: 'prelude'}));
const data = await io.get('https://example.com/me'); // no network — uses the adopted response

Attaching

io.track.attach() / io.track.detach() toggle io.track.active and return io.

io.track.detach(); // disable dedup
io.track.attach(); // re-enable

See also

Clone this wiki locally