@prismshadow/penguin-core/kernel · Authoring

@Module

decorator
Source ↗
IMPORT import { Module } from "@prismshadow/penguin-core/kernel";

Registers a class as a module. create(ctx, context) runs once per App; the class's @Use fields are injected before it, its @Provide fields are read after it, its @Bind fields are the code halves of its contributions, and park() (optional) parks its state.

Options ModuleMeta · default {}

name

readonly name?: string

An explicit node name, for a module whose assembly node keeps a name its class no longer carries (a moved implementation wrapped in a provider): the name the node boots and is replaced under. Absent = the class's own name, which is the default and keeps the name and the class from drifting.

contributes

readonly contributes?: Readonly<Record<string, readonly ({ readonly id: string; } & JsonObject)[]>>

Declares pure-JSON contribution entries targeting <HostModule>.<slot> slots on another module's *Slots interface. Each entry requires a unique id; for a code-carrying Slot<Data, Code>, bind the live implementation on a field with @Bind(id) in setup() — the booter guarantees this module boots before the slot host.

@function Module(meta?: ModuleMeta): (target: ModuleClass, context: ClassDecoratorContext) => void

Registers a class as a module. create(ctx, context) runs once per App; the class's @Use fields are injected before it, its @Provide fields are read after it, its @Bind fields are the code halves of its contributions, and park() (optional) parks its state.

Module
({
// Target "<HostModule>.<slot>"; each entry needs a unique `id`
ModuleMeta.contributes?: Readonly<Record<string, readonly ({
    readonly id: string;
} & JsonObject)[]>> | undefined
contributes
: {
"HttpModule.routes": [{ id: stringid: "admin.routes", prefix: stringprefix: "/api/admin" }], }, })class class AdminModuleAdminModule { // Code half for the entry above — ready before HttpModule boots @function Bind(id: string): (_value: undefined, context: ClassFieldDecoratorContext) => void

The code half of one contribution: @Bind("agents.routes") routes!: Hono; — assigned in create.

Bind
("admin.routes") AdminModule.routes: RouteHandlerroutes!: RouteHandler;
AdminModule.setup(): voidsetup() { this.AdminModule.routes: RouteHandlerroutes = { RouteHandler.handle(path: string): stringhandle: (path: stringpath) => `admin:${path: stringpath}` }; }}

context

readonly context?: ContextDecl

Declares the versioned JSON schema ({ version, schema? }) for state returned by park() across hot upgrades. On reboot, older parked documents run through migrations[v], are validated against schema (arktype syntax), and arrive as the second argument to setup(ctx, saved).

@function Module(meta?: ModuleMeta): (target: ModuleClass, context: ClassDecoratorContext) => void

Registers a class as a module. create(ctx, context) runs once per App; the class's @Use fields are injected before it, its @Provide fields are read after it, its @Bind fields are the code halves of its contributions, and park() (optional) parks its state.

Module
({
// Versioned JSON schema validated on hot-upgrade restore ModuleMeta.context?: ContextDecl | undefinedcontext: { ContextDecl.version: numberversion: 2, ContextDecl.schema?: Json | undefined

An arktype definition; absent = opaque (anything parks).

schema
: { count: stringcount: "number" } },
})class class CounterModuleCounterModule { CounterModule.count: numbercount = 0; // Upgrade legacy v1 parked documents before setup() runs
CounterModule.migrations: {
    1: (old: Json) => {
        count: number;
    };
}
migrations
= { 1: (old: Jsonold:
type Json = string | number | boolean | Json[] | {
    [key: string]: Json;
} | null

Kernel currency: everything that crosses a park/boot boundary is plain JSON.

Json
) => ({ count: numbercount:
var Number: NumberConstructor
(value?: any) => number

An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

Number
(old: Jsonold) || 0 }) };
CounterModule.setup(_ctx: ClassCtx, saved: Json): voidsetup(_ctx: ClassCtx_ctx:
type ClassCtx = {
    contributions: Record<string, Contributed[]>;
    resources: Resources;
    effect: (dispose: () => void) => void;
}

What a module class's create receives: everything in ModuleCtx except use — requirements are fields.

ClassCtx
, saved: Jsonsaved:
type Json = string | number | boolean | Json[] | {
    [key: string]: Json;
} | null

Kernel currency: everything that crosses a park/boot boundary is plain JSON.

Json
) {
this.CounterModule.count: numbercount = (saved: Jsonsaved as { count?: number | undefinedcount?: number } | null)?.count?: number | undefinedcount ?? 0; } CounterModule.park(): Jsonpark():
type Json = string | number | boolean | Json[] | {
    [key: string]: Json;
} | null

Kernel currency: everything that crosses a park/boot boundary is plain JSON.

Json
{
return { count: numbercount: this.CounterModule.count: numbercount }; }}

children

readonly children?: readonly ModuleClass[]

Lists child @Module and @Component classes owned by this subtree. The booter orders them topologically by @Use and Slot<Data, Code> dependencies (regardless of array order) and disposes in reverse; children stay private unless forwarded via exports.

@function Module(meta?: ModuleMeta): (target: ModuleClass, context: ClassDecoratorContext) => void

Registers a class as a module. create(ctx, context) runs once per App; the class's @Use fields are injected before it, its @Provide fields are read after it, its @Bind fields are the code halves of its contributions, and park() (optional) parks its state.

Module
({
// Order in array does not matter: SessionsModule boots before SchedulerModule ModuleMeta.children?: readonly ModuleClass[] | undefinedchildren: [class SchedulerModuleSchedulerModule, class SessionsModuleSessionsModule], })class class PlatformModulePlatformModule {}

exports

readonly exports?: readonly IfaceClass<any>[]

Interfaces this module forwards from its children — what the subtree offers outside.

@function Module(meta?: ModuleMeta): (target: ModuleClass, context: ClassDecoratorContext) => void

Registers a class as a module. create(ctx, context) runs once per App; the class's @Use fields are injected before it, its @Provide fields are read after it, its @Bind fields are the code halves of its contributions, and park() (optional) parks its state.

Module
({
ModuleMeta.children?: readonly ModuleClass[] | undefinedchildren: [class UsersRepoUsersRepo, class InternalCacheInternalCache], // Only Users is visible to siblings; InternalCache stays private ModuleMeta.exports?: readonly IfaceClass<any>[] | undefined

Interfaces this module forwards from its children — what the subtree offers outside.

exports
: [class UsersUsers],
})class class IdentityModuleIdentityModule {}