{
  "version": 3,
  "sources": ["../../src/modules/module-factory-locator/ModuleFactoryLocator.ts", "../../src/modules/module-factory-locator/standardCreatableFactories.ts"],
  "sourcesContent": ["import { assertEx } from '@ariestools/sdk'\nimport { asSchema, type Schema } from '@xyo-network/sdk-protocol'\n\nimport type {\n  CreatableModuleFactory,\n  CreatableModuleRegistry,\n  LabeledCreatableModuleFactory,\n  Labels,\n  ModuleFactoryLocatorInstance,\n} from '#module-model'\nimport {\n  hasAllLabels,\n  hasLabels,\n  registerCreatableModuleFactory,\n} from '#module-model'\n\nimport { standardCreatableFactories } from './standardCreatableFactories.ts'\n\n/**\n * A class which encapsulates the Service Locator Pattern for Module Factories\n */\nexport class ModuleFactoryLocator implements ModuleFactoryLocatorInstance {\n  protected readonly _registry: CreatableModuleRegistry\n\n  private _frozen = false\n\n  constructor(_registry: CreatableModuleRegistry = standardCreatableFactories()) {\n    this._registry = _registry\n  }\n\n  /**\n   * The current registry for the module factory\n   */\n  get registry(): Readonly<CreatableModuleRegistry> {\n    return this._registry\n  }\n\n  static empty(): ModuleFactoryLocator {\n    return new ModuleFactoryLocator({})\n  }\n\n  static standard(): ModuleFactoryLocator {\n    return new ModuleFactoryLocator(standardCreatableFactories())\n  }\n\n  freeze(): void {\n    this._frozen = true\n  }\n\n  /**\n   * Locates a module factory that matches the supplied schema and labels\n   * @param schema The config schema for the module\n   * @param labels The labels for the module factory\n   * @returns A module factory that matches the supplied schema and labels or throws if one is not found\n   */\n  locate(schema: Schema, labels?: Labels): CreatableModuleFactory | LabeledCreatableModuleFactory {\n    return assertEx(\n      this.tryLocate(schema, labels),\n\n      () => `No module factory for the supplied config schema [${schema}]${labels ? ` & labels [${JSON.stringify(labels)}]` : ''} registered`,\n    )\n  }\n\n  merge(locator: ModuleFactoryLocatorInstance): ModuleFactoryLocatorInstance {\n    const registry = { ...this.registry }\n    for (const schema in locator.registry) {\n      const typedSchema = asSchema(schema, true)\n      if (registry[typedSchema]) {\n        registry[typedSchema].push(...(locator.registry[typedSchema] ?? []))\n      } else {\n        registry[typedSchema] = locator.registry[typedSchema]\n      }\n    }\n    return new ModuleFactoryLocator(registry)\n  }\n\n  /**\n   * Registers a single module factory (with optional tags) with the locator\n   * @param factory The factory to register\n   * @param labels The labels for the module factory\n   */\n  register(factory: CreatableModuleFactory, labels?: Labels, primary: boolean | Schema | Schema[] = false): this {\n    assertEx(!this._frozen, () => 'Cannot register a module factory after the locator has been frozen')\n    registerCreatableModuleFactory(this._registry, factory, labels, primary)\n    return this\n  }\n\n  /**\n   * Registers multiple module factories with the locator\n   * @param factories The factories to register\n   */\n  registerMany(factories: CreatableModuleFactory[]): this {\n    for (const factory of factories) {\n      this.register(factory)\n    }\n    return this\n  }\n\n  /**\n   * Tries to locate a module factory that matches the supplied schema and labels\n   * @param schema The config schema for the module\n   * @param labels The labels for the module factory\n   * @returns A module factory that matches the supplied schema and labels or undefined\n   */\n  tryLocate(schema: Schema, labels?: Labels): CreatableModuleFactory | LabeledCreatableModuleFactory | undefined {\n    return labels\n      // Find the first factory that has labels and has all the labels provided\n      ? (this._registry[schema]?.filter(hasLabels).find(factory => hasAllLabels(factory?.labels, labels)) ?? this._registry[schema]?.[0])\n      // Otherwise, return the first factory\n      : this._registry[schema]?.[0]\n  }\n}\n", "import { MemoryArchivist } from '#archivist-memory'\nimport { ViewArchivist } from '#archivist-view'\nimport { HttpBridge } from '#bridge-http'\nimport { MemoryBoundWitnessDiviner } from '#diviner-boundwitness/memory'\nimport { IdentityDiviner } from '#diviner-identity'\nimport { GenericPayloadDiviner } from '#diviner-payload-generic'\nimport type {\n  CreatableModuleFactory, CreatableModuleRegistry, LabeledCreatableModuleFactory,\n} from '#module-model'\nimport { registerCreatableModuleFactories } from '#module-model'\nimport { MemoryNode } from '#node-memory'\nimport { ViewNode } from '#node-view'\nimport { MemorySentinel } from '#sentinel-memory'\nimport { AdhocWitness } from '#witness-adhoc'\n\n// order matters in this array.  later items will register themselves as primary for schemas shared with earlier items\nexport const standardCreatableModulesList: (CreatableModuleFactory | LabeledCreatableModuleFactory)[] = [\n  HttpBridge.factory(),\n  ViewArchivist.factory(),\n  ViewNode.factory(),\n  AdhocWitness.factory(),\n  GenericPayloadDiviner.factory(),\n  MemoryBoundWitnessDiviner.factory(),\n  IdentityDiviner.factory(),\n  MemoryArchivist.factory(),\n  MemoryArchivist.factory(),\n  MemoryNode.factory(),\n  MemorySentinel.factory(),\n  GenericPayloadDiviner.factory(),\n]\n\nexport const standardCreatableFactories = (): CreatableModuleRegistry => {\n  return registerCreatableModuleFactories(standardCreatableModulesList, {}, true)\n}\n"],
  "mappings": ";AAAA,SAAS,gBAAgB;AACzB,SAAS,gBAA6B;AAStC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACdP,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;AAC9B,SAAS,kBAAkB;AAC3B,SAAS,iCAAiC;AAC1C,SAAS,uBAAuB;AAChC,SAAS,6BAA6B;AAItC,SAAS,wCAAwC;AACjD,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AACzB,SAAS,sBAAsB;AAC/B,SAAS,oBAAoB;AAGtB,IAAM,+BAA2F;AAAA,EACtG,WAAW,QAAQ;AAAA,EACnB,cAAc,QAAQ;AAAA,EACtB,SAAS,QAAQ;AAAA,EACjB,aAAa,QAAQ;AAAA,EACrB,sBAAsB,QAAQ;AAAA,EAC9B,0BAA0B,QAAQ;AAAA,EAClC,gBAAgB,QAAQ;AAAA,EACxB,gBAAgB,QAAQ;AAAA,EACxB,gBAAgB,QAAQ;AAAA,EACxB,WAAW,QAAQ;AAAA,EACnB,eAAe,QAAQ;AAAA,EACvB,sBAAsB,QAAQ;AAChC;AAEO,IAAM,6BAA6B,MAA+B;AACvE,SAAO,iCAAiC,8BAA8B,CAAC,GAAG,IAAI;AAChF;;;ADZO,IAAM,uBAAN,MAAM,sBAA6D;AAAA,EACrD;AAAA,EAEX,UAAU;AAAA,EAElB,YAAY,YAAqC,2BAA2B,GAAG;AA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