Globals

Typed process-wide state, stored on globalThis under package-namespaced symbols.

Two things in the framework must be singletons regardless of how many times the module is evaluated: the AppsRegistry that getNearestApp resolves against, and the ParsersRegistry that every route reads while compiling. Module-level variables cannot guarantee that — a duplicated dependency, a re-import under a different resolution, or a test runner reloading modules each produces a second copy, and two apps registries means routes registering onto an app that is never served.

The keys are Symbol.for("@ozanarslan/corpus:<key>"), so they are shared across every copy of the package in the process and cannot collide with another library's globals.

Contents
  1. GlobalRegistry
  2. Globals
  3. GLOBAL_SYMBOLS

GlobalRegistry

interface

interface GlobalRegistry

What lives in the global store, and the type of each entry. Adding a key here requires a matching entry in GLOBAL_SYMBOLS, which the satisfies clause enforces.

NameDescription
initializedWhether one-time framework setup has run.
appsThe shared AppsRegistry — every App constructed in this process.
parsersThe shared ParsersRegistry — the parsers routes resolve at compile time.

Globals

class

class Globals

Typed accessor for the global store.

Keys are constrained to GlobalRegistry, so every read and write is checked against the type declared for that entry.

Globals.getSymbol()

static getSymbol<K extends keyof GlobalRegistry>(key: K):(typeof GLOBAL_SYMBOLS)[K]

Returns the symbol an entry is stored under.

Useful for inspecting or clearing global state directly — in a test that needs a clean process, for instance.

Parameters

Returns — Its symbol from GLOBAL_SYMBOLS.

Globals.create()

static create<K extends keyof GlobalRegistry>(key: K, init:()=>GlobalRegistry[K], ): GlobalRegistry[K]

Reads an entry, creating it on first access.

The initialiser runs only when the entry is absent, which is what makes this safe to call from anywhere: the first caller creates the value and every later one gets that same instance. This is how getOrInitAppsRegistry and getOrInitParsersRegistry work.

Parameters

Returns — The stored value.

Globals.get()

static get<K extends keyof GlobalRegistry>(key: K): GlobalRegistry[K]

Reads an entry that is expected to exist.

Parameters

Returns — The stored value.

Throws — Error when the entry has not been created. Use Globals.create when the caller may be the first.

Globals.set()

static set<K extends keyof GlobalRegistry>(key: K, value: GlobalRegistry[K]): void

Writes an entry, replacing any existing value.

Parameters

Globals.has()

static has<K extends keyof GlobalRegistry>(key: K): boolean

Reports whether an entry has been created, without creating it or throwing.

Parameters

Returns — true when the entry exists.

Globals.delete()

static delete<K extends keyof GlobalRegistry>(key: K): void

Removes an entry, so the next Globals.create rebuilds it from scratch. Mainly useful for resetting state between tests.

Parameters

GLOBAL_SYMBOLS

const

const GLOBAL_SYMBOLS;

The symbol for each GlobalRegistry entry.

Registered through Symbol.for, so every copy of the package in the process resolves to the same symbol and therefore to the same stored value.