Skip to content

Immutability and lifecycle ​

refine grows a node; finalizeDescriptor seals the graph.

package @smitejs/core

Smite's IR is immutable by construction. Descriptor data is frozen at creation, and the graph is progressively sealed until, at runtime, the whole reachable subtree is deeply frozen. Two functions drive this lifecycle.

refine — update data, keep identity ​

refine(descriptor, patch) shallow-merges patch into the node's data and replaces it with a fresh frozen snapshot. The node keeps its __kind, __key, and object identity, so references held by edges and the registry stay valid. Use it to attach metadata late, such as http.route attaching its req validation config.

ts
const route = defineDescriptor("http.route", "GET /users/:id", {
  path: "/users/:id",
  method: "GET",
});
refine(route, { summary: "list users" });
ts
"list users"

finalizeDescriptor — seal the graph ​

finalizeDescriptor(root) deep-freezes the root, its data, its child index, and every reachable node — recursively and cycle-safely. After this the graph is a read-only value: any further refine throws a TypeError.

ts
const app = createApp("api");
const route = defineDescriptor("http.route", "GET /users/:id", {
  path: "/users/:id",
  method: "GET",
});
relate(app, "http.route", route);
finalizeDescriptor(app);
ts
true
true

Lifecycle summary ​

  • Build phase: defineDescriptor → relate → refine (mutable, registry-backed).
  • Runtime phase: finalizeDescriptor (frozen, registry-free).

Executors call finalizeDescriptor for you — serve(app) does it before the first dispatch, so handlers can rely on the graph being stable.

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/internals/immutability