Skip to main content

Function: createRegistry()

createRegistry(blocks): Registry

Defined in: packages/core/src/createRegistry.ts:49

Turns the blocks an app ships into the lookup a renderer resolves nodes through: one name to one block, with every slot allow entry checked against the set. Hold the result for the life of the process — it reads a snapshot of the array and never re-reads it.

Parameters

blocks

readonly Block<StandardSchemaV1<unknown, unknown>, unknown>[]

Every block the app renders, each from defineBlock. Names must be unique across the array, and order does not matter: a slot may name a block declared later.

Returns

Registry

A registry resolving a block by name and listing the names it holds, in the order the blocks were given.

Throws

duplicate-block-name when two blocks share a name, carrying the name in at.

Throws

slot-allow-unknown when a slot's allow names a block no entry defines. Every bad entry is reported in one error as "Name" (Block.slot), alongside the names that are registered — two typos take one round trip, not two.

Example

import { createRegistry, defineBlock } from "@nubbin/core";
import { z } from "zod";

const pageBlock = defineBlock({
name: "Page",
schema: z.object({ title: z.string() }),
component: null,
version: 1,
slots: { items: { allow: ["Testimonial"] } },
});

const testimonialBlock = defineBlock({
name: "Testimonial",
schema: z.object({ quote: z.string() }),
component: null,
version: 1,
slots: {},
});

const registry = createRegistry([pageBlock, testimonialBlock]);

registry.get("Page")?.name; // "Page"
registry.get("Nope"); // undefined
registry.names(); // ["Page", "Testimonial"]