@open-pioneer/runtime 0.1.0 → 0.1.2
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.
- package/CHANGELOG.md +18 -0
- package/CustomElement.d.ts +111 -0
- package/CustomElement.js.map +1 -1
- package/PropertiesRegistry.d.ts +19 -0
- package/Service.d.ts +63 -0
- package/ServiceRegistry.d.ts +25 -0
- package/api.d.ts +76 -0
- package/builtin-services/ApiServiceImpl.d.ts +11 -0
- package/builtin-services/ApiServiceImpl.js.map +1 -1
- package/builtin-services/ApplicationContextImpl.d.ts +18 -0
- package/builtin-services/ApplicationContextImpl.js.map +1 -1
- package/builtin-services/ApplicationLifecycleEventService.d.ts +12 -0
- package/builtin-services/ApplicationLifecycleEventService.js.map +1 -1
- package/builtin-services/index.d.ts +20 -0
- package/builtin-services/index.js.map +1 -1
- package/errors.d.ts +17 -0
- package/errors.js.map +1 -1
- package/i18n.d.ts +42 -0
- package/i18n.js.map +1 -1
- package/index.d.ts +6 -0
- package/metadata/ObservableBox.d.ts +23 -0
- package/metadata/ObservableBox.js.map +1 -1
- package/metadata/index.d.ts +115 -0
- package/package.json +9 -6
- package/react-integration/PackageContext.d.ts +11 -0
- package/react-integration/PackageContext.js.map +1 -1
- package/react-integration/ReactIntegration.d.ts +20 -0
- package/react-integration/ReactIntegration.js.map +1 -1
- package/react-integration/hooks.d.ts +46 -0
- package/react-integration/hooks.js.map +1 -1
- package/react-integration/index.d.ts +2 -0
- package/service-layer/InterfaceSpec.d.ts +22 -0
- package/service-layer/InterfaceSpec.js.map +1 -1
- package/service-layer/PackageRepr.d.ts +27 -0
- package/service-layer/PackageRepr.js.map +1 -1
- package/service-layer/ServiceLayer.d.ts +61 -0
- package/service-layer/ServiceLayer.js.map +1 -1
- package/service-layer/ServiceLookup.d.ts +46 -0
- package/service-layer/ServiceLookup.js.map +1 -1
- package/service-layer/ServiceRepr.d.ts +94 -0
- package/service-layer/ServiceRepr.js.map +1 -1
- package/service-layer/verifyDependencies.d.ts +42 -0
- package/service-layer/verifyDependencies.js.map +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { PackageRepr } from "./PackageRepr";
|
|
2
|
+
import { ServiceLookupResult, ServicesLookupResult } from "./ServiceLookup";
|
|
3
|
+
import { InterfaceSpec, ReferenceSpec } from "./InterfaceSpec";
|
|
4
|
+
export type DynamicLookupResult = ServiceLookupResult | UndeclaredDependency;
|
|
5
|
+
export interface UndeclaredDependency {
|
|
6
|
+
type: "undeclared";
|
|
7
|
+
}
|
|
8
|
+
export declare class ServiceLayer {
|
|
9
|
+
private allServices;
|
|
10
|
+
private requiredServices;
|
|
11
|
+
private serviceLookup;
|
|
12
|
+
private serviceDependencies;
|
|
13
|
+
private declaredDependencies;
|
|
14
|
+
private state;
|
|
15
|
+
/**
|
|
16
|
+
* Constructs a new service layer instance.
|
|
17
|
+
* Requires the application's set of packages (from which services are taken)
|
|
18
|
+
* and an optional set of forced references (interfaces whose implementing services will be started unconditionally).
|
|
19
|
+
*
|
|
20
|
+
* In its current form, the service layer will start only forced references and references needed by the UI (and their dependencies).
|
|
21
|
+
*/
|
|
22
|
+
constructor(packages: readonly PackageRepr[], forcedReferences?: ReferenceSpec[]);
|
|
23
|
+
destroy(): void;
|
|
24
|
+
start(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Returns a service implementing the given interface.
|
|
27
|
+
* Checks that the given package actually declared a dependency on that interface
|
|
28
|
+
* to enforce coding guidelines.
|
|
29
|
+
*
|
|
30
|
+
* @param packageName the name of the package requesting the import
|
|
31
|
+
* @param spec the interface specifier
|
|
32
|
+
* @param options advanced options to customize lookup
|
|
33
|
+
* @throws if the service layer is not in 'started' state or if no service implements the interface.
|
|
34
|
+
*/
|
|
35
|
+
getService(packageName: string, spec: InterfaceSpec, options?: {
|
|
36
|
+
ignoreDeclarationCheck?: boolean;
|
|
37
|
+
}): ServiceLookupResult | UndeclaredDependency;
|
|
38
|
+
/**
|
|
39
|
+
* Returns all services implementing the given interface.
|
|
40
|
+
* Requires that the dependency on all implementations has been declared in the package.
|
|
41
|
+
*
|
|
42
|
+
* @param packageName the name of the package requesting the import
|
|
43
|
+
* @param interfaceName the interface name
|
|
44
|
+
*
|
|
45
|
+
* @throws if the service layer is not in 'started'.
|
|
46
|
+
*/
|
|
47
|
+
getServices(packageName: string, interfaceName: string): ServicesLookupResult | UndeclaredDependency;
|
|
48
|
+
/**
|
|
49
|
+
* Initializes the given service and its dependencies.
|
|
50
|
+
* Dependencies are initialized before the service that requires them.
|
|
51
|
+
*/
|
|
52
|
+
private createService;
|
|
53
|
+
/**
|
|
54
|
+
* Destroys the given service and its dependencies.
|
|
55
|
+
* The dependencies are destroyed after the service.
|
|
56
|
+
*/
|
|
57
|
+
private destroyService;
|
|
58
|
+
private isDeclaredDependency;
|
|
59
|
+
private getReference;
|
|
60
|
+
private getServiceDeps;
|
|
61
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceLayer.js","sources":["open-pioneer://external-pioneer-packages/@open-pioneer/runtime/service-layer/ServiceLayer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { ServiceRepr } from \"./ServiceRepr\";\nimport { createLogger, Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport {\n ComputedServiceDependencies,\n FrameworkDependency,\n UIDependency,\n verifyDependencies\n} from \"./verifyDependencies\";\nimport { PackageRepr } from \"./PackageRepr\";\nimport { ReadonlyServiceLookup, ServiceLookupResult, ServicesLookupResult } from \"./ServiceLookup\";\nimport {\n InterfaceSpec,\n isAllImplementationsSpec,\n isSingleImplementationSpec,\n ReferenceSpec,\n renderInterfaceSpec\n} from \"./InterfaceSpec\";\nimport { ReferenceMeta } from \"../Service\";\nimport { RUNTIME_PACKAGE_NAME } from \"../builtin-services\";\n\nconst LOG = createLogger(\"runtime:ServiceLayer\");\n\nexport type DynamicLookupResult = ServiceLookupResult | UndeclaredDependency;\n\nexport interface UndeclaredDependency {\n type: \"undeclared\";\n}\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 private allServices: ServiceRepr[];\n\n // Set of required services. These services and their dependencies will be started.\n private requiredServices: Set<ServiceRepr>;\n\n private serviceLookup: ReadonlyServiceLookup;\n\n // Service id --> Service dependencies\n private serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n // Package name --> Interface name --> Declarations\n private declaredDependencies;\n private 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 {\n if (this.state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n if (!options?.ignoreDeclarationCheck && !this.isDeclaredDependency(packageName, spec)) {\n return { type: \"undeclared\" };\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 {\n if (this.state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n if (!this.isDeclaredDependency(packageName, { interfaceName, all: true })) {\n return { type: \"undeclared\" };\n }\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 private 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 // eslint-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 private 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\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 private isDeclaredDependency(packageName: string, spec: ReferenceSpec) {\n const packageEntry = this.declaredDependencies.get(packageName);\n if (!packageEntry) {\n return false;\n }\n const interfaceEntry = packageEntry.get(spec.interfaceName);\n if (!interfaceEntry) {\n return false;\n }\n\n if (isSingleImplementationSpec(spec)) {\n if (spec.qualifier == null) {\n return interfaceEntry.unqualified;\n }\n return interfaceEntry.qualifiers.has(spec.qualifier);\n } else {\n return interfaceEntry.all;\n }\n }\n\n private getReference(ref: ServiceRepr): [unknown, ReferenceMeta];\n private getReference(ref: ServiceRepr[]): [unknown[], ReferenceMeta[]];\n private getReference(\n ref: ServiceRepr | ServiceRepr[]\n ): [unknown, ReferenceMeta] | [unknown[], ReferenceMeta[]];\n private 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 private 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":";;;;;;AAuBA,MAAM,GAAA,GAAM,aAAa,sBAAsB,CAAA,CAAA;AAcxC,MAAM,YAAa,CAAA;AAAA;AAAA,EAEd,WAAA,CAAA;AAAA;AAAA,EAGA,gBAAA,CAAA;AAAA,EAEA,aAAA,CAAA;AAAA;AAAA,EAGA,mBAAA,CAAA;AAAA;AAAA,EAGA,oBAAA,CAAA;AAAA,EACA,KAAiD,GAAA,aAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASzD,WAAY,CAAA,QAAA,EAAkC,gBAAoC,GAAA,EAAI,EAAA;AAClF,IAAM,MAAA,WAAA,GAAc,SAAS,GAAI,CAAA,CAAC,QAAQ,GAAI,CAAA,QAAQ,EAAE,IAAK,EAAA,CAAA;AAC7D,IAAA,MAAM,iBAAiB,QAClB,CAAA,GAAA;AAAA,MAAI,CAAC,GACF,KAAA,GAAA,CAAI,YAAa,CAAA,GAAA,CAAkB,CAAC,GAAQ,KAAA;AACxC,QAAO,OAAA;AAAA,UACH,IAAM,EAAA,IAAA;AAAA,UACN,aAAa,GAAI,CAAA,IAAA;AAAA,UACjB,GAAG,GAAA;AAAA,SACP,CAAA;AAAA,OACH,CAAA;AAAA,MAEJ,IAAK,EAAA,CAAA;AACV,IAAA,MAAM,kBAAqB,GAAA;AAAA,MACvB,GAAG,gBAAA,CAAiB,GAAyB,CAAA,CAAC,CAAO,MAAA;AAAA,QACjD,IAAM,EAAA,WAAA;AAAA,QACN,GAAG,CAAA;AAAA,OACL,CAAA,CAAA;AAAA,MACF,GAAG,cAAA;AAAA,KACP,CAAA;AACA,IAAA,M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AAC,cAAgB,EAAA;AACjB,QAAiB,cAAA,GAAA;AAAA,UACb,GAAK,EAAA,KAAA;AAAA,UACL,WAAa,EAAA,KAAA;AAAA,UACb,UAAA,sBAAgB,GAAY,EAAA;AAAA,SAChC,CAAA;AACA,QAAa,YAAA,CAAA,GAAA,CAAI,WAAY,CAAA,aAAA,EAAe,cAAc,CAAA,CAAA;AAAA,OAC9D;AAEA,MAAI,IAAA,wBAAA,CAAyB,WAAW,CAAG,EAAA;AACvC,QAAA,cAAA,CAAe,GAAM,GAAA,IAAA,CAAA;AAAA,OACzB,MAAA,IAAW,0BAA2B,CAAA,WAAW,CAAG,EAAA;AAChD,QAAI,IAAA,WAAA,CAAY,aAAa,IAAM,EAAA;AAC/B,UAAA,cAAA,CAAe,WAAc,GAAA,IAAA,CAAA;AAAA,SAC1B,MAAA;AACH,UAAe,cAAA,CAAA,UAAA,CAAW,GAAI,CAAA,WAAA,CAAY,SAAS,CAAA,CAAA;AAAA,SACvD;AAAA,OACG,MAAA;AACH,QAAA,MAAM,IAAI,KAAA,CAAM,OAAQ,CAAA,QAAA,EAAU,CAAiC,+BAAA,CAAA,CAAA,CAAA;AAAA,OACvE;AAAA,KACJ;AACA,IAAM,KAAA,CAAA,GAAA,CAAI,aAAa,YAAY,CAAA,CAAA;AAAA,GACvC;AACA,EAAO,OAAA,KAAA,CAAA;AACX;;;;"}
|
|
1
|
+
{"version":3,"file":"ServiceLayer.js","sources":["ServiceLayer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { ServiceRepr } from \"./ServiceRepr\";\nimport { createLogger, Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport {\n ComputedServiceDependencies,\n FrameworkDependency,\n UIDependency,\n verifyDependencies\n} from \"./verifyDependencies\";\nimport { PackageRepr } from \"./PackageRepr\";\nimport { ReadonlyServiceLookup, ServiceLookupResult, ServicesLookupResult } from \"./ServiceLookup\";\nimport {\n InterfaceSpec,\n isAllImplementationsSpec,\n isSingleImplementationSpec,\n ReferenceSpec,\n renderInterfaceSpec\n} from \"./InterfaceSpec\";\nimport { ReferenceMeta } from \"../Service\";\nimport { RUNTIME_PACKAGE_NAME } from \"../builtin-services\";\n\nconst LOG = createLogger(\"runtime:ServiceLayer\");\n\nexport type DynamicLookupResult = ServiceLookupResult | UndeclaredDependency;\n\nexport interface UndeclaredDependency {\n type: \"undeclared\";\n}\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 private allServices: ServiceRepr[];\n\n // Set of required services. These services and their dependencies will be started.\n private requiredServices: Set<ServiceRepr>;\n\n private serviceLookup: ReadonlyServiceLookup;\n\n // Service id --> Service dependencies\n private serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n // Package name --> Interface name --> Declarations\n private declaredDependencies;\n private 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 {\n if (this.state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n if (!options?.ignoreDeclarationCheck && !this.isDeclaredDependency(packageName, spec)) {\n return { type: \"undeclared\" };\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 {\n if (this.state !== \"started\") {\n throw new Error(ErrorId.INTERNAL, \"Service layer is not started.\");\n }\n\n if (!this.isDeclaredDependency(packageName, { interfaceName, all: true })) {\n return { type: \"undeclared\" };\n }\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 private 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 // eslint-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 private 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\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 private isDeclaredDependency(packageName: string, spec: ReferenceSpec) {\n const packageEntry = this.declaredDependencies.get(packageName);\n if (!packageEntry) {\n return false;\n }\n const interfaceEntry = packageEntry.get(spec.interfaceName);\n if (!interfaceEntry) {\n return false;\n }\n\n if (isSingleImplementationSpec(spec)) {\n if (spec.qualifier == null) {\n return interfaceEntry.unqualified;\n }\n return interfaceEntry.qualifiers.has(spec.qualifier);\n } else {\n return interfaceEntry.all;\n }\n }\n\n private getReference(ref: ServiceRepr): [unknown, ReferenceMeta];\n private getReference(ref: ServiceRepr[]): [unknown[], ReferenceMeta[]];\n private getReference(\n ref: ServiceRepr | ServiceRepr[]\n ): [unknown, ReferenceMeta] | [unknown[], ReferenceMeta[]];\n private 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 private 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":";;;;;;AAuBA,MAAM,GAAA,GAAM,aAAa,sBAAsB,CAAA,CAAA;AAcxC,MAAM,YAAa,CAAA;AAAA;AAAA,EAEd,WAAA,CAAA;AAAA;AAAA,EAGA,gBAAA,CAAA;AAAA,EAEA,aAAA,CAAA;AAAA;AAAA,EAGA,mBAAA,CAAA;AAAA;AAAA,EAGA,oBAAA,CAAA;AAAA,EACA,KAAiD,GAAA,aAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASzD,WAAY,CAAA,QAAA,EAAkC,gBAAoC,GAAA,EAAI,EAAA;AAClF,IAAM,MAAA,WAAA,GAAc,SAAS,GAAI,CAAA,CAAC,QAAQ,GAAI,CAAA,QAAQ,EAAE,IAAK,EAAA,CAAA;AAC7D,IAAA,MAAM,iBAAiB,QAClB,CAAA,GAAA;AAAA,MAAI,CAAC,GACF,KAAA,GAAA,CAAI,YAAa,CAAA,GAAA,CAAkB,CAAC,GAAQ,KAAA;AACxC,QAAO,OAAA;AAAA,UACH,IAAM,EAAA,IAAA;AAAA,UACN,aAAa,GAAI,CAAA,IAAA;AAAA,UACjB,GAAG,GAAA;AAAA,SACP,CAAA;AAAA,OACH,CAAA;AAAA,MAEJ,IAAK,EAAA,CAA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iceLookup, "lookup" | "lookupOne" | "lookupAll" | "serviceCount">;
|
|
4
|
+
export interface Unimplemented {
|
|
5
|
+
type: "unimplemented";
|
|
6
|
+
}
|
|
7
|
+
export interface Ambiguous {
|
|
8
|
+
type: "ambiguous";
|
|
9
|
+
choices: AmbiguousChoice[];
|
|
10
|
+
}
|
|
11
|
+
export type AmbiguousChoice = [serviceId: string, qualifier: string | undefined];
|
|
12
|
+
export interface Found<T> {
|
|
13
|
+
type: "found";
|
|
14
|
+
value: T;
|
|
15
|
+
}
|
|
16
|
+
export type ServiceLookupResult = Unimplemented | Ambiguous | Found<ServiceRepr>;
|
|
17
|
+
export type ServicesLookupResult = Found<ServiceRepr[]>;
|
|
18
|
+
export declare class ServiceLookup {
|
|
19
|
+
private services;
|
|
20
|
+
private _count;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the total number of registered services.
|
|
23
|
+
*/
|
|
24
|
+
get serviceCount(): number;
|
|
25
|
+
/**
|
|
26
|
+
* Attempts to register the given service as an implementation of `interfaceName`.
|
|
27
|
+
* Throws if the registration did not complete successfully.
|
|
28
|
+
*
|
|
29
|
+
* All services should be registered before any lookups are attempted.
|
|
30
|
+
*/
|
|
31
|
+
register(service: ServiceRepr, { interfaceName, qualifier }: InterfaceSpec): void;
|
|
32
|
+
/**
|
|
33
|
+
* Attempts to find the service(s) required by the given reference spec.
|
|
34
|
+
*/
|
|
35
|
+
lookup(ref: ReferenceSpec): ServiceLookupResult | ServicesLookupResult;
|
|
36
|
+
/**
|
|
37
|
+
* Attempts to find a service implementing the given interface.
|
|
38
|
+
*/
|
|
39
|
+
lookupOne({ interfaceName, qualifier }: InterfaceSpec): ServiceLookupResult;
|
|
40
|
+
/**
|
|
41
|
+
* Returns all services implementing the given interface.
|
|
42
|
+
*/
|
|
43
|
+
lookupAll(interfaceName: string): ServicesLookupResult;
|
|
44
|
+
private ensureInterfaceEntry;
|
|
45
|
+
}
|
|
46
|
+
export declare function renderAmbiguousServiceChoices(choices: AmbiguousChoice[], max?: number): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceLookup.js","sources":["open-pioneer://external-pioneer-packages/@open-pioneer/runtime/service-layer/ServiceLookup.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\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 private services = new Map<string, Services>();\n\n // Total service count\n private _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 private 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":";;;;AAwCO,MAAM,aAAc,CAAA;AAAA;AAAA,EAEf,QAAA,uBAAe,GAAsB,EAAA,CAAA;AAAA;AAAA,EAGrC,MAAS,GAAA,CAAA,CAAA;AAAA;AAAA;AAAA;AAAA,EAKjB,IAAI,YAAe,GAAA;AACf,IAAA,OAAO,IAAK,CAAA,MAAA,CAAA;AAAA,GAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAS,CAAA,OAAA,EAAsB,EAAE,aAAA,EAAe,WAAkC,EAAA;AAC9E,IAAA,IAAI,CAAC,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IAAI,SAAW,EAAA;AACX,MAAA,OAAA,IAAW,CAAqB,kBAAA,EAAA,SAAA,CAAA,EAAA,CAAA,CAAA;AAAA,KACpC;AAEA,IAAI,IAAA,EAAE,SAAS,GAAK,EAAA;AAChB,MAAA,MAAA;AAAA,KACJ;AAAA,GACJ;AAEA,EAAM,MAAA,SAAA,GAAY,QAAQ,MAAS,GAAA,KAAA,CAAA;AACnC,EAAA,IAAI,YAAY,CAAG,EAAA;AACf,IAAA,OAAA,IAAW,CAAQ,KAAA,EAAA,SAAA,CAAA,KAAA,CAAA,CAAA;AAAA,GACvB;AACA,EAAO,OAAA,OAAA,CAAA;AACX,CAAA;AAEA,SAAS,MAAY,GAA+B,EAAA;AAChD,EAAA,OAAO,GAAI,CAAA,MAAA,EAAS,CAAA,IAAA,EAAO,CAAA,KAAA,CAAA;AAC/B;;;;"}
|
|
1
|
+
{"version":3,"file":"ServiceLookup.js","sources":["ServiceLookup.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\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 private services = new Map<string, Services>();\n\n // Total service count\n private _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 private 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":";;;;AAwCO,MAAM,aAAc,CAAA;AAAA;AAAA,EAEf,QAAA,uBAAe,GAAsB,EAAA,CAAA;AAAA;AAAA,EAGrC,MAAS,GAAA,CAAA,CAAA;AAAA;AAAA;AAAA;AAAA,EAKjB,IAAI,YAAe,GAAA;AACf,IAAA,OAAO,IAAK,CAAA,MAAA,CAAA;AAAA,GAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAS,CAAA,OAAA,EAAsB,EAAE,aAAA,EAAe,WAAkC,EAAA;AAC9E,IAAA,IAAI,CAAC,aAAe,EAAA;AAChB,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAQ,CAAA,gBAAA;AAAA,QACR,CAAA,SAAA,EAAY,QAAQ,EAAmC,CAAA,8BAAA,EAAA,aAAA,CAAA,EAAA,CAAA;AAAA,OAC3D,CAAA;AAAA,KACJ;AACA,IAAI,IAAA,SAAA,IAAa,IAAQ,IAAA,CAAC,SAAW,EAAA;AACjC,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAQ,CAAA,gBAAA;AAAA,QACR,CAAA,SAAA,EAAY,OAAQ,CAAA,EAAA,CAAA,2CAAA,EAAgD,aAAoB,CAAA,IAAA,EAAA,SAAA,CAAA,EAAA,CAAA;AAAA,OAC5F,CAAA;AAAA,KACJ;AAEA,IAAM,MAAA,QAAA,GAAW,IAAK,CAAA,oBAAA,CAAqB,aAAa,CAAA,CAAA;AACxD,IAAA,IAAI,CAAC,SAAW,EAAA;AACZ,MAAS,QAAA,CAAA,WAAA,CAAY,KAAK,OAAO,CAAA,CAAA;AACjC,MAAA,EAAE,IAAK,CAAA,MAAA,CAAA;AACP,MAAA,OAAA;AAAA,KACJ;AAEA,IAAA,MAAM,iBAAoB,GAAA,QAAA,CAAS,WAAY,CAAA,GAAA,CAAI,SAAS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AA,CAAA;AAAA,KACrC;AACA,IAAO,OAAA,KAAA,CAAA;AAAA,GACX;AACJ,CAAA;AAEgB,SAAA,6BAAA,CAA8B,OAA4B,EAAA,GAAA,GAAM,CAAW,EAAA;AACvF,EAAA,IAAI,OAAU,GAAA,EAAA,CAAA;AACd,EAAA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,EAAA,KAAA,MAAW,CAAC,SAAA,EAAW,SAAS,CAAA,IAAK,OAAS,EAAA;AAC1C,IAAA,IAAI,KAAO,EAAA;AACP,MAAW,OAAA,IAAA,IAAA,CAAA;AAAA,KACf;AAEA,IAAA,OAAA,IAAW,CAAI,CAAA,EAAA,SAAA,CAAA,CAAA,CAAA,CAAA;AACf,IAAA,IAAI,SAAW,EAAA;AACX,MAAA,OAAA,IAAW,CAAqB,kBAAA,EAAA,SAAA,CAAA,EAAA,CAAA,CAAA;AAAA,KACpC;AAEA,IAAI,IAAA,EAAE,SAAS,GAAK,EAAA;AAChB,MAAA,MAAA;AAAA,KACJ;AAAA,GACJ;AAEA,EAAM,MAAA,SAAA,GAAY,QAAQ,MAAS,GAAA,KAAA,CAAA;AACnC,EAAA,IAAI,YAAY,CAAG,EAAA;AACf,IAAA,OAAA,IAAW,CAAQ,KAAA,EAAA,SAAA,CAAA,KAAA,CAAA,CAAA;AAAA,GACvB;AACA,EAAO,OAAA,OAAA,CAAA;AACX,CAAA;AAEA,SAAS,MAAY,GAA+B,EAAA;AAChD,EAAA,OAAO,GAAI,CAAA,MAAA,EAAS,CAAA,IAAA,EAAO,CAAA,KAAA,CAAA;AAC/B;;;;"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ServiceMetadata } from "../metadata";
|
|
2
|
+
import { Service, ServiceConstructor, ServiceOptions } from "../Service";
|
|
3
|
+
import { InterfaceSpec, ReferenceSpec } from "./InterfaceSpec";
|
|
4
|
+
import { PackageIntl } from "../i18n";
|
|
5
|
+
export type ServiceState = "not-constructed" | "constructing" | "constructed" | "destroyed";
|
|
6
|
+
export type ServiceDependency = ReferenceSpec & {
|
|
7
|
+
referenceName: string;
|
|
8
|
+
};
|
|
9
|
+
export interface ConstructorFactory {
|
|
10
|
+
type: "class";
|
|
11
|
+
clazz: ServiceConstructor<any>;
|
|
12
|
+
}
|
|
13
|
+
export interface FunctionFactory {
|
|
14
|
+
type: "function";
|
|
15
|
+
create: (options: ServiceOptions) => Service;
|
|
16
|
+
}
|
|
17
|
+
export type ServiceFactory = ConstructorFactory | FunctionFactory;
|
|
18
|
+
export interface ServiceReprOptions {
|
|
19
|
+
name: string;
|
|
20
|
+
packageName: string;
|
|
21
|
+
factory: ServiceFactory;
|
|
22
|
+
intl: PackageIntl;
|
|
23
|
+
dependencies?: ServiceDependency[];
|
|
24
|
+
interfaces?: InterfaceSpec[];
|
|
25
|
+
properties?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Represents metadata and state of a service in the runtime.
|
|
29
|
+
* `this.instance` is the actual service instance (when constructed).
|
|
30
|
+
*/
|
|
31
|
+
export declare class ServiceRepr {
|
|
32
|
+
static create(packageName: string, data: ServiceMetadata, intl: PackageIntl, properties?: Record<string, unknown>): ServiceRepr;
|
|
33
|
+
/** Unique id of this service. Contains the package name and the service name. */
|
|
34
|
+
readonly id: string;
|
|
35
|
+
/** Name of this service in it's package. */
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/** Name of the parent package. */
|
|
38
|
+
readonly packageName: string;
|
|
39
|
+
/** Locale-dependant i18n messages. */
|
|
40
|
+
readonly intl: PackageIntl;
|
|
41
|
+
/** Service properties made available via the service's constructor. */
|
|
42
|
+
readonly properties: Readonly<Record<string, unknown>>;
|
|
43
|
+
/** Dependencies required by the service constructor. */
|
|
44
|
+
readonly dependencies: readonly ServiceDependency[];
|
|
45
|
+
/** Interfaces provided by the service. */
|
|
46
|
+
readonly interfaces: readonly Readonly<InterfaceSpec>[];
|
|
47
|
+
/** Number of references to this service. */
|
|
48
|
+
private _useCount;
|
|
49
|
+
/** Service factory to construct an instance. */
|
|
50
|
+
private factory;
|
|
51
|
+
/** Current state of this service. "constructed" -> instance is available. */
|
|
52
|
+
private _state;
|
|
53
|
+
/** Service instance, once constructed. */
|
|
54
|
+
private _instance;
|
|
55
|
+
constructor(options: ServiceReprOptions);
|
|
56
|
+
/** Returns the current service instance or undefined if the service has not been constructed. */
|
|
57
|
+
get instance(): Service | undefined;
|
|
58
|
+
/** Returns the current state of the service. */
|
|
59
|
+
get state(): ServiceState;
|
|
60
|
+
/** Returns the current reference count. */
|
|
61
|
+
get useCount(): number;
|
|
62
|
+
/**
|
|
63
|
+
* Same as `instance`, but throws when the instance has not been constructed.
|
|
64
|
+
*/
|
|
65
|
+
getInstanceOrThrow(): import("../Service").ServiceLifecycleHooks;
|
|
66
|
+
/**
|
|
67
|
+
* Called before `create()` to place the service into the `constructing` state,
|
|
68
|
+
* which is currently used to detect cycles.
|
|
69
|
+
*/
|
|
70
|
+
beforeCreate(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Instantiates the service by invoking the service constructor
|
|
73
|
+
* with the given `options`.
|
|
74
|
+
*
|
|
75
|
+
* The service's use count is initialized to `1`, so every `create()`
|
|
76
|
+
* should be paired with a `removeRef()`.
|
|
77
|
+
*
|
|
78
|
+
* `destroy()` can be invoked once the final `removeRef()` has returned zero.
|
|
79
|
+
*/
|
|
80
|
+
create(options: Pick<ServiceOptions, "references" | "referencesMeta">): import("../Service").ServiceLifecycleHooks;
|
|
81
|
+
destroy(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Adds a use to the service's use count.
|
|
84
|
+
* References to a service are tracked: it should only be destroyed when it is no longer being used.
|
|
85
|
+
*/
|
|
86
|
+
addRef(): number;
|
|
87
|
+
/**
|
|
88
|
+
* Removes a use from the service's use count.
|
|
89
|
+
* Returns the new use count.
|
|
90
|
+
*/
|
|
91
|
+
removeRef(): number;
|
|
92
|
+
}
|
|
93
|
+
export declare function createConstructorFactory<T extends {}>(clazz: ServiceConstructor<T>): ConstructorFactory;
|
|
94
|
+
export declare function createFunctionFactory(create: (options: ServiceOptions) => Service): FunctionFactory;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceRepr.js","sources":["open-pioneer://external-pioneer-packages/@open-pioneer/runtime/service-layer/ServiceRepr.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { ErrorId } from \"../errors\";\nimport { ServiceMetadata } from \"../metadata\";\nimport { Service, ServiceConstructor, ServiceOptions } from \"../Service\";\nimport { Error } from \"@open-pioneer/core\";\nimport { InterfaceSpec, parseReferenceSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { PackageIntl } from \"../i18n\";\n\nexport type ServiceState = \"not-constructed\" | \"constructing\" | \"constructed\" | \"destroyed\";\n\nexport type ServiceDependency = ReferenceSpec & { referenceName: string };\n\nexport interface ConstructorFactory {\n type: \"class\";\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n clazz: ServiceConstructor<any>;\n}\n\nexport interface FunctionFactory {\n type: \"function\";\n create: (options: ServiceOptions) => Service;\n}\n\nexport type ServiceFactory = ConstructorFactory | FunctionFactory;\n\nexport interface ServiceReprOptions {\n name: string;\n packageName: string;\n factory: ServiceFactory;\n intl: PackageIntl;\n dependencies?: ServiceDependency[];\n interfaces?: InterfaceSpec[];\n properties?: Record<string, unknown>;\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: PackageIntl,\n properties?: Record<string, unknown>\n ): ServiceRepr {\n const clazz = data.clazz;\n const name = data.name;\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: ConstructorFactory = {\n type: \"class\",\n clazz\n };\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: 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 private _useCount = 0;\n\n /** Service factory to construct an instance. */\n private factory: ServiceFactory;\n\n /** Current state of this service. \"constructed\" -> instance is available. */\n private _state: ServiceState = \"not-constructed\";\n\n /** Service instance, once constructed. */\n private _instance: Service | 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(options: 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 try {\n this._instance = createService(this.factory, {\n ...options,\n properties: this.properties,\n intl: this.intl\n });\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._instance.destroy?.();\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>\n): ConstructorFactory {\n return { type: \"class\", clazz };\n}\n\nexport function createFunctionFactory(\n create: (options: ServiceOptions) => Service\n): FunctionFactory {\n return { type: \"function\", create };\n}\n\nconst SERVICE_NAME_REGEX = /^[a-z0-9_-]+$/i;\n\nfunction isValidServiceName(name: string) {\n return SERVICE_NAME_REGEX.test(name);\n}\n\nfunction createService(factory: ServiceFactory, options: ServiceOptions) {\n switch (factory.type) {\n case \"class\":\n return new factory.clazz(options);\n case \"function\":\n return factory.create(options);\n }\n}\n"],"names":["name"],"mappings":";;;;AAwCO,MAAM,WAAY,CAAA;AAAA,EACrB,OAAO,MAAA,CACH,WACA,EAAA,IAAA,EACA,MACA,UACW,EAAA;AACX,IAAA,MAAM,QAAQ,IAAK,CAAA,KAAA,CAAA;AACnB,IAAA,MAAM,OAAO,IAAK,CAAA,IAAA,CAAA;AAClB,IAAA,MAAM,eAAe,MAAO,CAAA,OAAA,CAAQ,KAAK,UAAc,IAAA,EAAE,CAAE,CAAA,GAAA;AAAA,MACvD,CAAC,CAACA,KAAM,EAAA,iBAAiB,CAAM,KAAA;AAC3B,QAAO,OAAA;AAAA,UACH,aAAeA,EAAAA,KAAAA;AAAA,UACf,GAAG,mBAAmB,iBAAiB,CAAA;AAAA,SAC3C,CAAA;AAAA,OACJ;AAAA,KACJ,CAAA;AACA,IAAA,MAAM,UAAa,GAAA,IAAA,CAAK,QAAU,EAAA,GAAA,CAAmB,CAAC,CAAM,KAAA;AACxD,MAAO,OAAA;AAAA,QACH,eAAe,CAAE,CAAA,IAAA;AAAA,QACjB,WAAW,CAAE,CAAA,SAAA;AAAA,OACjB,CAAA;AAAA,KACH,CAAA,CAAA;AACD,IAAA,MAAM,OAA8B,GAAA;AAAA,MAChC,IAAM,EAAA,OAAA;AAAA,MACN,KAAA;AAAA,KACJ,CAAA;AACA,IAAA,OAAO,IAAI,WAAY,CAAA;AAAA,MACnB,IAAA;AAAA,MACA,WAAA;AAAA,MACA,OAAA;AAAA,MACA,IAAA;AAAA,MACA,YAAA;AAAA,MACA,UAAA;AAAA,MACA,UAAA;AAAA,KACH,CAAA,CAAA;AAAA,GACL;AAAA;AAAA,EAGS,EAAA,CAAA;AAAA;AAAA,EAGA,IAAA,CAAA;AAAA;AAAA,EAGA,WAAA,CAAA;AAAA;AAAA,EAGA,IAAA,CAAA;AAAA;AAAA,EAGA,UAAA,CAAA;AAAA;AAAA,EAGA,YAAA,C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CAAmB,KAAK,IAAI,CAAA,CAAA;AACvC,CAAA;AAEA,SAAS,aAAA,CAAc,SAAyB,OAAyB,EAAA;AACrE,EAAA,QAAQ,QAAQ,IAAM;AAAA,IAClB,KAAK,OAAA;AACD,MAAO,OAAA,IAAI,OAAQ,CAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAAA,IACpC,KAAK,UAAA;AACD,MAAO,OAAA,OAAA,CAAQ,OAAO,OAAO,CAAA,CAAA;AAAA,GACrC;AACJ;;;;"}
|
|
1
|
+
{"version":3,"file":"ServiceRepr.js","sources":["ServiceRepr.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { ErrorId } from \"../errors\";\nimport { ServiceMetadata } from \"../metadata\";\nimport { Service, ServiceConstructor, ServiceOptions } from \"../Service\";\nimport { Error } from \"@open-pioneer/core\";\nimport { InterfaceSpec, parseReferenceSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { PackageIntl } from \"../i18n\";\n\nexport type ServiceState = \"not-constructed\" | \"constructing\" | \"constructed\" | \"destroyed\";\n\nexport type ServiceDependency = ReferenceSpec & { referenceName: string };\n\nexport interface ConstructorFactory {\n type: \"class\";\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n clazz: ServiceConstructor<any>;\n}\n\nexport interface FunctionFactory {\n type: \"function\";\n create: (options: ServiceOptions) => Service;\n}\n\nexport type ServiceFactory = ConstructorFactory | FunctionFactory;\n\nexport interface ServiceReprOptions {\n name: string;\n packageName: string;\n factory: ServiceFactory;\n intl: PackageIntl;\n dependencies?: ServiceDependency[];\n interfaces?: InterfaceSpec[];\n properties?: Record<string, unknown>;\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: PackageIntl,\n properties?: Record<string, unknown>\n ): ServiceRepr {\n const clazz = data.clazz;\n const name = data.name;\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: ConstructorFactory = {\n type: \"class\",\n clazz\n };\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: 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 private _useCount = 0;\n\n /** Service factory to construct an instance. */\n private factory: ServiceFactory;\n\n /** Current state of this service. \"constructed\" -> instance is available. */\n private _state: ServiceState = \"not-constructed\";\n\n /** Service instance, once constructed. */\n private _instance: Service | 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(options: 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 try {\n this._instance = createService(this.factory, {\n ...options,\n properties: this.properties,\n intl: this.intl\n });\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._instance.destroy?.();\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>\n): ConstructorFactory {\n return { type: \"class\", clazz };\n}\n\nexport function createFunctionFactory(\n create: (options: ServiceOptions) => Service\n): FunctionFactory {\n return { type: \"function\", create };\n}\n\nconst SERVICE_NAME_REGEX = /^[a-z0-9_-]+$/i;\n\nfunction isValidServiceName(name: string) {\n return SERVICE_NAME_REGEX.test(name);\n}\n\nfunction createService(factory: ServiceFactory, options: ServiceOptions) {\n switch (factory.type) {\n case \"class\":\n return new factory.clazz(options);\n case \"function\":\n return factory.create(options);\n }\n}\n"],"names":["name"],"mappings":";;;;AAwCO,MAAM,WAAY,CAAA;AAAA,EACrB,OAAO,MAAA,CACH,WACA,EAAA,IAAA,EACA,MACA,UACW,EAAA;AACX,IAAA,MAAM,QAAQ,IAAK,CAAA,KAAA,CAAA;AACnB,IAAA,MAAM,OAAO,IAAK,CAAA,IAAA,CAAA;AAClB,IAAA,MAAM,eAAe,MAAO,CAAA,OAAA,CAAQ,KAAK,UAAc,IAAA,EAAE,CAAE,CAAA,GAAA;AAAA,MACvD,CAAC,CAACA,KAAM,EAAA,iBAAiB,CAAM,KAAA;AAC3B,QAAO,OAAA;AAAA,UACH,aAAeA,EAAAA,KAAAA;AAAA,UACf,GAAG,mBAAmB,iBAAiB,CAAA;AAAA,SAC3C,CAAA;AAAA,OACJ;AAAA,KACJ,CAAA;AACA,IAAA,MAAM,UAAa,GAAA,IAAA,CAAK,QAAU,EAAA,GAAA,CAAmB,CAAC,CAAM,KAAA;AACxD,MAAO,OAAA;AAAA,QACH,eAAe,CAAE,CAAA,IAAA;AAAA,QACjB,WAAW,CAAE,CAAA,SAAA;AAAA,OACjB,CAAA;AAAA,KACH,CAAA,CAAA;AACD,IAAA,MAAM,OAA8B,GAAA;AAAA,MAChC,IAAM,EAAA,OAAA;AAAA,MACN,KAAA;AAAA,KACJ,CAAA;AACA,IAAA,OAAO,IAAI,WAAY,CAAA;AAAA,MACnB,IAAA;AAAA,MACA,WAAA;AAAA,MACA,OAAA;AAAA,MACA,IAAA;AAAA,MACA,YAAA;AAAA,MACA,UAAA;AAAA,MACA,UAAA;AAAA,KACH,CAAA,CAAA;AAAA,GACL;AAA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,EAAO,OAAA,EAAE,IAAM,EAAA,UAAA,EAAY,MAAO,EAAA,CAAA;AACtC,CAAA;AAEA,MAAM,kBAAqB,GAAA,gBAAA,CAAA;AAE3B,SAAS,mBAAmB,IAAc,EAAA;AACtC,EAAO,OAAA,kBAAA,CAAmB,KAAK,IAAI,CAAA,CAAA;AACvC,CAAA;AAEA,SAAS,aAAA,CAAc,SAAyB,OAAyB,EAAA;AACrE,EAAA,QAAQ,QAAQ,IAAM;AAAA,IAClB,KAAK,OAAA;AACD,MAAO,OAAA,IAAI,OAAQ,CAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAAA,IACpC,KAAK,UAAA;AACD,MAAO,OAAA,OAAA,CAAQ,OAAO,OAAO,CAAA,CAAA;AAAA,GACrC;AACJ;;;;"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ReferenceSpec } from "./InterfaceSpec";
|
|
2
|
+
import { ReadonlyServiceLookup } from "./ServiceLookup";
|
|
3
|
+
import { ServiceRepr } from "./ServiceRepr";
|
|
4
|
+
export interface DependencyOptions {
|
|
5
|
+
/**
|
|
6
|
+
* All services known to the service layer.
|
|
7
|
+
*/
|
|
8
|
+
services?: readonly ServiceRepr[];
|
|
9
|
+
/**
|
|
10
|
+
* References that must be satisfied by the services.
|
|
11
|
+
*/
|
|
12
|
+
requiredReferences?: readonly RequiredReference[];
|
|
13
|
+
}
|
|
14
|
+
export type RequiredReference = UIDependency | FrameworkDependency;
|
|
15
|
+
export type UIDependency = {
|
|
16
|
+
type: "ui";
|
|
17
|
+
packageName: string;
|
|
18
|
+
} & ReferenceSpec;
|
|
19
|
+
export type FrameworkDependency = {
|
|
20
|
+
type: "framework";
|
|
21
|
+
} & ReferenceSpec;
|
|
22
|
+
export type ComputedServiceDependencies = Record<string, ServiceRepr | ServiceRepr[]>;
|
|
23
|
+
export interface VerifyDependenciesResult {
|
|
24
|
+
/**
|
|
25
|
+
* Precomputed dependencies for every service.
|
|
26
|
+
*
|
|
27
|
+
* Key: service id.
|
|
28
|
+
*/
|
|
29
|
+
serviceDependencies: Map<string, ComputedServiceDependencies>;
|
|
30
|
+
/**
|
|
31
|
+
* Index structure mapping interface names to service instances.
|
|
32
|
+
*/
|
|
33
|
+
serviceLookup: ReadonlyServiceLookup;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Visits all services and ensures that their dependencies can be satisfied by each other without a cycle.
|
|
37
|
+
*
|
|
38
|
+
* Throws an error if a cycle is detected or if a required service is never implemented.
|
|
39
|
+
*
|
|
40
|
+
* @returns an object containing the service index and precomputed dependency information
|
|
41
|
+
*/
|
|
42
|
+
export declare function verifyDependencies(options: DependencyOptions): VerifyDependenciesResult;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verifyDependencies.js","sources":["open-pioneer://external-pioneer-packages/@open-pioneer/runtime/service-layer/verifyDependencies.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, ReferenceSpec, renderInterfaceSpec } from \"./InterfaceSpec\";\nimport {\n AmbiguousChoice,\n ReadonlyServiceLookup,\n renderAmbiguousServiceChoices,\n ServiceLookup\n} from \"./ServiceLookup\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\nexport interface DependencyOptions {\n /**\n * All services known to the service layer.\n */\n services?: readonly ServiceRepr[];\n\n /**\n * References that must be satisfied by the services.\n */\n requiredReferences?: readonly RequiredReference[];\n}\n\nexport type RequiredReference = UIDependency | FrameworkDependency;\n\nexport type UIDependency = { type: \"ui\"; packageName: string } & ReferenceSpec;\n\nexport type FrameworkDependency = { type: \"framework\" } & ReferenceSpec;\n\nexport type ComputedServiceDependencies = Record<string, ServiceRepr | ServiceRepr[]>;\n\nexport interface VerifyDependenciesResult {\n /**\n * Precomputed dependencies for every service.\n *\n * Key: service id.\n */\n serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n /**\n * Index structure mapping interface names to service instances.\n */\n serviceLookup: ReadonlyServiceLookup;\n}\n\n/**\n * Visits all services and ensures that their dependencies can be satisfied by each other without a cycle.\n *\n * Throws an error if a cycle is detected or if a required service is never implemented.\n *\n * @returns an object containing the service index and precomputed dependency information\n */\nexport function verifyDependencies(options: DependencyOptions): VerifyDependenciesResult {\n const verifier = new Verifier(options);\n verifier.verify();\n return {\n serviceLookup: verifier.getServiceLookup(),\n serviceDependencies: verifier.getComputedDependencies()\n };\n}\n\ninterface GraphItem {\n state: ItemState;\n service: ServiceRepr;\n dependencies: ComputedServiceDependencies;\n}\n\ntype ItemState = \"not-visited\" | \"pending\" | \"done\";\n\n/** The reason why a graph node is being visited. */\ntype ItemVisitReason = \"root\" | InterfaceReference;\n\ntype InterfaceReference =\n | {\n type: \"framework-reference\";\n value: ReferenceSpec;\n }\n | {\n type: \"ui-reference\";\n packageName: string;\n value: ReferenceSpec;\n }\n | {\n type: \"service-reference\";\n service: ServiceRepr;\n referenceName: string;\n value: ReferenceSpec;\n };\n\nclass Verifier {\n private requiredReferences: readonly RequiredReference[];\n private items: GraphItem[];\n private serviceLookup = new ServiceLookup();\n private serviceToGraphItem = new Map<ServiceRepr, GraphItem>();\n\n // Stack of visited nodes\n private stack: [item: GraphItem, reason: ItemVisitReason][] = [];\n\n constructor(options: DependencyOptions) {\n this.requiredReferences = options.requiredReferences ?? [];\n const services = options.services ?? [];\n const items = (this.items = services.map<GraphItem>((service) => {\n return {\n service: service,\n state: \"not-visited\",\n dependencies: {}\n };\n }));\n\n for (const item of items) {\n for (const spec of item.service.interfaces) {\n this.registerService(item, spec);\n }\n }\n }\n\n verify() {\n for (const ref of this.requiredReferences) {\n let interfaceRef: InterfaceReference;\n switch (ref.type) {\n case \"framework\":\n interfaceRef = { type: \"framework-reference\", value: ref };\n break;\n case \"ui\":\n interfaceRef = {\n type: \"ui-reference\",\n packageName: ref.packageName,\n value: ref\n };\n break;\n }\n this.visitReference(interfaceRef);\n }\n for (const item of this.items) {\n this.visitItem(item, \"root\");\n }\n }\n\n getServiceLookup() {\n return this.serviceLookup;\n }\n\n getComputedDependencies() {\n return new Map(\n this.items.map<[string, ComputedServiceDependencies]>((item) => {\n return [item.service.id, item.dependencies];\n })\n );\n }\n\n private visitItem(item: GraphItem, reason: ItemVisitReason) {\n const stack = this.stack;\n const state = item.state;\n if (state === \"done\") {\n return; // Item is initialized, cycle impossible\n }\n if (state === \"pending\") {\n this.throwCycleError(item, reason);\n }\n\n stack.push([item, reason]);\n item.state = \"pending\";\n for (const dep of item.service.dependencies) {\n item.dependencies[dep.referenceName] = this.visitReference({\n type: \"service-reference\",\n service: item.service,\n referenceName: dep.referenceName,\n value: dep\n });\n }\n item.state = \"done\";\n stack.pop();\n }\n\n private visitReference(ref: InterfaceReference) {\n const itemOrItems = this.findServices(ref);\n if (Array.isArray(itemOrItems)) {\n for (const child of itemOrItems) {\n this.visitItem(child, ref);\n }\n return itemOrItems.map((item) => item.service);\n } else {\n this.visitItem(itemOrItems, ref);\n return itemOrItems.service;\n }\n }\n\n private registerService(item: GraphItem, spec: InterfaceSpec) {\n this.serviceLookup.register(item.service, spec);\n this.serviceToGraphItem.set(item.service, item);\n }\n\n private findServices(ref: InterfaceReference): GraphItem | GraphItem[] {\n const spec = ref.value;\n const lookupResult = this.serviceLookup.lookup(spec);\n switch (lookupResult.type) {\n case \"unimplemented\": {\n const message = unimplementedInterfaceMessage(ref);\n throw new Error(ErrorId.INTERFACE_NOT_FOUND, message);\n }\n case \"ambiguous\": {\n const message = ambiguousInterfaceMessage(ref, lookupResult.choices);\n throw new Error(ErrorId.AMBIGUOUS_DEPENDENCY, message);\n }\n }\n\n const value = lookupResult.value;\n if (Array.isArray(value)) {\n return value.map((service) => this.getGraphItem(service));\n } else {\n return this.getGraphItem(value);\n }\n }\n\n private getGraphItem(service: ServiceRepr): GraphItem {\n const graphItem = this.serviceToGraphItem.get(service);\n if (!graphItem || graphItem.service !== service) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find matching graph item for service '${service.id}'.`\n );\n }\n return graphItem;\n }\n\n private throwCycleError(item: GraphItem, reason: ItemVisitReason) {\n // Find the item on the stack (it must be there because we detected a cycle).\n const stack = this.stack;\n const ownIndex = stack.findIndex((entry) => entry[0] === item);\n if (ownIndex === -1) {\n throw new Error(ErrorId.INTERNAL, \"Failed to find cycle participant on the stack.\");\n }\n\n const cycleInfo = stack\n .slice(ownIndex)\n .concat([[item, reason]])\n .map<string>((entry, index) => {\n const item = entry[0];\n const reason = index === 0 ? \"root\" : entry[1]; // info before the cycle does not matter\n\n let message = `'${item.service.id}'`;\n if (typeof reason === \"object\") {\n message += ` (${referenceDetail(reason)})`;\n }\n return message;\n });\n throw new Error(\n ErrorId.DEPENDENCY_CYCLE,\n `Detected dependency cycle: ${cycleInfo.join(\" => \")}.`\n );\n }\n}\n\nfunction unimplementedInterfaceMessage(ref: InterfaceReference): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n switch (ref.type) {\n case \"service-reference\":\n return `Service '${ref.service.id}' requires an unimplemented interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n case \"ui-reference\":\n return `The UI of package '${ref.packageName}' requires an unimplemented interface ${interfaceText}.`;\n case \"framework-reference\":\n return `The framework requires an unimplemented interface ${interfaceText}.`;\n }\n}\n\nfunction ambiguousInterfaceMessage(ref: InterfaceReference, choices: AmbiguousChoice[]): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n const serviceChoices = renderAmbiguousServiceChoices(choices);\n\n let message = \"\";\n switch (ref.type) {\n case \"service-reference\":\n message = `Service '${ref.service.id}' requires the ambiguous interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n break;\n case \"ui-reference\":\n message = `The UI of package '${ref.packageName}' requires the ambiguous interface ${interfaceText}.`;\n break;\n case \"framework-reference\":\n message = `The framework requires the ambiguous interface ${interfaceText}.`;\n break;\n }\n message += ` Possible choices are ${serviceChoices}.`;\n return message;\n}\n\nfunction referenceDetail(reason: InterfaceReference) {\n switch (reason.type) {\n case \"service-reference\":\n return `'${reason.referenceName}' providing '${reason.value.interfaceName}'`;\n case \"ui-reference\":\n return `UI of package '${reason.packageName}' requiring '${reason.value.interfaceName}'`;\n case \"framework-reference\":\n return `framework requiring '${reason.value.interfaceName}'`;\n }\n}\n"],"names":["item","reason"],"mappings":";;;;;AAsDO,SAAS,mBAAmB,OAAsD,EAAA;AACrF,EAAM,MAAA,QAAA,GAAW,IAAI,QAAA,CAAS,OAAO,CAAA,CAAA;AACrC,EAAA,QAAA,CAAS,MAAO,EAAA,CAAA;AAChB,EAAO,OAAA;AAAA,IACH,aAAA,EAAe,SAAS,gBAAiB,EAAA;AAAA,IACzC,mBAAA,EAAqB,SAAS,uBAAwB,EAAA;AAAA,GAC1D,CAAA;AACJ,CAAA;AA8BA,MAAM,QAAS,CAAA;AAAA,EACH,kBAAA,CAAA;AAAA,EACA,KAAA,CAAA;AAAA,EACA,aAAA,GAAgB,IAAI,aAAc,EAAA,CAAA;AAAA,EAClC,kBAAA,uBAAyB,GAA4B,EAAA,CAAA;AAAA;AAAA,EAGrD,QAAsD,EAAC,CAAA;AAAA,EAE/D,YAAY,OAA4B,EAAA;AACpC,IAAK,IAAA,CAAA,kBAAA,GAAqB,OAAQ,CAAA,kBAAA,IAAsB,EAAC,CAAA;AACzD,IAAM,MAAA,QAAA,GAAW,OAAQ,CAAA,QAAA,IAAY,EAAC,CAAA;AACtC,IAAA,MAAM,QAAS,IAAK,CAAA,KAAA,GAAQ,QAAS,CAAA,GAAA,CAAe,CAAC,OAAY,KAAA;AAC7D,MAAO,OAAA;AAAA,QACH,OAAA;AAAA,QACA,KAAO,EAAA,aAAA;AAAA,QACP,cAAc,EAAC;AAAA,OACnB,CAAA;AAAA,KACH,CAAA,CAAA;AAED,IAAA,KAAA,MAAW,QAAQ,KAAO,EAAA;AACtB,MAAW,KAAA,MAAA,IAAA,IAAQ,IAAK,CAAA,OAAA,CAAQ,UAAY,EAAA;AACxC,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A,CAAA,mBAAA,EAAsB,IAAI,WAAiD,CAAA,mCAAA,EAAA,aAAA,CAAA,CAAA,CAAA,CAAA;AACrF,MAAA,MAAA;AAAA,IACJ,KAAK,qBAAA;AACD,MAAA,OAAA,GAAU,CAAkD,+CAAA,EAAA,aAAA,CAAA,CAAA,CAAA,CAAA;AAC5D,MAAA,MAAA;AAAA,GACR;AACA,EAAA,OAAA,IAAW,CAAyB,sBAAA,EAAA,cAAA,CAAA,CAAA,CAAA,CAAA;AACpC,EAAO,OAAA,OAAA,CAAA;AACX,CAAA;AAEA,SAAS,gBAAgB,MAA4B,EAAA;AACjD,EAAA,QAAQ,OAAO,IAAM;AAAA,IACjB,KAAK,mBAAA;AACD,MAAA,OAAO,CAAI,CAAA,EAAA,MAAA,CAAO,aAA6B,CAAA,aAAA,EAAA,MAAA,CAAO,KAAM,CAAA,aAAA,CAAA,CAAA,CAAA,CAAA;AAAA,IAChE,KAAK,cAAA;AACD,MAAA,OAAO,CAAkB,eAAA,EAAA,MAAA,CAAO,WAA2B,CAAA,aAAA,EAAA,MAAA,CAAO,KAAM,CAAA,aAAA,CAAA,CAAA,CAAA,CAAA;AAAA,IAC5E,KAAK,qBAAA;AACD,MAAO,OAAA,CAAA,qBAAA,EAAwB,OAAO,KAAM,CAAA,aAAA,CAAA,CAAA,CAAA,CAAA;AAAA,GACpD;AACJ;;;;"}
|
|
1
|
+
{"version":3,"file":"verifyDependencies.js","sources":["verifyDependencies.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, ReferenceSpec, renderInterfaceSpec } from \"./InterfaceSpec\";\nimport {\n AmbiguousChoice,\n ReadonlyServiceLookup,\n renderAmbiguousServiceChoices,\n ServiceLookup\n} from \"./ServiceLookup\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\nexport interface DependencyOptions {\n /**\n * All services known to the service layer.\n */\n services?: readonly ServiceRepr[];\n\n /**\n * References that must be satisfied by the services.\n */\n requiredReferences?: readonly RequiredReference[];\n}\n\nexport type RequiredReference = UIDependency | FrameworkDependency;\n\nexport type UIDependency = { type: \"ui\"; packageName: string } & ReferenceSpec;\n\nexport type FrameworkDependency = { type: \"framework\" } & ReferenceSpec;\n\nexport type ComputedServiceDependencies = Record<string, ServiceRepr | ServiceRepr[]>;\n\nexport interface VerifyDependenciesResult {\n /**\n * Precomputed dependencies for every service.\n *\n * Key: service id.\n */\n serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n /**\n * Index structure mapping interface names to service instances.\n */\n serviceLookup: ReadonlyServiceLookup;\n}\n\n/**\n * Visits all services and ensures that their dependencies can be satisfied by each other without a cycle.\n *\n * Throws an error if a cycle is detected or if a required service is never implemented.\n *\n * @returns an object containing the service index and precomputed dependency information\n */\nexport function verifyDependencies(options: DependencyOptions): VerifyDependenciesResult {\n const verifier = new Verifier(options);\n verifier.verify();\n return {\n serviceLookup: verifier.getServiceLookup(),\n serviceDependencies: verifier.getComputedDependencies()\n };\n}\n\ninterface GraphItem {\n state: ItemState;\n service: ServiceRepr;\n dependencies: ComputedServiceDependencies;\n}\n\ntype ItemState = \"not-visited\" | \"pending\" | \"done\";\n\n/** The reason why a graph node is being visited. */\ntype ItemVisitReason = \"root\" | InterfaceReference;\n\ntype InterfaceReference =\n | {\n type: \"framework-reference\";\n value: ReferenceSpec;\n }\n | {\n type: \"ui-reference\";\n packageName: string;\n value: ReferenceSpec;\n }\n | {\n type: \"service-reference\";\n service: ServiceRepr;\n referenceName: string;\n value: ReferenceSpec;\n };\n\nclass Verifier {\n private requiredReferences: readonly RequiredReference[];\n private items: GraphItem[];\n private serviceLookup = new ServiceLookup();\n private serviceToGraphItem = new Map<ServiceRepr, GraphItem>();\n\n // Stack of visited nodes\n private stack: [item: GraphItem, reason: ItemVisitReason][] = [];\n\n constructor(options: DependencyOptions) {\n this.requiredReferences = options.requiredReferences ?? [];\n const services = options.services ?? [];\n const items = (this.items = services.map<GraphItem>((service) => {\n return {\n service: service,\n state: \"not-visited\",\n dependencies: {}\n };\n }));\n\n for (const item of items) {\n for (const spec of item.service.interfaces) {\n this.registerService(item, spec);\n }\n }\n }\n\n verify() {\n for (const ref of this.requiredReferences) {\n let interfaceRef: InterfaceReference;\n switch (ref.type) {\n case \"framework\":\n interfaceRef = { type: \"framework-reference\", value: ref };\n break;\n case \"ui\":\n interfaceRef = {\n type: \"ui-reference\",\n packageName: ref.packageName,\n value: ref\n };\n break;\n }\n this.visitReference(interfaceRef);\n }\n for (const item of this.items) {\n this.visitItem(item, \"root\");\n }\n }\n\n getServiceLookup() {\n return this.serviceLookup;\n }\n\n getComputedDependencies() {\n return new Map(\n this.items.map<[string, ComputedServiceDependencies]>((item) => {\n return [item.service.id, item.dependencies];\n })\n );\n }\n\n private visitItem(item: GraphItem, reason: ItemVisitReason) {\n const stack = this.stack;\n const state = item.state;\n if (state === \"done\") {\n return; // Item is initialized, cycle impossible\n }\n if (state === \"pending\") {\n this.throwCycleError(item, reason);\n }\n\n stack.push([item, reason]);\n item.state = \"pending\";\n for (const dep of item.service.dependencies) {\n item.dependencies[dep.referenceName] = this.visitReference({\n type: \"service-reference\",\n service: item.service,\n referenceName: dep.referenceName,\n value: dep\n });\n }\n item.state = \"done\";\n stack.pop();\n }\n\n private visitReference(ref: InterfaceReference) {\n const itemOrItems = this.findServices(ref);\n if (Array.isArray(itemOrItems)) {\n for (const child of itemOrItems) {\n this.visitItem(child, ref);\n }\n return itemOrItems.map((item) => item.service);\n } else {\n this.visitItem(itemOrItems, ref);\n return itemOrItems.service;\n }\n }\n\n private registerService(item: GraphItem, spec: InterfaceSpec) {\n this.serviceLookup.register(item.service, spec);\n this.serviceToGraphItem.set(item.service, item);\n }\n\n private findServices(ref: InterfaceReference): GraphItem | GraphItem[] {\n const spec = ref.value;\n const lookupResult = this.serviceLookup.lookup(spec);\n switch (lookupResult.type) {\n case \"unimplemented\": {\n const message = unimplementedInterfaceMessage(ref);\n throw new Error(ErrorId.INTERFACE_NOT_FOUND, message);\n }\n case \"ambiguous\": {\n const message = ambiguousInterfaceMessage(ref, lookupResult.choices);\n throw new Error(ErrorId.AMBIGUOUS_DEPENDENCY, message);\n }\n }\n\n const value = lookupResult.value;\n if (Array.isArray(value)) {\n return value.map((service) => this.getGraphItem(service));\n } else {\n return this.getGraphItem(value);\n }\n }\n\n private getGraphItem(service: ServiceRepr): GraphItem {\n const graphItem = this.serviceToGraphItem.get(service);\n if (!graphItem || graphItem.service !== service) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find matching graph item for service '${service.id}'.`\n );\n }\n return graphItem;\n }\n\n private throwCycleError(item: GraphItem, reason: ItemVisitReason) {\n // Find the item on the stack (it must be there because we detected a cycle).\n const stack = this.stack;\n const ownIndex = stack.findIndex((entry) => entry[0] === item);\n if (ownIndex === -1) {\n throw new Error(ErrorId.INTERNAL, \"Failed to find cycle participant on the stack.\");\n }\n\n const cycleInfo = stack\n .slice(ownIndex)\n .concat([[item, reason]])\n .map<string>((entry, index) => {\n const item = entry[0];\n const reason = index === 0 ? \"root\" : entry[1]; // info before the cycle does not matter\n\n let message = `'${item.service.id}'`;\n if (typeof reason === \"object\") {\n message += ` (${referenceDetail(reason)})`;\n }\n return message;\n });\n throw new Error(\n ErrorId.DEPENDENCY_CYCLE,\n `Detected dependency cycle: ${cycleInfo.join(\" => \")}.`\n );\n }\n}\n\nfunction unimplementedInterfaceMessage(ref: InterfaceReference): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n switch (ref.type) {\n case \"service-reference\":\n return `Service '${ref.service.id}' requires an unimplemented interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n case \"ui-reference\":\n return `The UI of package '${ref.packageName}' requires an unimplemented interface ${interfaceText}.`;\n case \"framework-reference\":\n return `The framework requires an unimplemented interface ${interfaceText}.`;\n }\n}\n\nfunction ambiguousInterfaceMessage(ref: InterfaceReference, choices: AmbiguousChoice[]): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n const serviceChoices = renderAmbiguousServiceChoices(choices);\n\n let message = \"\";\n switch (ref.type) {\n case \"service-reference\":\n message = `Service '${ref.service.id}' requires the ambiguous interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n break;\n case \"ui-reference\":\n message = `The UI of package '${ref.packageName}' requires the ambiguous interface ${interfaceText}.`;\n break;\n case \"framework-reference\":\n message = `The framework requires the ambiguous interface ${interfaceText}.`;\n break;\n }\n message += ` Possible choices are ${serviceChoices}.`;\n return message;\n}\n\nfunction referenceDetail(reason: InterfaceReference) {\n switch (reason.type) {\n case \"service-reference\":\n return `'${reason.referenceName}' providing '${reason.value.interfaceName}'`;\n case \"ui-reference\":\n return `UI of package '${reason.packageName}' requiring '${reason.value.interfaceName}'`;\n case \"framework-reference\":\n return `framework requiring '${reason.value.interfaceName}'`;\n }\n}\n"],"names":["item","reason"],"mappings":";;;;;AAsDO,SAAS,mBAAmB,OAAsD,EAAA;AACrF,EAAM,MAAA,QAAA,GAAW,IAAI,QAAA,CAAS,OAAO,CAAA,CAAA;AACrC,EAAA,QAAA,CAAS,MAAO,EAAA,CAAA;AAChB,EAAO,OAAA;AAAA,IACH,aAAA,EAAe,SAAS,gBAAiB,EAAA;AAAA,IACzC,mBAAA,EAAqB,SAAS,uBAAwB,EAAA;AAAA,GAC1D,CAAA;AACJ,CAAA;AA8BA,MAAM,QAAS,CAAA;AAAA,EACH,kBAAA,CAAA;AAAA,EACA,KAAA,CAAA;AAAA,EACA,aAAA,GAAgB,IAAI,aAAc,EAAA,CAAA;AAAA,EAClC,kBAAA,uBAAyB,GAA4B,EAAA,CAAA;AAAA;AAAA,EAGrD,QAAsD,EAAC,CAAA;AAAA,EAE/D,YAAY,OAA4B,EAAA;AACpC,IAAK,IAAA,CAAA,kBAAA,GAAqB,OAAQ,CAAA,kBAAA,IAAsB,EAAC,CAAA;AACzD,IAAM,MAAA,QAAA,GAAW,OAAQ,CAAA,QAAA,IA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