@open-pioneer/runtime 4.8.0 → 4.9.0-dev.20261002130116

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +189 -173
  2. package/CustomElement.d.ts +5 -5
  3. package/CustomElement.js +1 -3
  4. package/CustomElement.js.map +1 -1
  5. package/README.md +14 -6
  6. package/Service.d.ts +2 -2
  7. package/Service.js.map +1 -1
  8. package/_virtual/source-info.js +7 -5
  9. package/_virtual/source-info.js.map +1 -1
  10. package/api.d.ts +7 -8
  11. package/app/AppInstance.js +10 -6
  12. package/app/AppInstance.js.map +1 -1
  13. package/app/gatherConfig.d.ts +13 -4
  14. package/app/gatherConfig.js +23 -12
  15. package/app/gatherConfig.js.map +1 -1
  16. package/app/logErrors.js +1 -1
  17. package/builtin-services/ApplicationContextImpl.js +7 -1
  18. package/builtin-services/ApplicationContextImpl.js.map +1 -1
  19. package/builtin-services/{ApplicationLifecycleEventService.d.ts → ApplicationLifecycleEventServiceImpl.d.ts} +1 -1
  20. package/builtin-services/{ApplicationLifecycleEventService.js → ApplicationLifecycleEventServiceImpl.js} +4 -4
  21. package/builtin-services/ApplicationLifecycleEventServiceImpl.js.map +1 -0
  22. package/builtin-services/NumberParserServiceImpl.d.ts +1 -1
  23. package/builtin-services/NumberParserServiceImpl.js +3 -3
  24. package/builtin-services/NumberParserServiceImpl.js.map +1 -1
  25. package/builtin-services/index.js +2 -2
  26. package/builtin-services/index.js.map +1 -1
  27. package/dom/index.d.ts +0 -1
  28. package/dom/index.js +1 -1
  29. package/dom/index.js.map +1 -1
  30. package/i18n/AppIntl.d.ts +1 -1
  31. package/i18n/AppIntl.js +5 -10
  32. package/i18n/AppIntl.js.map +1 -1
  33. package/i18n/PackageIntl.js +2 -5
  34. package/i18n/PackageIntl.js.map +1 -1
  35. package/metadata/ObservableBox.d.ts +1 -1
  36. package/metadata/ObservableBox.js.map +1 -1
  37. package/package.json +6 -6
  38. package/react-integration/PackageContext.d.ts +1 -1
  39. package/react-integration/PackageContext.js.map +1 -1
  40. package/react-integration/hooks.d.ts +4 -4
  41. package/react-integration/hooks.js +6 -7
  42. package/react-integration/hooks.js.map +1 -1
  43. package/service-layer/PackageRepr.d.ts +1 -1
  44. package/service-layer/PackageRepr.js +1 -1
  45. package/service-layer/PackageRepr.js.map +1 -1
  46. package/service-layer/ServiceLayer.d.ts +3 -2
  47. package/service-layer/ServiceLayer.js +4 -3
  48. package/service-layer/ServiceLayer.js.map +1 -1
  49. package/service-layer/ServiceLookup.d.ts +4 -3
  50. package/service-layer/ServiceLookup.js +3 -3
  51. package/service-layer/ServiceLookup.js.map +1 -1
  52. package/service-layer/ServiceRepr.d.ts +2 -2
  53. package/service-layer/ServiceRepr.js +7 -5
  54. package/service-layer/ServiceRepr.js.map +1 -1
  55. package/builtin-services/ApplicationLifecycleEventService.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceLayer.js","sources":["ServiceLayer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createLogger, Error } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { RUNTIME_PACKAGE_NAME } from \"../builtin-services\";\nimport { ErrorId } from \"../errors\";\nimport { ReferenceMeta } from \"../Service\";\nimport {\n InterfaceSpec,\n isAllImplementationsSpec,\n isSingleImplementationSpec,\n ReferenceSpec,\n renderInterfaceSpec\n} from \"./InterfaceSpec\";\nimport { PackageRepr } from \"./PackageRepr\";\nimport { ReadonlyServiceLookup, ServiceLookupResult, ServicesLookupResult } from \"./ServiceLookup\";\nimport { ServiceRepr } from \"./ServiceRepr\";\nimport {\n ComputedServiceDependencies,\n FrameworkDependency,\n UIDependency,\n verifyDependencies\n} from \"./verifyDependencies\";\n\nconst LOG = createLogger(sourceId);\n\nexport type DynamicLookupResult = ServiceLookupResult | UndeclaredDependency;\n\n/**\n * Returned when a dependency is not declared in the package's metadata.\n */\nexport interface UndeclaredDependency {\n type: \"undeclared\";\n}\n\n/**\n * Returned when a package is not found in the application's metadata.\n */\nexport interface UnknownPackage {\n type: \"unknown-package\";\n}\n\nconst UNDECLARED_DEPENDENCY = { type: \"undeclared\" } satisfies UndeclaredDependency;\nconst UNKNOWN_PACKAGE = { type: \"unknown-package\" } satisfies UnknownPackage;\n\ninterface DependencyDeclarations {\n all: boolean;\n unqualified: boolean;\n qualifiers: Set<string>;\n}\n\nexport class ServiceLayer {\n // All services in the application.\n #allServices: ServiceRepr[];\n\n // Set of required services. These services and their dependencies will be started.\n #requiredServices: Set<ServiceRepr>;\n\n #serviceLookup: ReadonlyServiceLookup;\n\n // Service id --> Service dependencies\n #serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n // Package name --> Interface name --> Declarations\n #declaredDependencies;\n #state: \"not-started\" | \"started\" | \"destroyed\" = \"not-started\";\n\n /**\n * Constructs a new service layer instance.\n * Requires the application's set of packages (from which services are taken)\n * and an optional set of forced references (interfaces whose implementing services will be started unconditionally).\n *\n * In its current form, the service layer will start only forced references and references needed by the UI (and their dependencies).\n */\n constructor(packages: readonly PackageRepr[], forcedReferences: ReferenceSpec[] = []) {\n const allServices = packages.map((pkg) => pkg.services).flat();\n const uiDependencies = packages\n .map((pkg) =>\n pkg.uiReferences.map<UIDependency>((dep) => {\n return {\n type: \"ui\",\n packageName: pkg.name,\n ...dep\n };\n })\n )\n .flat();\n const requiredReferences = [\n ...forcedReferences.map<FrameworkDependency>((r) => ({\n type: \"framework\",\n ...r\n })),\n ...uiDependencies\n ];\n const { serviceLookup, serviceDependencies } = verifyDependencies({\n services: allServices,\n requiredReferences: requiredReferences\n });\n\n this.#allServices = allServices;\n this.#requiredServices = getRequiredServices(requiredReferences, serviceLookup);\n this.#serviceLookup = serviceLookup;\n this.#serviceDependencies = serviceDependencies;\n this.#declaredDependencies = buildDependencyIndex(packages);\n }\n\n destroy() {\n for (const service of this.#requiredServices) {\n this.#destroyService(service);\n }\n this.#state = \"destroyed\";\n }\n\n start() {\n if (this.#state !== \"not-started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer was already started.\");\n }\n\n for (const service of this.#requiredServices) {\n this.#createService(service);\n }\n this.#state = \"started\";\n\n // Give warnings for unneeded services during development.\n if (import.meta.env.DEV) {\n const unneededServices = this.#allServices\n .filter(\n (service) =>\n service.packageName !== RUNTIME_PACKAGE_NAME &&\n service.state === \"not-constructed\"\n )\n .map((service) => `'${service.id}'`);\n if (unneededServices.length) {\n const services = unneededServices.join(\", \");\n LOG.warn(\n `The following services are contained in the application but were not started: ${services}.`\n );\n }\n }\n }\n\n /**\n * Returns a service implementing the given interface.\n * Checks that the given package actually declared a dependency on that interface\n * to enforce coding guidelines.\n *\n * @param packageName the name of the package requesting the import\n * @param spec the interface specifier\n * @param options advanced options to customize lookup\n * @throws if the service layer is not in 'started' state or if no service implements the interface.\n */\n getService(\n packageName: string,\n spec: InterfaceSpec,\n options?: { ignoreDeclarationCheck?: boolean }\n ): ServiceLookupResult | UndeclaredDependency | UnknownPackage {\n if (this.#state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n if (!options?.ignoreDeclarationCheck) {\n const checkError = this.#checkDependency(packageName, spec);\n if (checkError) {\n return checkError;\n }\n }\n\n return this.#serviceLookup.lookupOne(spec);\n }\n\n /**\n * Returns all services implementing the given interface.\n * Requires that the dependency on all implementations has been declared in the package.\n *\n * @param packageName the name of the package requesting the import\n * @param interfaceName the interface name\n *\n * @throws if the service layer is not in 'started'.\n */\n getServices(\n packageName: string,\n interfaceName: string\n ): ServicesLookupResult | UndeclaredDependency | UnknownPackage {\n if (this.#state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n const checkError = this.#checkDependency(packageName, { interfaceName, all: true });\n if (checkError) {\n return checkError;\n }\n return this.#serviceLookup.lookupAll(interfaceName);\n }\n\n /**\n * Initializes the given service and its dependencies.\n * Dependencies are initialized before the service that requires them.\n */\n #createService(service: ServiceRepr) {\n if (service.state === \"constructed\") {\n const instance = service.getInstanceOrThrow();\n service.addRef();\n return instance;\n }\n if (service.state === \"constructing\") {\n throw new Error(ErrorId.INTERNAL, \"Cycle during service construction.\");\n }\n if (service.state !== \"not-constructed\") {\n throw new Error(ErrorId.INTERNAL, \"Invalid service state.\");\n }\n\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const references: Record<string, any> = {};\n const referencesMeta: Record<string, ReferenceMeta | ReferenceMeta[]> = {};\n\n // Sets state to 'constructing' to detect cycles\n service.beforeCreate();\n\n // Initialize dependencies recursively before creating the current service.\n for (const [referenceName, serviceDeps] of Object.entries(this.#getServiceDeps(service))) {\n const [referenceValue, referenceMeta] = this.#getReference(serviceDeps);\n references[referenceName] = referenceValue;\n referencesMeta[referenceName] = referenceMeta;\n }\n\n // Sets state to 'constructed' to finish the state transition, useCount is 1.\n LOG.debug(`Creating service '${service.id}'.`);\n return service.create({ references, referencesMeta });\n }\n\n /**\n * Destroys the given service and its dependencies.\n * The dependencies are destroyed after the service.\n */\n #destroyService(service: ServiceRepr) {\n if (service.state === \"destroyed\") {\n return;\n }\n\n // Destroy the service before its dependencies (reverse order\n // compared to construction).\n if (service.removeRef() <= 0) {\n LOG.debug(`Destroying service '${service.id}'`);\n service.destroy();\n\n for (const serviceDeps of Object.values(this.#getServiceDeps(service))) {\n if (Array.isArray(serviceDeps)) {\n for (const dep of serviceDeps) {\n this.#destroyService(dep);\n }\n } else {\n this.#destroyService(serviceDeps);\n }\n }\n }\n }\n\n /** Undefined -> everything is okay */\n #checkDependency(\n packageName: string,\n spec: ReferenceSpec\n ): UnknownPackage | UndeclaredDependency | undefined {\n const packageEntry = this.#declaredDependencies.get(packageName);\n if (!packageEntry) {\n return UNKNOWN_PACKAGE;\n }\n\n const interfaceEntry = packageEntry.get(spec.interfaceName);\n if (!interfaceEntry) {\n return UNDECLARED_DEPENDENCY;\n }\n\n if (isSingleImplementationSpec(spec)) {\n if (spec.qualifier == null) {\n return interfaceEntry.unqualified ? undefined : UNDECLARED_DEPENDENCY;\n }\n return interfaceEntry.qualifiers.has(spec.qualifier)\n ? undefined\n : UNDECLARED_DEPENDENCY;\n } else {\n return interfaceEntry.all ? undefined : UNDECLARED_DEPENDENCY;\n }\n }\n\n #getReference(ref: ServiceRepr): [unknown, ReferenceMeta];\n #getReference(ref: ServiceRepr[]): [unknown[], ReferenceMeta[]];\n #getReference(\n ref: ServiceRepr | ServiceRepr[]\n ): [unknown, ReferenceMeta] | [unknown[], ReferenceMeta[]];\n #getReference(\n ref: ServiceRepr | ServiceRepr[]\n ): [unknown, ReferenceMeta] | [unknown[], ReferenceMeta[]] {\n if (Array.isArray(ref)) {\n const referenceValues: unknown[] = [];\n const referenceMeta: ReferenceMeta[] = [];\n for (const d of ref) {\n const [value, meta] = this.#getReference(d);\n referenceValues.push(value);\n referenceMeta.push(meta);\n }\n return [referenceValues, referenceMeta];\n }\n\n const referenceValue = this.#createService(ref);\n const referenceMeta: ReferenceMeta = {\n serviceId: ref.id\n };\n return [referenceValue, referenceMeta];\n }\n\n #getServiceDeps(service: ServiceRepr) {\n const dependencies = this.#serviceDependencies.get(service.id);\n if (!dependencies) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find precomputed service dependencies for '${service.id}'.`\n );\n }\n return dependencies;\n }\n}\n\nfunction getRequiredServices(\n requiredReferences: readonly ReferenceSpec[],\n serviceLookup: ReadonlyServiceLookup\n): Set<ServiceRepr> {\n const requiredServices = new Set<ServiceRepr>();\n for (const referenceSpec of requiredReferences) {\n const result = serviceLookup.lookup(referenceSpec);\n if (result.type !== \"found\") {\n // Should be caught by verifier\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find required reference to ${renderInterfaceSpec(referenceSpec)}.`\n );\n }\n if (Array.isArray(result.value)) {\n for (const service of result.value) {\n requiredServices.add(service);\n }\n } else {\n requiredServices.add(result.value);\n }\n }\n return requiredServices;\n}\n\nfunction buildDependencyIndex(packages: readonly PackageRepr[]) {\n // Register declared UI dependencies.\n // This is needed as a lookup structure to that dynamic service lookups\n // can be validated at runtime.\n const index = new Map<string, Map<string, DependencyDeclarations>>();\n for (const pkg of packages) {\n const packageName = pkg.name;\n const packageEntry = new Map<string, DependencyDeclarations>();\n for (const uiReference of pkg.uiReferences) {\n let interfaceEntry = packageEntry.get(uiReference.interfaceName);\n if (!interfaceEntry) {\n interfaceEntry = {\n all: false,\n unqualified: false,\n qualifiers: new Set<string>()\n };\n packageEntry.set(uiReference.interfaceName, interfaceEntry);\n }\n\n if (isAllImplementationsSpec(uiReference)) {\n interfaceEntry.all = true;\n } else if (isSingleImplementationSpec(uiReference)) {\n if (uiReference.qualifier == null) {\n interfaceEntry.unqualified = true;\n } else {\n interfaceEntry.qualifiers.add(uiReference.qualifier);\n }\n } else {\n throw new Error(ErrorId.INTERNAL, `Unexpected implementation spec.`);\n }\n }\n index.set(packageName, packageEntry);\n }\n return index;\n}\n"],"names":["referenceMeta"],"mappings":";;;;;;;AAyBA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAkBjC,MAAM,qBAAA,GAAwB,EAAE,IAAA,EAAM,YAAA,EAAa;AACnD,MAAM,eAAA,GAAkB,EAAE,IAAA,EAAM,iBAAA,EAAkB;AAQ3C,MAAM,YAAA,CAAa;AAAA;AAAA,EAEtB,YAAA;AAAA;AAAA,EAGA,iBAAA;AAAA,EAEA,cAAA;AAAA;AAAA,EAGA,oBAAA;AAAA;AAAA,EAGA,qBAAA;AAAA,EACA,MAAA,GAAkD,aAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASlD,WAAA,CAAY,QAAA,EAAkC,gBAAA,GAAoC,EAAC,EAAG;AAClF,IAAA,MAAM,WAAA,GAAc,SAAS,GAAA,CAAI,CAAC,QAAQ,GAAA,CAAI,QAAQ,EAAE,IAAA,EAAK;AAC7D,IAAA,MAAM,iBAA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AAA,KAAA,MAAW,WAAA,IAAe,IAAI,YAAA,EAAc;AACxC,MAAA,IAAI,cAAA,GAAiB,YAAA,CAAa,GAAA,CAAI,WAAA,CAAY,aAAa,CAAA;AAC/D,MAAA,IAAI,CAAC,cAAA,EAAgB;AACjB,QAAA,cAAA,GAAiB;AAAA,UACb,GAAA,EAAK,KAAA;AAAA,UACL,WAAA,EAAa,KAAA;AAAA,UACb,UAAA,sBAAgB,GAAA;AAAY,SAChC;AACA,QAAA,YAAA,CAAa,GAAA,CAAI,WAAA,CAAY,aAAA,EAAe,cAAc,CAAA;AAAA,MAC9D;AAEA,MAAA,IAAI,wBAAA,CAAyB,WAAW,CAAA,EAAG;AACvC,QAAA,cAAA,CAAe,GAAA,GAAM,IAAA;AAAA,MACzB,CAAA,MAAA,IAAW,0BAAA,CAA2B,WAAW,CAAA,EAAG;AAChD,QAAA,IAAI,WAAA,CAAY,aAAa,IAAA,EAAM;AAC/B,UAAA,cAAA,CAAe,WAAA,GAAc,IAAA;AAAA,QACjC,CAAA,MAAO;AACH,UAAA,cAAA,CAAe,UAAA,CAAW,GAAA,CAAI,WAAA,CAAY,SAAS,CAAA;AAAA,QACvD;AAAA,MACJ,CAAA,MAAO;AACH,QAAA,MAAM,IAAI,KAAA,CAAM,OAAA,CAAQ,QAAA,EAAU,CAAA,+BAAA,CAAiC,CAAA;AAAA,MACvE;AAAA,IACJ;AACA,IAAA,KAAA,CAAM,GAAA,CAAI,aAAa,YAAY,CAAA;AAAA,EACvC;AACA,EAAA,OAAO,KAAA;AACX;;"}
