Skip to content

Concepts: code forward

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

code‐forward

Code-forward lets a page fire requests and apply config before double-meh (or your app) has finished loading, then adopts the results seamlessly — the network never waits on script-download → parse → execute. It is driven by a single global, globalThis.__doubleMeh.

import io from 'double-meh'; // installs code-forward automatically

Pre-load: the command queue

Before the library loads, __doubleMeh is a plain command queue. An inline <head> script pushes to three arrays:

<script>
  window.__doubleMeh = window.__doubleMeh || {};
  var dm = window.__doubleMeh;

  // 1. Fire the request NOW and hand the promise over to be adopted:
  (dm.arrived = dm.arrived || []).push(['/api/me', fetch('/api/me')]);

  // 2. Register interest in an in-flight key (dedupe a request made elsewhere):
  (dm.inFlight = dm.inFlight || []).push('/api/config');

  // 3. Queue setup to run once io is live:
  (dm.setup = dm.setup || []).push(function (io) {
    io.cache.attach();
  });
</script>

Post-load: the drain

When double-meh loads it drains the queue — setup callbacks run with io, inFlight keys are registered on track, and each arrived [target, response] pair is adopted (into track for in-flight sharing and into cache for durability). Your normal call then resolves from the early fetch instead of hitting the network again:

import io from 'double-meh';

const me = await io.get('/api/me'); // resolves from the <head> fetch — no second request

After draining, __doubleMeh becomes the live io handle (Object.assign(dm, io)), and keeps dm.use(fn), dm.fly(target), and dm.arrived(target, response) live so late deliveries still land.

The ready event

Installation fires a one-time event: io.on('ready', () => …).

See also

Clone this wiki locally