import type { RootLogger } from "@workleap/logging"; import type { Runtime } from "../runtime/Runtime.ts"; import { isFunction } from "../shared/assertions.ts"; import type { ModuleDefinition } from "./ModuleManager.ts"; import { ModuleRegistrationError, type ModuleRegistrationStatus, type ModuleRegistrationStatusChangedListener, type ModuleRegistry, type RegisterModulesOptions } from "./ModuleRegistry.ts"; import { registerModule, type DeferredRegistrationFunction, type ModuleRegisterFunction } from "./registerModule.ts"; export const LocalModuleRegistryId = "local"; export const LocalModulesRegistrationStartedEvent = "squide-local-modules-registration-started"; export const LocalModulesRegistrationCompletedEvent = "squide-local-modules-registration-completed"; export const LocalModuleRegistrationFailedEvent = "squide-local-module-registration-failed"; export const LocalModulesDeferredRegistrationStartedEvent = "squide-local-modules-deferred-registration-started"; export const LocalModulesDeferredRegistrationCompletedEvent = "squide-local-modules-deferred-registration-completed"; export const LocalModuleDeferredRegistrationFailedEvent = "squide-local-module-deferred-registration-failed"; export const LocalModulesDeferredRegistrationsUpdateStartedEvent = "squide-local-modules-deferred-registrations-update-started"; export const LocalModulesDeferredRegistrationsUpdateCompletedEvent = "squide-local-modules-deferred-registrations-update-completed-started"; export const LocalModuleDeferredRegistrationUpdateFailedEvent = "squide-local-module-deferred-registration-update-failed"; export interface LocalModulesRegistrationStartedEventPayload { moduleCount: number; } export interface LocalModulesRegistrationCompletedEventPayload { moduleCount: number; } export interface LocalModulesDeferredRegistrationStartedEventPayload { registrationCount: number; } export interface LocalModulesDeferredRegistrationCompletedEventPayload { registrationCount: number; } export interface LocalModulesDeferredRegistrationsUpdateStartedEventPayload { registrationCount: number; } export interface LocalModulesDeferredRegistrationsUpdateCompletedEventPayload { registrationCount: number; } declare module "../messaging/EventBus.ts" { interface EventMap { "squide-local-modules-registration-started": LocalModulesRegistrationStartedEventPayload; "squide-local-modules-registration-completed": LocalModulesRegistrationCompletedEventPayload; "squide-local-module-registration-failed": ModuleRegistrationError; "squide-local-modules-deferred-registration-started": LocalModulesDeferredRegistrationStartedEventPayload; "squide-local-modules-deferred-registration-completed": LocalModulesDeferredRegistrationCompletedEventPayload; "squide-local-module-deferred-registration-failed": ModuleRegistrationError; "squide-local-modules-deferred-registrations-update-started": LocalModulesDeferredRegistrationsUpdateStartedEventPayload; "squide-local-modules-deferred-registrations-update-completed-started": LocalModulesDeferredRegistrationsUpdateCompletedEventPayload; "squide-local-module-deferred-registration-update-failed": ModuleRegistrationError; } } interface DeferredRegistration { index: string; fct: DeferredRegistrationFunction; } export class LocalModuleRegistry implements ModuleRegistry { #registrationStatus: ModuleRegistrationStatus = "none"; readonly #deferredRegistrations: DeferredRegistration[] = []; readonly #statusChangedListeners = new Set(); get id() { return LocalModuleRegistryId; } async registerModules(runtime: TRuntime, registrationFunctions: ModuleRegisterFunction[], { context }: RegisterModulesOptions = {}) { const errors: ModuleRegistrationError[] = []; if (this.#registrationStatus !== "none") { throw new Error("[squide] The registerLocalModules function can only be called once."); } if (registrationFunctions.length > 0) { runtime.logger.information(`[squide] Found ${registrationFunctions.length} local module${registrationFunctions.length !== 1 ? "s" : ""} to register.`); this.#setRegistrationStatus("registering-modules"); runtime.eventBus.dispatch(LocalModulesRegistrationStartedEvent, { moduleCount: registrationFunctions.length } satisfies LocalModulesRegistrationStartedEventPayload); let completedCount = 0; await Promise.allSettled( registrationFunctions.map(async (x, index) => { const loggerScope = (runtime.logger as RootLogger).startScope(`[squide] ${index + 1}/${registrationFunctions.length} Registering local module.`); const runtimeScope = runtime.startScope(loggerScope); try { const optionalDeferredRegistration = await registerModule(x, runtimeScope as TRuntime, context); if (isFunction(optionalDeferredRegistration)) { this.#deferredRegistrations.push({ index: `${index + 1}/${registrationFunctions.length}`, fct: optionalDeferredRegistration as DeferredRegistrationFunction }); } completedCount += 1; loggerScope.information("[squide] Successfully registered local module.", { style: { color: "green" } }); loggerScope.end({ labelStyle: { color: "green" } }); } catch (error: unknown) { loggerScope .withText("[squide] An error occured while registering the local module.") .withError(error as Error) .error(); loggerScope.end({ labelStyle: { color: "red" } }); errors.push(new ModuleRegistrationError("An error occured while registering a local module.", { cause: error })); } }) ); if (errors.length > 0) { errors.forEach(x => { runtime.eventBus.dispatch(LocalModuleRegistrationFailedEvent, x); }); } // Must be dispatched before updating the registration status to ensure bootstrapping events sequencing. runtime.eventBus.dispatch(LocalModulesRegistrationCompletedEvent, { moduleCount: completedCount } satisfies LocalModulesRegistrationCompletedEventPayload); this.#setRegistrationStatus(this.#deferredRegistrations.length > 0 ? "modules-registered" : "ready"); } else { // There's no modules to register, it can be considered as ready. this.#setRegistrationStatus("ready"); } return errors; } async registerDeferredRegistrations(runtime: TRuntime, data?: TData) { const errors: ModuleRegistrationError[] = []; if (this.#registrationStatus === "ready" && this.#deferredRegistrations.length === 0) { // No deferred registrations were returned by the local modules, skip this phase. return errors; } if (this.#registrationStatus === "none" || this.#registrationStatus === "registering-modules") { throw new Error("[squide] The registerDeferredRegistrations function can only be called once the local modules are registered."); } if (this.#registrationStatus !== "modules-registered") { throw new Error("[squide] The registerDeferredRegistrations function can only be called once."); } this.#setRegistrationStatus("registering-deferred-registration"); runtime.eventBus.dispatch(LocalModulesDeferredRegistrationStartedEvent, { registrationCount: this.#deferredRegistrations.length } satisfies LocalModulesDeferredRegistrationStartedEventPayload); let completedCount = 0; await Promise.allSettled( this.#deferredRegistrations.map(async ({ index, fct: deferredRegister }) => { const loggerScope = (runtime.logger as RootLogger).startScope(`[squide] ${index} Registering local module deferred registrations.`); const runtimeScope = runtime.startScope(loggerScope); loggerScope.withText("Data:").withObject(data).debug(); try { await deferredRegister(runtimeScope, data, "register"); completedCount += 1; loggerScope.information("[squide] Successfully registered deferred registrations.", { style: { color: "green" } }); loggerScope.end({ labelStyle: { color: "green" } }); } catch (error: unknown) { loggerScope .withText("[squide] An error occured while registering the deferred registrations.") .withError(error as Error) .error(); loggerScope.end({ labelStyle: { color: "red" } }); errors.push(new ModuleRegistrationError("An error occured while registering the deferred registrations of a local module.", { cause: error })); } }) ); if (errors.length > 0) { errors.forEach(x => { runtime.eventBus.dispatch(LocalModuleDeferredRegistrationFailedEvent, x); }); } // Must be dispatched before updating the registration status to ensure bootstrapping events sequencing. runtime.eventBus.dispatch(LocalModulesDeferredRegistrationCompletedEvent, { registrationCount: completedCount } satisfies LocalModulesDeferredRegistrationCompletedEventPayload); this.#setRegistrationStatus("ready"); return errors; } async updateDeferredRegistrations(runtime: TRuntime, data?: TData) { const errors: ModuleRegistrationError[] = []; if (this.#registrationStatus !== "ready") { throw new Error("[squide] The updateDeferredRegistrations function can only be called once the local modules are ready."); } runtime.eventBus.dispatch(LocalModulesDeferredRegistrationsUpdateStartedEvent, { registrationCount: this.#deferredRegistrations.length } satisfies LocalModulesDeferredRegistrationsUpdateStartedEventPayload); let completedCount = 0; await Promise.allSettled( this.#deferredRegistrations.map(async ({ index, fct: deferredRegister }) => { const loggerScope = (runtime.logger as RootLogger).startScope(`[squide] ${index} Updating local module deferred registrations.`); const runtimeScope = runtime.startScope(loggerScope); loggerScope.withText("Data:").withObject(data).debug(); try { await deferredRegister(runtimeScope, data, "update"); completedCount += 1; loggerScope.information("[squide] Successfully updated deferred registrations.", { style: { color: "green" } }); loggerScope.end({ labelStyle: { color: "green" } }); } catch (error: unknown) { loggerScope .withText(`[squide] ${index} An error occured while updating the deferred registrations.`) .withError(error as Error) .error(); loggerScope.end({ labelStyle: { color: "red" } }); errors.push(new ModuleRegistrationError("An error occured while updating the deferred registrations a local module.", { cause: error })); } }) ); if (errors.length > 0) { errors.forEach(x => { runtime.eventBus.dispatch(LocalModuleDeferredRegistrationUpdateFailedEvent, x); }); } // Must be dispatched before updating the registration status to ensure bootstrapping events sequencing. runtime.eventBus.dispatch(LocalModulesDeferredRegistrationsUpdateCompletedEvent, { registrationCount: completedCount } satisfies LocalModulesDeferredRegistrationsUpdateCompletedEventPayload); return errors; } registerStatusChangedListener(callback: ModuleRegistrationStatusChangedListener) { this.#statusChangedListeners.add(callback); return () => { this.removeStatusChangedListener(callback); }; } removeStatusChangedListener(callback: ModuleRegistrationStatusChangedListener) { this.#statusChangedListeners.delete(callback); } setAsReady() { if (this.#registrationStatus !== "none") { throw new Error(`[squide] Cannot set a module registry as "ready" when registrations are already in progress. The registry current status is "${this.#registrationStatus}".`); } this.#setRegistrationStatus("ready"); } #setRegistrationStatus(status: ModuleRegistrationStatus) { this.#registrationStatus = status; this.#statusChangedListeners.forEach(x => { x(); }); } get registrationStatus() { return this.#registrationStatus; } } export function toLocalModuleDefinitions(localModules: (ModuleRegisterFunction | undefined)[]): ModuleDefinition[] { return localModules .filter((x): x is ModuleRegisterFunction => Boolean(x)) .map(x => ({ definition: x, registryId: LocalModuleRegistryId })); }