1
+ {"version":3,"file":"ServiceLayer.js","sources":["ServiceLayer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createLogger, Error } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { RUNTIME_PACKAGE_NAME } from \"../builtin-services\";\nimport { ErrorId } from \"../errors\";\nimport { ReferenceMeta } from \"../Service\";\nimport {\n InterfaceSpec,\n isAllImplementationsSpec,\n isSingleImplementationSpec,\n ReferenceSpec,\n renderInterfaceSpec\n} from \"./InterfaceSpec\";\nimport { PackageRepr } from \"./PackageRepr\";\nimport { ReadonlyServiceLookup, ServiceLookupResult, ServicesLookupResult } from \"./ServiceLookup\";\nimport { ServiceRepr } from \"./ServiceRepr\";\nimport {\n ComputedServiceDependencies,\n FrameworkDependency,\n UIDependency,\n verifyDependencies\n} from \"./verifyDependencies\";\n\nconst LOG = createLogger(sourceId);\n\nexport type DynamicLookupResult = ServiceLookupResult | UndeclaredDependency;\n\n/**\n * Returned when a dependency is not declared in the package's metadata.\n */\nexport interface UndeclaredDependency {\n type: \"undeclared\";\n}\n\n/**\n * Returned when a package is not found in the application's metadata.\n */\nexport interface UnknownPackage {\n type: \"unknown-package\";\n}\n\nconst UNDECLARED_DEPENDENCY = { type: \"undeclared\" } satisfies UndeclaredDependency;\nconst UNKNOWN_PACKAGE = { type: \"unknown-package\" } satisfies UnknownPackage;\n\ninterface DependencyDeclarations {\n all: boolean;\n unqualified: boolean;\n qualifiers: Set<string>;\n}\n\nexport class ServiceLayer {\n // All services in the application.\n #allServices: ServiceRepr[];\n\n // Set of required services. These services and their dependencies will be started.\n #requiredServices: Set<ServiceRepr>;\n\n #serviceLookup: ReadonlyServiceLookup;\n\n // Service id --> Service dependencies\n #serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n // Package name --> Interface name --> Declarations\n #declaredDependencies;\n #state: \"not-started\" | \"started\" | \"destroyed\" = \"not-started\";\n\n /**\n * Constructs a new service layer instance.\n * Requires the application's set of packages (from which services are taken)\n * and an optional set of forced references (interfaces whose implementing services will be started unconditionally).\n *\n * In its current form, the service layer will start only forced references and references needed by the UI (and their dependencies).\n */\n constructor(packages: readonly PackageRepr[], forcedReferences: ReferenceSpec[] = []) {\n const allServices = packages.map((pkg) => pkg.services).flat();\n const uiDependencies = packages\n .map((pkg) =>\n pkg.uiReferences.map<UIDependency>((dep) => {\n return {\n type: \"ui\",\n packageName: pkg.name,\n ...dep\n };\n })\n )\n .flat();\n const requiredReferences = [\n ...forcedReferences.map<FrameworkDependency>((r) => ({\n type: \"framework\",\n ...r\n })),\n ...uiDependencies\n ];\n const { serviceLookup, serviceDependencies } = verifyDependencies({\n services: allServices,\n requiredReferences: requiredReferences\n });\n\n this.#allServices = allServices;\n this.#requiredServices = getRequiredServices(requiredReferences, serviceLookup);\n this.#serviceLookup = serviceLookup;\n this.#serviceDependencies = serviceDependencies;\n this.#declaredDependencies = buildDependencyIndex(packages);\n }\n\n destroy() {\n for (const service of this.#requiredServices) {\n this.#destroyService(service);\n }\n this.#state = \"destroyed\";\n }\n\n start() {\n if (this.#state !== \"not-started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer was already started.\");\n }\n\n for (const service of this.#requiredServices) {\n this.#createService(service);\n }\n this.#state = \"started\";\n\n // Give warnings for unneeded services during development.\n if (import.meta.env.DEV) {\n const unneededServices = this.#allServices\n .filter(\n (service) =>\n service.packageName !== RUNTIME_PACKAGE_NAME &&\n service.state === \"not-constructed\"\n )\n .map((service) => `'${service.id}'`);\n if (unneededServices.length) {\n const services = unneededServices.join(\", \");\n LOG.warn(\n `The following services are contained in the application but were not started: ${services}.`\n );\n }\n }\n }\n\n /**\n * Returns a service implementing the given interface.\n * Checks that the given package actually declared a dependency on that interface\n * to enforce coding guidelines.\n *\n * @param packageName the name of the package requesting the import\n * @param spec the interface specifier\n * @param options advanced options to customize lookup\n * @throws if the service layer is not in 'started' state.\n * Returns `{ type: \"unimplemented\" }` when no service implements the interface.\n */\n getService(\n packageName: string,\n spec: InterfaceSpec,\n options?: { ignoreDeclarationCheck?: boolean }\n ): ServiceLookupResult | UndeclaredDependency | UnknownPackage {\n if (this.#state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n if (!options?.ignoreDeclarationCheck) {\n const checkError = this.#checkDependency(packageName, spec);\n if (checkError) {\n return checkError;\n }\n }\n\n return this.#serviceLookup.lookupOne(spec);\n }\n\n /**\n * Returns all services implementing the given interface.\n * Requires that the dependency on all implementations has been declared in the package.\n *\n * @param packageName the name of the package requesting the import\n * @param interfaceName the interface name\n *\n * @throws if the service layer is not in 'started' state.\n */\n getServices(\n packageName: string,\n interfaceName: string\n ): ServicesLookupResult | UndeclaredDependency | UnknownPackage {\n if (this.#state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n const checkError = this.#checkDependency(packageName, { interfaceName, all: true });\n if (checkError) {\n return checkError;\n }\n return this.#serviceLookup.lookupAll(interfaceName);\n }\n\n /**\n * Initializes the given service and its dependencies.\n * Dependencies are initialized before the service that requires them.\n */\n #createService(service: ServiceRepr) {\n if (service.state === \"constructed\") {\n const instance = service.getInstanceOrThrow();\n service.addRef();\n return instance;\n }\n if (service.state === \"constructing\") {\n throw new Error(ErrorId.INTERNAL, \"Cycle during service construction.\");\n }\n if (service.state !== \"not-constructed\") {\n throw new Error(ErrorId.INTERNAL, \"Invalid service state.\");\n }\n\n const references: Record<string, unknown> = {};\n const referencesMeta: Record<string, ReferenceMeta | ReferenceMeta[]> = {};\n\n // Sets state to 'constructing' to detect cycles\n service.beforeCreate();\n\n // Initialize dependencies recursively before creating the current service.\n for (const [referenceName, serviceDeps] of Object.entries(this.#getServiceDeps(service))) {\n const [referenceValue, referenceMeta] = this.#getReference(serviceDeps);\n references[referenceName] = referenceValue;\n referencesMeta[referenceName] = referenceMeta;\n }\n\n // Sets state to 'constructed' to finish the state transition, useCount is 1.\n LOG.debug(`Creating service '${service.id}'.`);\n return service.create({ references, referencesMeta });\n }\n\n /**\n * Destroys the given service and its dependencies.\n * The dependencies are destroyed after the service.\n */\n #destroyService(service: ServiceRepr) {\n if (service.state === \"destroyed\") {\n return;\n }\n\n // Destroy the service before its dependencies (reverse order\n // compared to construction).\n if (service.removeRef() <= 0) {\n LOG.debug(`Destroying service '${service.id}'`);\n service.destroy();\n\n for (const serviceDeps of Object.values(this.#getServiceDeps(service))) {\n if (Array.isArray(serviceDeps)) {\n for (const dep of serviceDeps) {\n this.#destroyService(dep);\n }\n } else {\n this.#destroyService(serviceDeps);\n }\n }\n }\n }\n\n /** Undefined -> everything is okay */\n #checkDependency(\n packageName: string,\n spec: ReferenceSpec\n ): UnknownPackage | UndeclaredDependency | undefined {\n const packageEntry = this.#declaredDependencies.get(packageName);\n if (!packageEntry) {\n return UNKNOWN_PACKAGE;\n }\n\n const interfaceEntry = packageEntry.get(spec.interfaceName);\n if (!interfaceEntry) {\n return UNDECLARED_DEPENDENCY;\n }\n\n if (isSingleImplementationSpec(spec)) {\n if (spec.qualifier == null) {\n return interfaceEntry.unqualified ? undefined : UNDECLARED_DEPENDENCY;\n }\n return interfaceEntry.qualifiers.has(spec.qualifier)\n ? undefined\n : UNDECLARED_DEPENDENCY;\n } else {\n return interfaceEntry.all ? undefined : UNDECLARED_DEPENDENCY;\n }\n }\n\n #getReference(ref: ServiceRepr): [unknown, ReferenceMeta];\n #getReference(ref: ServiceRepr[]): [unknown[], ReferenceMeta[]];\n #getReference(\n ref: ServiceRepr | ServiceRepr[]\n ): [unknown, ReferenceMeta] | [unknown[], ReferenceMeta[]];\n #getReference(\n ref: ServiceRepr | ServiceRepr[]\n ): [unknown, ReferenceMeta] | [unknown[], ReferenceMeta[]] {\n if (Array.isArray(ref)) {\n const referenceValues: unknown[] = [];\n const referenceMeta: ReferenceMeta[] = [];\n for (const d of ref) {\n const [value, meta] = this.#getReference(d);\n referenceValues.push(value);\n referenceMeta.push(meta);\n }\n return [referenceValues, referenceMeta];\n }\n\n const referenceValue = this.#createService(ref);\n const referenceMeta: ReferenceMeta = {\n serviceId: ref.id\n };\n return [referenceValue, referenceMeta];\n }\n\n #getServiceDeps(service: ServiceRepr) {\n const dependencies = this.#serviceDependencies.get(service.id);\n if (!dependencies) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find precomputed service dependencies for '${service.id}'.`\n );\n }\n return dependencies;\n }\n}\n\nfunction getRequiredServices(\n requiredReferences: readonly ReferenceSpec[],\n serviceLookup: ReadonlyServiceLookup\n): Set<ServiceRepr> {\n const requiredServices = new Set<ServiceRepr>();\n for (const referenceSpec of requiredReferences) {\n const result = serviceLookup.lookup(referenceSpec);\n if (result.type !== \"found\") {\n // Should be caught by verifier\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find required reference to ${renderInterfaceSpec(referenceSpec)}.`\n );\n }\n if (Array.isArray(result.value)) {\n for (const service of result.value) {\n requiredServices.add(service);\n }\n } else {\n requiredServices.add(result.value);\n }\n }\n return requiredServices;\n}\n\nfunction buildDependencyIndex(packages: readonly PackageRepr[]) {\n // Register declared UI dependencies.\n // This is needed as a lookup structure to that dynamic service lookups\n // can be validated at runtime.\n const index = new Map<string, Map<string, DependencyDeclarations>>();\n for (const pkg of packages) {\n const packageName = pkg.name;\n const packageEntry = new Map<string, DependencyDeclarations>();\n for (const uiReference of pkg.uiReferences) {\n let interfaceEntry = packageEntry.get(uiReference.interfaceName);\n if (!interfaceEntry) {\n interfaceEntry = {\n all: false,\n unqualified: false,\n qualifiers: new Set<string>()\n };\n packageEntry.set(uiReference.interfaceName, interfaceEntry);\n }\n\n if (isAllImplementationsSpec(uiReference)) {\n interfaceEntry.all = true;\n } else if (isSingleImplementationSpec(uiReference)) {\n if (uiReference.qualifier == null) {\n interfaceEntry.unqualified = true;\n } else {\n interfaceEntry.qualifiers.add(uiReference.qualifier);\n }\n } else {\n throw new Error(ErrorId.INTERNAL, `Unexpected implementation spec.`);\n }\n }\n index.set(packageName, packageEntry);\n }\n return index;\n}\n"],"names":["referenceMeta"],"mappings":";;;;;;;AAyBA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAkBjC,MAAM,qBAAA,GAAwB,EAAE,IAAA,EAAM,YAAA,EAAa;AACnD,MAAM,eAAA,GAAkB,EAAE,IAAA,EAAM,iBAAA,EAAkB;AAQ3C,MAAM,YAAA,CAAa;AA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E;AAAA,IACJ;AACA,IAAA,KAAA,CAAM,GAAA,CAAI,aAAa,YAAY,CAAA;AAAA,EACvC;AACA,EAAA,OAAO,KAAA;AACX;;"}
@@ -1,6 +1,6 @@
1
1
  import { InterfaceSpec, ReferenceSpec } from "./InterfaceSpec";
