You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
import { compileMorfo, type CompiledMorfo } from './compile';
|
|
import { registerCompiledContract } from './contracts';
|
|
import type { Morfo } from './types';
|
|
|
|
export interface RegisteredMorfo {
|
|
readonly morfo: Morfo;
|
|
readonly compiled: CompiledMorfo;
|
|
}
|
|
|
|
const MORFOS = new Map<string, RegisteredMorfo>();
|
|
const LISTENERS = new Set<(record: RegisteredMorfo) => void>();
|
|
|
|
/**
|
|
* Register a morfo. Compiles it, registers its `data-*` contract, and notifies
|
|
* listeners.
|
|
*
|
|
* Note: morfo no longer owns a translation catalog. Component-owned strings
|
|
* live in `src/uix/langs/components/{kebab}.ts` and are registered globally by
|
|
* `ActiveUix` under `components.{kebab}.*`. The morfo merely declares its text
|
|
* slots via `morfo.texts: Record<string, LangRef>` (absolute idlangrefs).
|
|
*/
|
|
export function registerMorfo(morfo: Morfo): CompiledMorfo {
|
|
const compiled = compileMorfo(morfo);
|
|
registerCompiledContract(compiled);
|
|
|
|
const key = morfo.kebab.toLowerCase();
|
|
const previous = MORFOS.get(key);
|
|
const record: RegisteredMorfo = { morfo, compiled };
|
|
|
|
MORFOS.set(key, record);
|
|
|
|
if (!previous) {
|
|
for (const listener of LISTENERS) listener(record);
|
|
}
|
|
|
|
return compiled;
|
|
}
|
|
|
|
export function getRegisteredMorfo(kebab: string): RegisteredMorfo | undefined {
|
|
return MORFOS.get(kebab.toLowerCase());
|
|
}
|
|
|
|
export function listRegisteredMorfos(): readonly RegisteredMorfo[] {
|
|
return [...MORFOS.values()];
|
|
}
|
|
|
|
export function onMorfoRegistered(listener: (record: RegisteredMorfo) => void): () => void {
|
|
LISTENERS.add(listener);
|
|
return () => LISTENERS.delete(listener);
|
|
}
|
|
|
|
export function _resetMorfoRegistryForTesting(): void {
|
|
MORFOS.clear();
|
|
LISTENERS.clear();
|
|
}
|