studioCodebase

tcmts.studio.studioCodebase · #nn32367a · term · runs on the server

uses tcmts.share.servedCodebase, which uses process

The codebase the page being made reads: the one served, on its own branch, or, if the browser reads another branch (BRANCH_COOKIE) that's still there, the same codebase opened apart reading that one (--branch: nothing changes it), kept for the pages after.

import { openApart } from "#codebase/tcmts/cli/codebase";
import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { cookie } from "#codebase/tcmts/server";
import { servedCodebase, shareCache } from "#codebase/tcmts/share";
import type { ShareCache } from "#codebase/tcmts/share";
import { BRANCH_COOKIE, readingBranches } from "#codebase/tcmts/studio";

export function studioCodebase(): { cb: Codebase; cache: ShareCache } {
  const own = servedCodebase();
  const b = cookie(BRANCH_COOKIE);
  if (!b || b === own.cb.store.branch().name || !own.cb.store.hasBranch(b)) return own;
  let r = readingBranches.get(b);
  if (!r) {
    const cb = openApart(own.cb.lay.root);
    cb.store.readOnly(b);
    readingBranches.set(b, (r = { cb, cache: shareCache() }));
  }
  return r;
}

uses

used by