2
2
  import { ServiceRepr } from "./ServiceRepr";
3
- export type ReadonlyServiceLookup = Pick<ServiceLookup, "lookup" | "lookupOne" | "lookupAll" | "serviceCount">;
3
+ export type ReadonlyServiceLookup = Pick<ServiceLookup, "lookup" | "lookupOne" | "lookupAll" | "registrationCount">;
4
4
  export interface Unimplemented {
5
5
  type: "unimplemented";
6
6
  }
@@ -18,9 +18,10 @@ export type ServicesLookupResult = Found<ServiceRepr[]>;
18
18
  export declare class ServiceLookup {
19
19
  #private;
20
20
  /**
21
- * Returns the total number of registered services.
21
+ * Returns the number of (service, interface) registrations.
22
+ * A service providing two interfaces counts twice.
22
23
  */
23
- get serviceCount(): number;
24
+ get registrationCount(): number;
24
25
  /**
25
26
  * Attempts to register the given service as an implementation of `interfaceName`.
26
27
  * Throws if the registration did not complete successfully.
@@ -5,12 +5,12 @@ import { isSingleImplementationSpec } from './InterfaceSpec.js';
5
5
  class ServiceLookup {
6
6
  // Service implementations indexed by interface name.
7
7
  #services = /* @__PURE__ */ new Map();
8
- // Total service count
9
8
  #count = 0;
10
9
  /**
11
- * Returns the total number of registered services.
10
+ * Returns the number of (service, interface) registrations.
11
+ * A service providing two interfaces counts twice.
12
12
  */
13
- get serviceCount() {
13
+ get registrationCount() {
14
14
  return this.#count;
15
15
  }
16
16
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceLookup.js","sources":["ServiceLookup.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, isSingleImplementationSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\ninterface Services {\n // The unqualified service implementations (if any).\n unqualified: ServiceRepr[];\n\n // Services indexed by their qualifier (if any).\n byQualifier: Map<string, ServiceRepr>;\n}\n\nexport type ReadonlyServiceLookup = Pick<\n ServiceLookup,\n \"lookup\" | \"lookupOne\" | \"lookupAll\" | \"serviceCount\"\n>;\n\nexport interface Unimplemented {\n type: \"unimplemented\";\n}\n\nexport interface Ambiguous {\n type: \"ambiguous\";\n choices: AmbiguousChoice[];\n}\n\nexport type AmbiguousChoice = [serviceId: string, qualifier: string | undefined];\n\nexport interface Found<T> {\n type: \"found\";\n value: T;\n}\n\nexport type ServiceLookupResult = Unimplemented | Ambiguous | Found<ServiceRepr>;\n\nexport type ServicesLookupResult = Found<ServiceRepr[]>;\n\nexport class ServiceLookup {\n // Service implementations indexed by interface name.\n #services = new Map<string, Services>();\n\n // Total service count\n #count = 0;\n\n /**\n * Returns the total number of registered services.\n */\n get serviceCount() {\n return this.#count;\n }\n\n /**\n * Attempts to register the given service as an implementation of `interfaceName`.\n * Throws if the registration did not complete successfully.\n *\n * All services should be registered before any lookups are attempted.\n */\n register(service: ServiceRepr, { interfaceName, qualifier }: InterfaceSpec): void {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' provides invalid interface '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' uses an invalid qualifier for interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.#ensureInterfaceEntry(interfaceName);\n if (!qualifier) {\n services.unqualified.push(service);\n ++this.#count;\n return;\n }\n\n const existingQualified = services.byQualifier.get(qualifier);\n if (existingQualified) {\n throw new Error(\n ErrorId.DUPLICATE_INTERFACE,\n `Cannot register '${service.id}' as interface '${interfaceName}' with qualifier '${qualifier}'.` +\n ` The interface is already provided by service '${existingQualified.id}'.` +\n ` You can choose a different qualifier or remove the service to resolve the collision.`\n );\n }\n services.byQualifier.set(qualifier, service);\n ++this.#count;\n }\n\n /**\n * Attempts to find the service(s) required by the given reference spec.\n */\n lookup(ref: ReferenceSpec): ServiceLookupResult | ServicesLookupResult {\n if (isSingleImplementationSpec(ref)) {\n return this.lookupOne(ref);\n } else {\n return this.lookupAll(ref.interfaceName);\n }\n }\n\n /**\n * Attempts to find a service implementing the given interface.\n */\n lookupOne({ interfaceName, qualifier }: InterfaceSpec): ServiceLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid qualifier during service lookup of interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.#services.get(interfaceName);\n if (!services) {\n return { type: \"unimplemented\" };\n }\n\n if (!qualifier) {\n // Just pick one, but the choice must be unambiguous.\n const count = services.unqualified.length + services.byQualifier.size;\n if (count > 1) {\n // [serviceId, qualifier] tuples\n const choices: AmbiguousChoice[] = [];\n choices.push(\n ...Array.from(services.byQualifier.entries()).map<AmbiguousChoice>(\n ([qualifier, service]) => [service.id, qualifier]\n )\n );\n choices.push(\n ...services.unqualified.map<AmbiguousChoice>((s) => [s.id, undefined])\n );\n return {\n type: \"ambiguous\",\n choices: choices\n };\n }\n\n const service = services.unqualified[0] ?? first(services.byQualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n const service = services.byQualifier.get(qualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n /**\n * Returns all services implementing the given interface.\n */\n lookupAll(interfaceName: string): ServicesLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n\n const services = this.#services.get(interfaceName);\n if (!services) {\n return { type: \"found\", value: [] };\n }\n\n const deduped = new Set([...services.unqualified, ...services.byQualifier.values()]);\n return {\n type: \"found\",\n value: Array.from(deduped)\n };\n }\n\n #ensureInterfaceEntry(interfaceName: string): Services {\n const services = this.#services;\n let entry = services.get(interfaceName);\n if (!entry) {\n entry = {\n unqualified: [],\n byQualifier: new Map()\n };\n services.set(interfaceName, entry);\n }\n return entry;\n }\n}\n\nexport function renderAmbiguousServiceChoices(choices: AmbiguousChoice[], max = 3): string {\n let message = \"\";\n let count = 0;\n for (const [serviceId, qualifier] of choices) {\n if (count) {\n message += \", \";\n }\n\n message += `'${serviceId}'`;\n if (qualifier) {\n message += ` (with qualifier '${qualifier}')`;\n }\n\n if (++count >= max) {\n break;\n }\n }\n\n const remaining = choices.length - count;\n if (remaining > 0) {\n message += ` and ${remaining} more`;\n }\n return message;\n}\n\nfunction first<K, V>(map: Map<K, V>): V | undefined {\n return map.values().next().value;\n}\n"],"names":["qualifier","service"],"mappings":";;;;AAyCO,MAAM,aAAA,CAAc;AAAA;AAAA,EAEvB,SAAA,uBAAgB,GAAA,EAAsB;AAAA;AAAA,EAGtC,MAAA,GAAS,CAAA;AAAA;AAAA;AAAA;AAAA,EAKT,IAAI,YAAA,GAAe;AACf,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAA,CAAS,OAAA,EAAsB,EAAE,aAAA,EAAe,WAAU,EAAwB;AAC9E,IAAA,IAAI,CAAC,aAAA,EAAe;AAChB,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAA,CAAQ,gBAAA;AAAA,QACR,CAAA,SAAA,EAAY,OAAA,CAAQ,EAAE,CAAA,8BAAA,EAAiC,aAAa,CAAA,EAAA;AAAA,OACxE;AAAA,IACJ;AACA,IAAA,IAAI,SAAA,IAAa,IAAA,IAAQ,CAAC,SAAA,EAAW;AACjC,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAA,CAAQ,gBAAA;AA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,IAAI,CAAC,KAAA,EAAO;AACR,MAAA,KAAA,GAAQ;AAAA,QACJ,aAAa,EAAC;AAAA,QACd,WAAA,sBAAiB,GAAA;AAAI,OACzB;AACA,MAAA,QAAA,CAAS,GAAA,CAAI,eAAe,KAAK,CAAA;AAAA,IACrC;AACA,IAAA,OAAO,KAAA;AAAA,EACX;AACJ;AAEO,SAAS,6BAAA,CAA8B,OAAA,EAA4B,GAAA,GAAM,CAAA,EAAW;AACvF,EAAA,IAAI,OAAA,GAAU,EAAA;AACd,EAAA,IAAI,KAAA,GAAQ,CAAA;AACZ,EAAA,KAAA,MAAW,CAAC,SAAA,EAAW,SAAS,CAAA,IAAK,OAAA,EAAS;AAC1C,IAAA,IAAI,KAAA,EAAO;AACP,MAAA,OAAA,IAAW,IAAA;AAAA,IACf;AAEA,IAAA,OAAA,IAAW,IAAI,SAAS,CAAA,CAAA,CAAA;AACxB,IAAA,IAAI,SAAA,EAAW;AACX,MAAA,OAAA,IAAW,qBAAqB,SAAS,CAAA,EAAA,CAAA;AAAA,IAC7C;AAEA,IAAA,IAAI,EAAE,SAAS,GAAA,EAAK;AAChB,MAAA;AAAA,IACJ;AAAA,EACJ;AAEA,EAAA,MAAM,SAAA,GAAY,QAAQ,MAAA,GAAS,KAAA;AACnC,EAAA,IAAI,YAAY,CAAA,EAAG;AACf,IAAA,OAAA,IAAW,QAAQ,SAAS,CAAA,KAAA,CAAA;AAAA,EAChC;AACA,EAAA,OAAO,OAAA;AACX;AAEA,SAAS,MAAY,GAAA,EAA+B;AAChD,EAAA,OAAO,GAAA,CAAI,MAAA,EAAO,CAAE,IAAA,EAAK,CAAE,KAAA;AAC/B;;"}
1
+ {"version":3,"file":"ServiceLookup.js","sources":["ServiceLookup.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, isSingleImplementationSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\ninterface Services {\n // The unqualified service implementations (if any).\n unqualified: ServiceRepr[];\n\n // Services indexed by their qualifier (if any).\n byQualifier: Map<string, ServiceRepr>;\n}\n\nexport type ReadonlyServiceLookup = Pick<\n ServiceLookup,\n \"lookup\" | \"lookupOne\" | \"lookupAll\" | \"registrationCount\"\n>;\n\nexport interface Unimplemented {\n type: \"unimplemented\";\n}\n\nexport interface Ambiguous {\n type: \"ambiguous\";\n choices: AmbiguousChoice[];\n}\n\nexport type AmbiguousChoice = [serviceId: string, qualifier: string | undefined];\n\nexport interface Found<T> {\n type: \"found\";\n value: T;\n}\n\nexport type ServiceLookupResult = Unimplemented | Ambiguous | Found<ServiceRepr>;\n\nexport type ServicesLookupResult = Found<ServiceRepr[]>;\n\nexport class ServiceLookup {\n // Service implementations indexed by interface name.\n #services = new Map<string, Services>();\n\n #count = 0;\n\n /**\n * Returns the number of (service, interface) registrations.\n * A service providing two interfaces counts twice.\n */\n get registrationCount() {\n return this.#count;\n }\n\n /**\n * Attempts to register the given service as an implementation of `interfaceName`.\n * Throws if the registration did not complete successfully.\n *\n * All services should be registered before any lookups are attempted.\n */\n register(service: ServiceRepr, { interfaceName, qualifier }: InterfaceSpec): void {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' provides invalid interface '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' uses an invalid qualifier for interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.#ensureInterfaceEntry(interfaceName);\n if (!qualifier) {\n services.unqualified.push(service);\n ++this.#count;\n return;\n }\n\n const existingQualified = services.byQualifier.get(qualifier);\n if (existingQualified) {\n throw new Error(\n ErrorId.DUPLICATE_INTERFACE,\n `Cannot register '${service.id}' as interface '${interfaceName}' with qualifier '${qualifier}'.` +\n ` The interface is already provided by service '${existingQualified.id}'.` +\n ` You can choose a different qualifier or remove the service to resolve the collision.`\n );\n }\n services.byQualifier.set(qualifier, service);\n ++this.#count;\n }\n\n /**\n * Attempts to find the service(s) required by the given reference spec.\n */\n lookup(ref: ReferenceSpec): ServiceLookupResult | ServicesLookupResult {\n if (isSingleImplementationSpec(ref)) {\n return this.lookupOne(ref);\n } else {\n return this.lookupAll(ref.interfaceName);\n }\n }\n\n /**\n * Attempts to find a service implementing the given interface.\n */\n lookupOne({ interfaceName, qualifier }: InterfaceSpec): ServiceLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid qualifier during service lookup of interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.#services.get(interfaceName);\n if (!services) {\n return { type: \"unimplemented\" };\n }\n\n if (!qualifier) {\n // Just pick one, but the choice must be unambiguous.\n const count = services.unqualified.length + services.byQualifier.size;\n if (count > 1) {\n // [serviceId, qualifier] tuples\n const choices: AmbiguousChoice[] = [];\n choices.push(\n ...Array.from(services.byQualifier.entries()).map<AmbiguousChoice>(\n ([qualifier, service]) => [service.id, qualifier]\n )\n );\n choices.push(\n ...services.unqualified.map<AmbiguousChoice>((s) => [s.id, undefined])\n );\n return {\n type: \"ambiguous\",\n choices: choices\n };\n }\n\n const service = services.unqualified[0] ?? first(services.byQualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n const service = services.byQualifier.get(qualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n /**\n * Returns all services implementing the given interface.\n */\n lookupAll(interfaceName: string): ServicesLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n\n const services = this.#services.get(interfaceName);\n if (!services) {\n return { type: \"found\", value: [] };\n }\n\n const deduped = new Set([...services.unqualified, ...services.byQualifier.values()]);\n return {\n type: \"found\",\n value: Array.from(deduped)\n };\n }\n\n #ensureInterfaceEntry(interfaceName: string): Services {\n const services = this.#services;\n let entry = services.get(interfaceName);\n if (!entry) {\n entry = {\n unqualified: [],\n byQualifier: new Map()\n };\n services.set(interfaceName, entry);\n }\n return entry;\n }\n}\n\nexport function renderAmbiguousServiceChoices(choices: AmbiguousChoice[], max = 3): string {\n let message = \"\";\n let count = 0;\n for (const [serviceId, qualifier] of choices) {\n if (count) {\n message += \", \";\n }\n\n message += `'${serviceId}'`;\n if (qualifier) {\n message += ` (with qualifier '${qualifier}')`;\n }\n\n if (++count >= max) {\n break;\n }\n }\n\n const remaining = choices.length - count;\n if (remaining > 0) {\n message += ` and ${remaining} more`;\n }\n return message;\n}\n\nfunction first<K, V>(map: Map<K, V>): V | undefined {\n return map.values().next().value;\n}\n"],"names":["qualifier","service"],"mappings":";;;;AAyCO,MAAM,aAAA,CAAc;AAAA;AAAA,EAEvB,SAAA,uBAAgB,GAAA,EAAsB;AAAA,EAEtC,MAAA,GAAS,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMT,IAAI,iBAAA,GAAoB;AACpB,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAA,CAAS,OAAA,EAAsB,EAAE,aAAA,EAAe,WAAU,EAAwB;AAC9E,IAAA,IAAI,CAAC,aAAA,EAAe;AAChB,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAA,CAAQ,gBAAA;AAAA,QACR,CAAA,SAAA,EAAY,OAAA,CAAQ,EAAE,CAAA,8BAAA,EAAiC,aAAa,CAAA,EAAA;AAAA,OACxE;AAAA,IACJ;AACA,IAAA,IAAI,SAAA,IAAa,IAAA,IAAQ,CAAC,SAAA,EAAW;AACjC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AAA,GAAQ,QAAA,CAAS,GAAA,CAAI,aAAa,CAAA;AACtC,IAAA,IAAI,CAAC,KAAA,EAAO;AACR,MAAA,KAAA,GAAQ;AAAA,QACJ,aAAa,EAAC;AAAA,QACd,WAAA,sBAAiB,GAAA;AAAI,OACzB;AACA,MAAA,QAAA,CAAS,GAAA,CAAI,eAAe,KAAK,CAAA;AAAA,IACrC;AACA,IAAA,OAAO,KAAA;AAAA,EACX;AACJ;AAEO,SAAS,6BAAA,CAA8B,OAAA,EAA4B,GAAA,GAAM,CAAA,EAAW;AACvF,EAAA,IAAI,OAAA,GAAU,EAAA;AACd,EAAA,IAAI,KAAA,GAAQ,CAAA;AACZ,EAAA,KAAA,MAAW,CAAC,SAAA,EAAW,SAAS,CAAA,IAAK,OAAA,EAAS;AAC1C,IAAA,IAAI,KAAA,EAAO;AACP,MAAA,OAAA,IAAW,IAAA;AAAA,IACf;AAEA,IAAA,OAAA,IAAW,IAAI,SAAS,CAAA,CAAA,CAAA;AACxB,IAAA,IAAI,SAAA,EAAW;AACX,MAAA,OAAA,IAAW,qBAAqB,SAAS,CAAA,EAAA,CAAA;AAAA,IAC7C;AAEA,IAAA,IAAI,EAAE,SAAS,GAAA,EAAK;AAChB,MAAA;AAAA,IACJ;AAAA,EACJ;AAEA,EAAA,MAAM,SAAA,GAAY,QAAQ,MAAA,GAAS,KAAA;AACnC,EAAA,IAAI,YAAY,CAAA,EAAG;AACf,IAAA,OAAA,IAAW,QAAQ,SAAS,CAAA,KAAA,CAAA;AAAA,EAChC;AACA,EAAA,OAAO,OAAA;AACX;AAEA,SAAS,MAAY,GAAA,EAA+B;AAChD,EAAA,OAAO,GAAA,CAAI,MAAA,EAAO,CAAE,IAAA,EAAK,CAAE,KAAA;AAC/B;;"}
@@ -29,11 +29,11 @@ export declare class ServiceRepr {
29
29
  static create(packageName: string, data: ServiceMetadata, intl: ReadonlyReactive<PackageIntl>, properties?: Record<string, unknown>): ServiceRepr;
30
30
  /** Unique id of this service. Contains the package name and the service name. */
31
31
  readonly id: string;
32
- /** Name of this service in it's package. */
32
+ /** Name of this service in its package. */
33
33
  readonly name: string;
34
34
  /** Name of the parent package. */
35
35
  readonly packageName: string;
36
- /** Locale-dependant i18n messages. */
36
+ /** Locale-dependent i18n messages. */
37
37
  readonly intl: ReadonlyReactive<PackageIntl>;
38
38
  /** Service properties made available via the service's constructor. */
39
39
  readonly properties: Readonly<Record<string, unknown>>;
@@ -1,10 +1,11 @@
1
1
  import { watchValue } from '@conterra/reactivity-core';
2
2
  import { createLogger, Error, destroyResource, deprecated } from '@open-pioneer/core';
3
+ import { sourceId$1 as sourceId } from '../_virtual/source-info.js';
3
4
  import { ErrorId } from '../errors.js';
4
5
  import { IS_SERVICE_FACTORY } from '../Service.js';
5
6
  import { parseReferenceSpec } from './InterfaceSpec.js';
6
7
 
7
- const LOG = createLogger("runtime:ServiceRepr");
8
+ const LOG = createLogger(sourceId);
8
9
  class ServiceRepr {
9
10
  static create(packageName, data, intl, properties) {
10
11
  const clazz = data.clazz;
@@ -36,11 +37,11 @@ class ServiceRepr {
36
37
  }
37
38
  /** Unique id of this service. Contains the package name and the service name. */
38
39
  id;
39
- /** Name of this service in it's package. */
40
+ /** Name of this service in its package. */
40
41
  name;
41
42
  /** Name of the parent package. */
42
43
  packageName;
43
- /** Locale-dependant i18n messages. */
44
+ /** Locale-dependent i18n messages. */
44
45
  intl;
45
46
  /** Service properties made available via the service's constructor. */
46
47
  properties;
@@ -135,7 +136,7 @@ class ServiceRepr {
135
136
  );
136
137
  }
137
138
  const intl = this.intl;
138
- const hmrState = this.#hmrState = /* @__PURE__ */ createHmrState(this.id, intl);
139
+ const hmrState = this.#hmrState = createHmrState(this.id, intl);
139
140
  try {
140
141
  const options = createServiceOptions({
141
142
  references,
@@ -152,6 +153,7 @@ class ServiceRepr {
152
153
  this.#useCount = 1;
153
154
  return this.#instance;
154
155
  } catch (e) {
156
+ this.#hmrState = destroyResource(hmrState);
155
157
  throw new Error(
156
158
  ErrorId.SERVICE_CONSTRUCTION_FAILED,
157
159
  `Failed to create service instance '${this.id}'.`,
@@ -286,7 +288,7 @@ function createIntlDeprecation(serviceId) {
286
288
  return deprecated({
287
289
  name: `ServiceOptions.intl (used by ${serviceId})`,
288
290
  packageName: "@open-pioneer/runtime",
289
- since: "4.6.0",
291
+ since: "v4.6.0",
290
292
  alternative: "use currentIntl instead and watch for changes where appropriate"
291
293
  });
292
294
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceRepr.js","sources":["ServiceRepr.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { ReadonlyReactive, watchValue } from \"@conterra/reactivity-core\";\nimport { createLogger, deprecated, destroyResource, Error, Resource } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { PackageIntl } from \"../i18n\";\nimport { ServiceMetadata } from \"../metadata\";\nimport {\n IS_SERVICE_FACTORY,\n MarkedServiceFactoryConstructor,\n Service,\n ServiceConstructor,\n ServiceFactory,\n ServiceOptions\n} from \"../Service\";\nimport { InterfaceSpec, parseReferenceSpec, ReferenceSpec } from \"./InterfaceSpec\";\n\nconst LOG = createLogger(\"runtime:ServiceRepr\");\n\nexport type ServiceState = \"not-constructed\" | \"constructing\" | \"constructed\" | \"destroyed\";\n\nexport type ServiceDependency = ReferenceSpec & { referenceName: string };\n\nexport interface ServiceInstanceFactory {\n create(options: ServiceOptions): Service;\n destroy(srv: Service): void;\n}\n\nexport interface ServiceReprOptions {\n name: string;\n packageName: string;\n factory: ServiceInstanceFactory;\n intl: ReadonlyReactive<PackageIntl>;\n dependencies?: ServiceDependency[];\n interfaces?: InterfaceSpec[];\n properties?: Record<string, unknown>;\n}\n\ninterface HmrState extends Resource {\n /** Called when usage of `serviceOptions.intl` is detected at runtime. */\n onIntlUsed(): void;\n}\n\n/**\n * Represents metadata and state of a service in the runtime.\n * `this.instance` is the actual service instance (when constructed).\n */\nexport class ServiceRepr {\n static create(\n packageName: string,\n data: ServiceMetadata,\n intl: ReadonlyReactive<PackageIntl>,\n properties?: Record<string, unknown>\n ): ServiceRepr {\n const clazz = data.clazz;\n const name = data.name;\n // oxlint-disable-next-line oxc/no-map-spread\n const dependencies = Object.entries(data.references ?? {}).map(\n ([name, referenceMetadata]) => {\n return {\n referenceName: name,\n ...parseReferenceSpec(referenceMetadata)\n };\n }\n );\n const interfaces = data.provides?.map<InterfaceSpec>((i) => {\n return {\n interfaceName: i.name,\n qualifier: i.qualifier\n };\n });\n const factory = createConstructorFactory(clazz);\n return new ServiceRepr({\n name,\n packageName,\n factory,\n intl,\n dependencies,\n interfaces,\n properties\n });\n }\n\n /** Unique id of this service. Contains the package name and the service name. */\n readonly id: string;\n\n /** Name of this service in it's package. */\n readonly name: string;\n\n /** Name of the parent package. */\n readonly packageName: string;\n\n /** Locale-dependant i18n messages. */\n readonly intl: ReadonlyReactive<PackageIntl>;\n\n /** Service properties made available via the service's constructor. */\n readonly properties: Readonly<Record<string, unknown>>;\n\n /** Dependencies required by the service constructor. */\n readonly dependencies: readonly ServiceDependency[];\n\n /** Interfaces provided by the service. */\n readonly interfaces: readonly Readonly<InterfaceSpec>[];\n\n /** Number of references to this service. */\n #useCount = 0;\n\n /** Service factory to construct an instance. */\n #factory: ServiceInstanceFactory;\n\n /** Current state of this service. \"constructed\" -> instance is available. */\n #state: ServiceState = \"not-constructed\";\n\n /** Service instance, once constructed. */\n #instance: Service | undefined = undefined;\n\n /** Used in dev mode only. */\n #hmrState: HmrState | undefined = undefined;\n\n /** Lazily initialized deprecation message for this service. */\n #intlDeprecation: (() => void) | undefined = undefined;\n\n constructor(options: ServiceReprOptions) {\n const {\n name,\n packageName,\n factory,\n intl,\n dependencies = [],\n interfaces = [],\n properties = {}\n } = options;\n if (!isValidServiceName(name)) {\n throw new Error(ErrorId.INTERNAL, `Invalid service name: '${name}'.`);\n }\n\n this.id = `${packageName}::${name}`;\n this.name = name;\n this.packageName = packageName;\n this.#factory = factory;\n this.intl = intl;\n this.dependencies = dependencies;\n this.interfaces = interfaces;\n this.properties = properties;\n }\n\n /** Returns the current service instance or undefined if the service has not been constructed. */\n get instance(): Service | undefined {\n return this.#instance;\n }\n\n /** Returns the current state of the service. */\n get state() {\n return this.#state;\n }\n\n /** Returns the current reference count. */\n get useCount() {\n return this.#useCount;\n }\n\n /**\n * Same as `instance`, but throws when the instance has not been constructed.\n */\n getInstanceOrThrow() {\n const instance = this.#instance;\n if (!instance) {\n throw new Error(ErrorId.INTERNAL, \"Expected service instance to be present.\");\n }\n return instance;\n }\n\n /**\n * Called before `create()` to place the service into the `constructing` state,\n * which is currently used to detect cycles.\n */\n beforeCreate() {\n if (this.#state === \"not-constructed\") {\n this.#state = \"constructing\";\n } else {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is already under construction.\"\n );\n }\n }\n\n /**\n * Instantiates the service by invoking the service constructor\n * with the given `options`.\n *\n * The service's use count is initialized to `1`, so every `create()`\n * should be paired with a `removeRef()`.\n *\n * `destroy()` can be invoked once the final `removeRef()` has returned zero.\n */\n create(references: Pick<ServiceOptions, \"references\" | \"referencesMeta\">) {\n if (this.#state !== \"constructing\" || this.instance !== undefined) {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is not being constructed.\"\n );\n }\n\n const intl = this.intl;\n const hmrState = (this.#hmrState = /*#__PURE__*/ createHmrState(this.id, intl));\n try {\n const options = createServiceOptions({\n references,\n properties: this.properties,\n currentIntl: this.intl,\n intlDeprecation: () => {\n const deprecated = (this.#intlDeprecation ??= createIntlDeprecation(this.id));\n deprecated();\n },\n hmrState\n });\n this.#instance = this.#factory.create(options);\n this.#state = \"constructed\";\n this.#useCount = 1;\n return this.#instance;\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_CONSTRUCTION_FAILED,\n `Failed to create service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n\n destroy() {\n if (this.#instance) {\n try {\n this.#factory.destroy(this.#instance);\n this.#hmrState = destroyResource(this.#hmrState);\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_DESTRUCTION_FAILED,\n `Failed to destroy service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n this.#instance = undefined;\n this.#state = \"destroyed\";\n }\n\n /**\n * Adds a use to the service's use count.\n * References to a service are tracked: it should only be destroyed when it is no longer being used.\n */\n addRef() {\n return (this.#useCount += 1);\n }\n\n /**\n * Removes a use from the service's use count.\n * Returns the new use count.\n */\n removeRef() {\n return (this.#useCount -= 1);\n }\n}\n\nexport function createConstructorFactory<T extends {}>(\n clazz: ServiceConstructor<T> | MarkedServiceFactoryConstructor<T>\n): ServiceInstanceFactory {\n // check if factory is a service factory constructor (has the IS_SERVICE_FACTORY symbol)\n if (IS_SERVICE_FACTORY in clazz) {\n return createFactoryForServiceFactory(clazz);\n }\n // Is a regular service constructor\n return {\n create(options: ServiceOptions<T>) {\n return new clazz(options);\n },\n destroy(instance: Service<T>) {\n instance.destroy?.();\n }\n };\n}\n\nexport function createFactoryForServiceFactory<T extends {}>(\n facClazz: MarkedServiceFactoryConstructor<T>\n): ServiceInstanceFactory {\n return new ServiceFactoryInstanceFactory(facClazz);\n}\n\nclass ServiceFactoryInstanceFactory<T extends {}> implements ServiceInstanceFactory {\n #fac: ServiceFactory | undefined = undefined;\n #facClazz: MarkedServiceFactoryConstructor<T>;\n\n constructor(facClazz: MarkedServiceFactoryConstructor<T>) {\n this.#facClazz = facClazz;\n }\n\n create(options: ServiceOptions<T>) {\n const fac = new this.#facClazz(options);\n const instance = fac.createService();\n this.#fac = fac;\n return instance;\n }\n\n destroy(srv: Service) {\n if (!this.#fac) {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service factory instance is not present.\"\n );\n }\n try {\n this.#fac.destroyService?.(srv);\n } finally {\n this.#fac = undefined;\n }\n }\n}\n\nexport function createFunctionFactory(\n create: (options: ServiceOptions) => Service\n): ServiceInstanceFactory {\n return {\n create,\n destroy(srv) {\n srv.destroy?.();\n }\n };\n}\n\n/**\n * Triggers a full page reload if:\n *\n * - We are in development mode (import.meta.hot) AND\n * - The service uses the deprecated `intl` option AND\n * - The messages changes due to a HMR event\n */\nfunction createHmrState(\n serviceId: string,\n intl: ReadonlyReactive<PackageIntl>\n): HmrState | undefined {\n let hmrState: HmrState | undefined;\n if (import.meta.hot) {\n let intlWatch: Resource | undefined = undefined;\n hmrState = {\n destroy() {\n intlWatch = destroyResource(intlWatch);\n },\n\n onIntlUsed() {\n if (intlWatch) {\n return;\n }\n\n intlWatch = watchValue(\n () => intl.value,\n (_newIntl, _, context) => {\n LOG.warn(\n `Service ${serviceId} has used the deprecated 'intl' option but messages have changed.` +\n ` Hot reloading of intl changes is only implemented ` +\n ` with 'currentIntl'.` +\n ` Triggering full reload.`\n );\n context.destroy();\n\n // TODO: Figure out a cleaner way to force vite to reload the application\n // https://github.com/open-pioneer/trails-core-packages/issues/164\n window.location.reload();\n }\n );\n }\n };\n }\n return hmrState;\n}\n\nfunction createServiceOptions(options: {\n references: Pick<ServiceOptions, \"references\" | \"referencesMeta\">;\n properties: Record<string, unknown>;\n currentIntl: ReadonlyReactive<PackageIntl>;\n intlDeprecation: () => void;\n hmrState: HmrState | undefined;\n}) {\n const { references, properties, currentIntl, intlDeprecation, hmrState } = options;\n return {\n ...references,\n properties,\n get intl() {\n intlDeprecation();\n if (import.meta.hot) {\n hmrState?.onIntlUsed();\n }\n return currentIntl.value;\n },\n currentIntl\n };\n}\n\nfunction createIntlDeprecation(serviceId: string) {\n return deprecated({\n name: `ServiceOptions.intl (used by ${serviceId})`,\n packageName: \"@open-pioneer/runtime\",\n since: \"4.6.0\",\n alternative: \"use currentIntl instead and watch for changes where appropriate\"\n });\n}\n\nconst SERVICE_NAME_REGEX = /^[a-z0-9_-]+$/i;\n\nfunction isValidServiceName(name: string) {\n return SERVICE_NAME_REGEX.test(name);\n}\n"],"names":["name","deprecated"],"mappings":";;;;;;AAkBA,MAAM,GAAA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B,gBAAA;AAE3B,SAAS,mBAAmB,IAAA,EAAc;AACtC,EAAA,OAAO,kBAAA,CAAmB,KAAK,IAAI,CAAA;AACvC;;"}
1
+ {"version":3,"file":"ServiceRepr.js","sources":["ServiceRepr.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { ReadonlyReactive, watchValue } from \"@conterra/reactivity-core\";\nimport { createLogger, deprecated, destroyResource, Error, Resource } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { ErrorId } from \"../errors\";\nimport { PackageIntl } from \"../i18n\";\nimport { ServiceMetadata } from \"../metadata\";\nimport {\n IS_SERVICE_FACTORY,\n MarkedServiceFactoryConstructor,\n Service,\n ServiceConstructor,\n ServiceFactory,\n ServiceOptions\n} from \"../Service\";\nimport { InterfaceSpec, parseReferenceSpec, ReferenceSpec } from \"./InterfaceSpec\";\n\nconst LOG = createLogger(sourceId);\n\nexport type ServiceState = \"not-constructed\" | \"constructing\" | \"constructed\" | \"destroyed\";\n\nexport type ServiceDependency = ReferenceSpec & { referenceName: string };\n\nexport interface ServiceInstanceFactory {\n create(options: ServiceOptions): Service;\n destroy(srv: Service): void;\n}\n\nexport interface ServiceReprOptions {\n name: string;\n packageName: string;\n factory: ServiceInstanceFactory;\n intl: ReadonlyReactive<PackageIntl>;\n dependencies?: ServiceDependency[];\n interfaces?: InterfaceSpec[];\n properties?: Record<string, unknown>;\n}\n\ninterface HmrState extends Resource {\n /** Called when usage of `serviceOptions.intl` is detected at runtime. */\n onIntlUsed(): void;\n}\n\n/**\n * Represents metadata and state of a service in the runtime.\n * `this.instance` is the actual service instance (when constructed).\n */\nexport class ServiceRepr {\n static create(\n packageName: string,\n data: ServiceMetadata,\n intl: ReadonlyReactive<PackageIntl>,\n properties?: Record<string, unknown>\n ): ServiceRepr {\n const clazz = data.clazz;\n const name = data.name;\n // oxlint-disable-next-line oxc/no-map-spread\n const dependencies = Object.entries(data.references ?? {}).map(\n ([name, referenceMetadata]) => {\n return {\n referenceName: name,\n ...parseReferenceSpec(referenceMetadata)\n };\n }\n );\n const interfaces = data.provides?.map<InterfaceSpec>((i) => {\n return {\n interfaceName: i.name,\n qualifier: i.qualifier\n };\n });\n const factory = createConstructorFactory(clazz);\n return new ServiceRepr({\n name,\n packageName,\n factory,\n intl,\n dependencies,\n interfaces,\n properties\n });\n }\n\n /** Unique id of this service. Contains the package name and the service name. */\n readonly id: string;\n\n /** Name of this service in its package. */\n readonly name: string;\n\n /** Name of the parent package. */\n readonly packageName: string;\n\n /** Locale-dependent i18n messages. */\n readonly intl: ReadonlyReactive<PackageIntl>;\n\n /** Service properties made available via the service's constructor. */\n readonly properties: Readonly<Record<string, unknown>>;\n\n /** Dependencies required by the service constructor. */\n readonly dependencies: readonly ServiceDependency[];\n\n /** Interfaces provided by the service. */\n readonly interfaces: readonly Readonly<InterfaceSpec>[];\n\n /** Number of references to this service. */\n #useCount = 0;\n\n /** Service factory to construct an instance. */\n #factory: ServiceInstanceFactory;\n\n /** Current state of this service. \"constructed\" -> instance is available. */\n #state: ServiceState = \"not-constructed\";\n\n /** Service instance, once constructed. */\n #instance: Service | undefined = undefined;\n\n /** Used in dev mode only. */\n #hmrState: HmrState | undefined = undefined;\n\n /** Lazily initialized deprecation message for this service. */\n #intlDeprecation: (() => void) | undefined = undefined;\n\n constructor(options: ServiceReprOptions) {\n const {\n name,\n packageName,\n factory,\n intl,\n dependencies = [],\n interfaces = [],\n properties = {}\n } = options;\n if (!isValidServiceName(name)) {\n throw new Error(ErrorId.INTERNAL, `Invalid service name: '${name}'.`);\n }\n\n this.id = `${packageName}::${name}`;\n this.name = name;\n this.packageName = packageName;\n this.#factory = factory;\n this.intl = intl;\n this.dependencies = dependencies;\n this.interfaces = interfaces;\n this.properties = properties;\n }\n\n /** Returns the current service instance or undefined if the service has not been constructed. */\n get instance(): Service | undefined {\n return this.#instance;\n }\n\n /** Returns the current state of the service. */\n get state() {\n return this.#state;\n }\n\n /** Returns the current reference count. */\n get useCount() {\n return this.#useCount;\n }\n\n /**\n * Same as `instance`, but throws when the instance has not been constructed.\n */\n getInstanceOrThrow() {\n const instance = this.#instance;\n if (!instance) {\n throw new Error(ErrorId.INTERNAL, \"Expected service instance to be present.\");\n }\n return instance;\n }\n\n /**\n * Called before `create()` to place the service into the `constructing` state,\n * which is currently used to detect cycles.\n */\n beforeCreate() {\n if (this.#state === \"not-constructed\") {\n this.#state = \"constructing\";\n } else {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is already under construction.\"\n );\n }\n }\n\n /**\n * Instantiates the service by invoking the service constructor\n * with the given `options`.\n *\n * The service's use count is initialized to `1`, so every `create()`\n * should be paired with a `removeRef()`.\n *\n * `destroy()` can be invoked once the final `removeRef()` has returned zero.\n */\n create(references: Pick<ServiceOptions, \"references\" | \"referencesMeta\">) {\n if (this.#state !== \"constructing\" || this.instance !== undefined) {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is not being constructed.\"\n );\n }\n\n const intl = this.intl;\n const hmrState = (this.#hmrState = createHmrState(this.id, intl));\n try {\n const options = createServiceOptions({\n references,\n properties: this.properties,\n currentIntl: this.intl,\n intlDeprecation: () => {\n const deprecated = (this.#intlDeprecation ??= createIntlDeprecation(this.id));\n deprecated();\n },\n hmrState\n });\n this.#instance = this.#factory.create(options);\n this.#state = \"constructed\";\n this.#useCount = 1;\n return this.#instance;\n } catch (e) {\n this.#hmrState = destroyResource(hmrState);\n throw new Error(\n ErrorId.SERVICE_CONSTRUCTION_FAILED,\n `Failed to create service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n\n destroy() {\n if (this.#instance) {\n try {\n this.#factory.destroy(this.#instance);\n this.#hmrState = destroyResource(this.#hmrState);\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_DESTRUCTION_FAILED,\n `Failed to destroy service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n this.#instance = undefined;\n this.#state = \"destroyed\";\n }\n\n /**\n * Adds a use to the service's use count.\n * References to a service are tracked: it should only be destroyed when it is no longer being used.\n */\n addRef() {\n return (this.#useCount += 1);\n }\n\n /**\n * Removes a use from the service's use count.\n * Returns the new use count.\n */\n removeRef() {\n return (this.#useCount -= 1);\n }\n}\n\nexport function createConstructorFactory<T extends {}>(\n clazz: ServiceConstructor<T> | MarkedServiceFactoryConstructor<T>\n): ServiceInstanceFactory {\n // check if factory is a service factory constructor (has the IS_SERVICE_FACTORY symbol)\n if (IS_SERVICE_FACTORY in clazz) {\n return createFactoryForServiceFactory(clazz);\n }\n // Is a regular service constructor\n return {\n create(options: ServiceOptions<T>) {\n return new clazz(options);\n },\n destroy(instance: Service<T>) {\n instance.destroy?.();\n }\n };\n}\n\nexport function createFactoryForServiceFactory<T extends {}>(\n facClazz: MarkedServiceFactoryConstructor<T>\n): ServiceInstanceFactory {\n return new ServiceFactoryInstanceFactory(facClazz);\n}\n\nclass ServiceFactoryInstanceFactory<T extends {}> implements ServiceInstanceFactory {\n #fac: ServiceFactory | undefined = undefined;\n #facClazz: MarkedServiceFactoryConstructor<T>;\n\n constructor(facClazz: MarkedServiceFactoryConstructor<T>) {\n this.#facClazz = facClazz;\n }\n\n create(options: ServiceOptions<T>) {\n const fac = new this.#facClazz(options);\n const instance = fac.createService();\n this.#fac = fac;\n return instance;\n }\n\n destroy(srv: Service) {\n if (!this.#fac) {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service factory instance is not present.\"\n );\n }\n try {\n this.#fac.destroyService?.(srv);\n } finally {\n this.#fac = undefined;\n }\n }\n}\n\nexport function createFunctionFactory(\n create: (options: ServiceOptions) => Service\n): ServiceInstanceFactory {\n return {\n create,\n destroy(srv) {\n srv.destroy?.();\n }\n };\n}\n\n/**\n * Triggers a full page reload if:\n *\n * - We are in development mode (import.meta.hot) AND\n * - The service uses the deprecated `intl` option AND\n * - The messages changes due to a HMR event\n */\nfunction createHmrState(\n serviceId: string,\n intl: ReadonlyReactive<PackageIntl>\n): HmrState | undefined {\n let hmrState: HmrState | undefined;\n if (import.meta.hot) {\n let intlWatch: Resource | undefined = undefined;\n hmrState = {\n destroy() {\n intlWatch = destroyResource(intlWatch);\n },\n\n onIntlUsed() {\n if (intlWatch) {\n return;\n }\n\n intlWatch = watchValue(\n () => intl.value,\n (_newIntl, _, context) => {\n LOG.warn(\n `Service ${serviceId} has used the deprecated 'intl' option but messages have changed.` +\n ` Hot reloading of intl changes is only implemented ` +\n ` with 'currentIntl'.` +\n ` Triggering full reload.`\n );\n context.destroy();\n\n // TODO: Figure out a cleaner way to force vite to reload the application\n // https://github.com/open-pioneer/trails-core-packages/issues/164\n window.location.reload();\n }\n );\n }\n };\n }\n return hmrState;\n}\n\nfunction createServiceOptions(options: {\n references: Pick<ServiceOptions, \"references\" | \"referencesMeta\">;\n properties: Record<string, unknown>;\n currentIntl: ReadonlyReactive<PackageIntl>;\n intlDeprecation: () => void;\n hmrState: HmrState | undefined;\n}) {\n const { references, properties, currentIntl, intlDeprecation, hmrState } = options;\n return {\n ...references,\n properties,\n get intl() {\n intlDeprecation();\n if (import.meta.hot) {\n hmrState?.onIntlUsed();\n }\n return currentIntl.value;\n },\n currentIntl\n };\n}\n\nfunction createIntlDeprecation(serviceId: string) {\n return deprecated({\n name: `ServiceOptions.intl (used by ${serviceId})`,\n packageName: \"@open-pioneer/runtime\",\n since: \"v4.6.0\",\n alternative: \"use currentIntl instead and watch for changes where appropriate\"\n });\n}\n\nconst SERVICE_NAME_REGEX = /^[a-z0-9_-]+$/i;\n\nfunction isValidServiceName(name: string) {\n return SERVICE_NAME_REGEX.test(name);\n}\n"],"names":["name","deprecated"],"mappings":";;;;;;;AAmBA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AA8B1B,MAAM,WAAA,CAAY;AAAA,EACrB,OAAO,MAAA,CACH,WAAA,EACA,IAAA,EACA,MACA,UAAA,EACW;AACX,IAAA,MAAM,QAAQ,I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AA;AACvC;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApplicationLifecycleEventService.js","sources":["ApplicationLifecycleEventService.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createLogger, Error } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { ApplicationLifecycleListener } from \"../api\";\nimport { ErrorId } from \"../errors\";\nimport { ServiceOptions } from \"../Service\";\n\nconst LOG = createLogger(sourceId);\n\ninterface References {\n listeners: ApplicationLifecycleListener[];\n}\n\nexport class ApplicationLifecycleEventService {\n #listeners: [serviceId: string, listener: ApplicationLifecycleListener][];\n\n constructor(options: ServiceOptions<References>) {\n const listeners = options.references.listeners;\n const listenersMeta = options.referencesMeta.listeners;\n this.#listeners = listeners.map((listener, index) => [\n // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion\n listenersMeta[index]!.serviceId,\n listener\n ]);\n }\n\n /** @internal */\n emitLifecycleEvent(event: \"after-start\" | \"before-stop\"): void {\n const methodName = (() => {\n switch (event) {\n case \"after-start\":\n return \"afterApplicationStart\";\n case \"before-stop\":\n return \"beforeApplicationStop\";\n default:\n throw new Error(ErrorId.INTERNAL, `Invalid event '${event}'`);\n }\n })();\n for (const [serviceId, listener] of this.#listeners) {\n try {\n listener[methodName]?.();\n } catch (e) {\n LOG.error(`Unexpected error from application lifecycle listener ${serviceId}`, e);\n }\n }\n }\n}\n"],"names":[],"mappings":";;;;AASA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAM1B,MAAM,gCAAA,CAAiC;AAAA,EAC1C,UAAA;AAAA,EAEA,YAAY,OAAA,EAAqC;AAC7C,IAAA,MAAM,SAAA,GAAY,QAAQ,UAAA,CAAW,SAAA;AACrC,IAAA,MAAM,aAAA,GAAgB,QAAQ,cAAA,CAAe,SAAA;AAC7C,IAAA,IAAA,CAAK,UAAA,GAAa,SAAA,CAAU,GAAA,CAAI,CAAC,UAAU,KAAA,KAAU;AAAA;AAAA,MAEjD,aAAA,CAAc,KAAK,CAAA,CAAG,SAAA;AAAA,MACtB;AAAA,KACH,CAAA;AAAA,EACL;AAAA;AAAA,EAGA,mBAAmB,KAAA,EAA4C;AAC3D,IAAA,MAAM,cAAc,MAAM;AACtB,MAAA,QAAQ,KAAA;AAAO,QACX,KAAK,aAAA;AACD,UAAA,OAAO,uBAAA;AAAA,QACX,KAAK,aAAA;AACD,UAAA,OAAO,uBAAA;AAAA,QACX;AACI,UAAA,MAAM,IAAI,KAAA,CAAM,OAAA,CAAQ,QAAA,EAAU,CAAA,eAAA,EAAkB,KAAK,CAAA,CAAA,CAAG,CAAA;AAAA;AACpE,IACJ,CAAA,GAAG;AACH,IAAA,KAAA,MAAW,CAAC,SAAA,EAAW,QAAQ,CAAA,IAAK,KAAK,UAAA,EAAY;AACjD,MAAA,IAAI;AACA,QAAA,QAAA,CAAS,UAAU,CAAA,IAAI;AAAA,MAC3B,SAAS,CAAA,EAAG;AACR,QAAA,GAAA,CAAI,KAAA,CAAM,CAAA,qDAAA,EAAwD,SAAS,CAAA,CAAA,EAAI,CAAC,CAAA;AAAA,MACpF;AAAA,IACJ;AAAA,EACJ;AACJ;;"}