@layerzerolabs/base-definitions 0.2.6 → 0.2.7

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.
@@ -4,51 +4,51 @@
4
4
 
5
5
  CLI Building entry: src/definitions.ts, src/index.ts, src/lzyringe.ts, src/registrarBuilder.ts, src/utils.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
- CLI tsup v8.4.0
7
+ CLI tsup v8.5.1
8
8
  CLI Using tsup config: /home/runner/work/monorepo-internal/monorepo-internal/packages/framework/definitions/base-definitions/tsup.config.ts
9
9
  CLI Target: ES2023
10
10
  CLI Cleaning output folder
11
11
  CJS Build start
12
12
  ESM Build start
13
13
  CJS dist/definitions.cjs 742.00 B
14
- CJS dist/index.cjs 1.42 KB
15
14
  CJS dist/lzyringe.cjs 302.00 B
16
- CJS dist/registrarBuilder.cjs 478.00 B
17
- CJS dist/utils.cjs 341.00 B
15
+ CJS dist/index.cjs 1.42 KB
18
16
  CJS dist/JKTGUQY4.cjs 1.70 KB
17
+ CJS dist/utils.cjs 341.00 B
18
+ CJS dist/registrarBuilder.cjs 481.00 B
19
+ CJS dist/3AKKEY5J.cjs 6.27 KB
19
20
  CJS dist/OIBEOODK.cjs 812.00 B
20
- CJS dist/YJF4D23A.cjs 245.00 B
21
- CJS dist/O7HENH25.cjs 4.66 KB
22
21
  CJS dist/VTYVOFLM.cjs 1.68 KB
22
+ CJS dist/YJF4D23A.cjs 245.00 B
23
23
  CJS dist/definitions.cjs.map 76.00 B
24
- CJS dist/index.cjs.map 70.00 B
25
24
  CJS dist/lzyringe.cjs.map 73.00 B
25
+ CJS dist/index.cjs.map 70.00 B
26
26
  CJS dist/registrarBuilder.cjs.map 81.00 B
27
- CJS dist/utils.cjs.map 70.00 B
28
- CJS dist/JKTGUQY4.cjs.map 3.78 KB
27
+ CJS dist/3AKKEY5J.cjs.map 17.00 KB
29
28
  CJS dist/OIBEOODK.cjs.map 1.83 KB
30
- CJS dist/YJF4D23A.cjs.map 73.00 B
31
- CJS dist/O7HENH25.cjs.map 12.79 KB
32
29
  CJS dist/VTYVOFLM.cjs.map 14.00 KB
33
- CJS ⚡️ Build success in 272ms
34
- ESM dist/definitions.js 216.00 B
35
- ESM dist/lzyringe.js 146.00 B
30
+ CJS dist/YJF4D23A.cjs.map 73.00 B
31
+ CJS dist/JKTGUQY4.cjs.map 3.78 KB
32
+ CJS dist/utils.cjs.map 70.00 B
33
+ CJS ⚡️ Build success in 166ms
36
34
  ESM dist/index.js 389.00 B
35
+ ESM dist/lzyringe.js 146.00 B
36
+ ESM dist/definitions.js 216.00 B
37
37
  ESM dist/JJBD65HU.js 1.65 KB
38
38
  ESM dist/registrarBuilder.js 191.00 B
39
+ ESM dist/H6QNIXMU.js 6.07 KB
39
40
  ESM dist/utils.js 173.00 B
40
41
  ESM dist/EAPC6542.js 733.00 B
41
- ESM dist/4DUQS2FO.js 4.51 KB
42
- ESM dist/NQBJGAQC.js 1.47 KB
43
42
  ESM dist/VUOMXK5T.js 222.00 B
44
- ESM dist/definitions.js.map 75.00 B
45
- ESM dist/lzyringe.js.map 72.00 B
43
+ ESM dist/NQBJGAQC.js 1.47 KB
46
44
  ESM dist/index.js.map 69.00 B
45
+ ESM dist/lzyringe.js.map 72.00 B
46
+ ESM dist/definitions.js.map 75.00 B
47
47
  ESM dist/JJBD65HU.js.map 3.78 KB
48
+ ESM dist/H6QNIXMU.js.map 16.99 KB
48
49
  ESM dist/registrarBuilder.js.map 80.00 B
49
50
  ESM dist/utils.js.map 69.00 B
50
51
  ESM dist/EAPC6542.js.map 1.82 KB
51
- ESM dist/4DUQS2FO.js.map 12.78 KB
52
- ESM dist/NQBJGAQC.js.map 13.99 KB
53
52
  ESM dist/VUOMXK5T.js.map 72.00 B
54
- ESM ⚡️ Build success in 273ms
53
+ ESM dist/NQBJGAQC.js.map 13.99 KB
54
+ ESM ⚡️ Build success in 167ms
@@ -2,3 +2,7 @@
2
2
  > @layerzerolabs/base-definitions@0.0.2 lint /home/runner/work/monorepo-internal/monorepo-internal/packages/framework/definitions/base-definitions
3
3
  > eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)
4
4
 
5
+ (node:23094) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/monorepo-internal/monorepo-internal/eslint.config.js?mtime=1764985257978 is not specified and it doesn't parse as CommonJS.
6
+ Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
7
+ To eliminate this warning, add "type": "module" to /home/runner/work/monorepo-internal/monorepo-internal/package.json.
8
+ (Use `node --trace-warnings ...` to show where the warning was created)
@@ -5,13 +5,13 @@
5
5
  Testing types with tsc and vue-tsc is an experimental feature.
6
6
  Breaking changes might not follow SemVer, please pin Vitest's version when using it.
7
7
 
8
-  RUN  v3.2.3 /home/runner/work/monorepo-internal/monorepo-internal/packages/framework/definitions/base-definitions
8
+  RUN  v3.2.4 /home/runner/work/monorepo-internal/monorepo-internal/packages/framework/definitions/base-definitions
9
9
 
10
- ✓ test/registrarBuilder.test.ts (4 tests) 9ms
10
+ ✓ test/registrarBuilder.test.ts (7 tests) 11ms
11
11
 
12
12
   Test Files  1 passed (1)
13
-  Tests  4 passed (4)
13
+  Tests  7 passed (7)
14
14
  Type Errors  no errors
15
-  Start at  21:06:01
16
-  Duration  626ms (transform 185ms, setup 0ms, collect 159ms, tests 9ms, environment 0ms, prepare 192ms)
15
+  Start at  01:44:45
16
+  Duration  575ms (transform 107ms, setup 0ms, collect 99ms, tests 11ms, environment 0ms, prepare 126ms)
17
17
 
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var YJF4D23A_cjs = require('./YJF4D23A.cjs');
4
+ var dependencyGraph = require('@layerzerolabs/dependency-graph');
4
5
  var dfs = require('@layerzerolabs/dfs');
5
6
 
6
7
  var RegistrarBuilder = class {
@@ -8,13 +9,24 @@ var RegistrarBuilder = class {
8
9
  YJF4D23A_cjs.__name(this, "RegistrarBuilder");
9
10
  }
10
11
  defaultHandler;
12
+ namedHandlers = {};
13
+ classHandlers = [];
14
+ classAliases = [];
15
+ getEntrypoints = /* @__PURE__ */ YJF4D23A_cjs.__name((node) => [
16
+ node
17
+ ], "getEntrypoints");
11
18
  constructor() {
12
19
  this.defaultHandler = async (node) => {
13
20
  throw new Error(`This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`);
14
21
  };
15
22
  }
16
- classHandlers = [];
17
- classAliases = [];
23
+ addNamedHandler(node, fn) {
24
+ if (this.namedHandlers[node.name]) {
25
+ throw new Error(`Cannot define a named handler for an identifier that already has a handler: ${node.name}`);
26
+ }
27
+ this.namedHandlers[node.name] = fn;
28
+ return this;
29
+ }
18
30
  /**
19
31
  * Add a function fn to be called on all nodes of class
20
32
  * @param nodeClass
@@ -93,6 +105,14 @@ var RegistrarBuilder = class {
93
105
  });
94
106
  return this;
95
107
  }
108
+ /**
109
+ * Set a custom entrypoint getter function. By default, the entrypoint getter
110
+ * returns the node passed in as the sole entrypoint.
111
+ */
112
+ setEntrypointsGetter(fn) {
113
+ this.getEntrypoints = fn;
114
+ return this;
115
+ }
96
116
  /**
97
117
  * Add a default function fn to be used on nodes whose types do not have handlers.
98
118
  * If this is not set, the default behaviour will be to throw
@@ -112,6 +132,19 @@ var RegistrarBuilder = class {
112
132
  return node;
113
133
  }, "mergedNodePrehandler");
114
134
  const mergedNodeHandler = /* @__PURE__ */ YJF4D23A_cjs.__name(async (node, ancestry) => {
135
+ if (node.name === "_virtualEntrypoint") {
136
+ return {
137
+ key: node.name,
138
+ value: void 0
139
+ };
140
+ }
141
+ const namedHandler = this.namedHandlers[node.name];
142
+ if (namedHandler) {
143
+ return {
144
+ key: node.name,
145
+ value: await namedHandler(node, ancestry)
146
+ };
147
+ }
115
148
  for (const { nodeClass, identifier, fn } of this.classHandlers) {
116
149
  if (node instanceof nodeClass) {
117
150
  return {
@@ -126,7 +159,24 @@ var RegistrarBuilder = class {
126
159
  };
127
160
  }, "mergedNodeHandler");
128
161
  return {
129
- traverseDependencies: /* @__PURE__ */ YJF4D23A_cjs.__name(async (node) => await dfs.dfs(node, mergedNodeHandler, mergedNodePrehandler)(), "traverseDependencies")
162
+ traverseDependencies: /* @__PURE__ */ YJF4D23A_cjs.__name(async (node) => {
163
+ const entrypoints = this.getEntrypoints(node);
164
+ if (entrypoints.length === 1) return await dfs.dfs(node, mergedNodeHandler, mergedNodePrehandler)();
165
+ const dependencies = entrypoints.reduce((acc, cur) => {
166
+ acc[cur.name] = cur;
167
+ return acc;
168
+ }, {});
169
+ let VirtualNode = class VirtualNode extends dependencyGraph.DependencyNode {
170
+ static {
171
+ YJF4D23A_cjs.__name(this, "VirtualNode");
172
+ }
173
+ };
174
+ const virtualEntrypoint = new VirtualNode({
175
+ name: "_virtualEntrypoint",
176
+ dependencies
177
+ });
178
+ return await dfs.dfs(virtualEntrypoint, mergedNodeHandler, mergedNodePrehandler)();
179
+ }, "traverseDependencies")
130
180
  };
131
181
  }
132
182
  };
@@ -146,5 +196,5 @@ var multiplexOrderedRegistrars = /* @__PURE__ */ YJF4D23A_cjs.__name((...registr
146
196
 
147
197
  exports.RegistrarBuilder = RegistrarBuilder;
148
198
  exports.multiplexOrderedRegistrars = multiplexOrderedRegistrars;
149
- //# sourceMappingURL=O7HENH25.cjs.map
150
- //# sourceMappingURL=O7HENH25.cjs.map
199
+ //# sourceMappingURL=3AKKEY5J.cjs.map
200
+ //# sourceMappingURL=3AKKEY5J.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/registrarBuilder.ts"],"names":["RegistrarBuilder","defaultHandler","namedHandlers","classHandlers","classAliases","getEntrypoints","node","Error","JSON","stringify","addNamedHandler","fn","name","addClassHandler","nodeClass","identifier","find","fromNodeClass","i","handler","prototype","isPrototypeOf","splice","push","addClassAlias","getToNode","alias","setEntrypointsGetter","addDefault","build","mergedNodePrehandler","toNode","mergedNodeHandler","__name","ancestry","key","value","undefined","namedHandler","traverseDependencies","entrypoints","length","dfs","dependencies","reduce","acc","cur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CAAYI,MAAAA,CAAO,CAACC,KAAKC,GAAAA,KAAAA;AAC1CD,UAAAA,GAAAA,CAAIC,GAAAA,CAAIlC,IAAI,CAAA,GAAIkC,GAAAA;AAChB,UAAA,OAAOD,GAAAA;AACX,QAAA,CAAA,EAAG,EAAC,CAAA;AAEJ,QAAA,IAAME,WAAAA,GAAN,MAAMA,WAAAA,SAAoBC,8BAAAA,CAAAA;UAnQ1C;;;AAmQ0D,SAAA;AAE1C,QAAA,MAAMC,iBAAAA,GAAoB,IAAIF,WAAAA,CAAY;UACtCnC,IAAAA,EAAM,oBAAA;AACN+B,UAAAA;SACJ,CAAA;AAEA,QAAA,OAAO,MAAMD,OAAAA,CACTO,iBAAAA,EACAjB,iBAAAA,EACAF,oBAAAA,CAAAA,EAAAA;MAER,CAAA,EAtBsB,sBAAA;AAuB1B,KAAA;AACJ,EAAA;AACJ;AAEO,IAAMoB,0BAAAA,2CAGNC,UAAAA,KAAAA;AAEH,EAAA,MAAMC,kBAAAA,GAID;AACDb,IAAAA,oBAAAA,gDACOc,MAAAA,KAAAA;AAEH,MAAA,MAAMR,MAA2B,EAAC;AAClC,MAAA,KAAA,MAAW,CAACjC,IAAAA,EAAM0C,SAAAA,CAAAA,IAAcH,UAAAA,EAAY;AACxC,QAAA,MAAMI,MAAAA,GAAS,MAAMD,SAAAA,CAAUf,oBAAAA,CAAoB,GAAIc,MAAAA,CAAAA;AACvDR,QAAAA,GAAAA,CAAIjC,IAAAA,CAAAA,GAAQ2C,MAAAA;AAChB,MAAA;AAEA,MAAA,OAAOV,GAAAA;IACX,CAAA,EAVsB,sBAAA;AAW1B,GAAA;AAEA,EAAA,OAAOO,kBAAAA;AACX,CAAA,EAxB0C,4BAAA","file":"3AKKEY5J.cjs","sourcesContent":["import type { Dependencies } from '@layerzerolabs/dependency-graph';\nimport { DependencyNode } from '@layerzerolabs/dependency-graph';\nimport type { Registrar } from '@layerzerolabs/dfs';\nimport { dfs } from '@layerzerolabs/dfs';\nexport type { Registrar };\n\ntype NodeClass = new (...args: any[]) => DependencyNode;\ntype AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;\n\n/**\n * <!-- anchor:RegistrarBuilder -->\n * A builder class for a {@link Registrar}.\n * Builds a registrar that switches on node class--i.e., enables defining\n * specific registration behaviour for specific node classes.\n * Intelligently handles inheritance, always using the most specific\n * handler defined for any given class.\n * The registrar built shall return an object that maps from a handler ID,\n * to a map from node name to the return of each handler,\n * e.g., { objectNodes: { myObjectNode: 'my handler return' }}.\n * This can be used to extract a specific resolved value after resolving the tree,\n * given that the handlers return the resolved nodes.\n */\nexport class RegistrarBuilder<_ReturnTypes> {\n protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n\n protected namedHandlers: Record<\n string,\n (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>\n > = {};\n\n protected classHandlers: {\n nodeClass: AbstractNodeClass;\n identifier: string;\n fn: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n }[] = [];\n\n protected classAliases: {\n fromNodeClass: AbstractNodeClass;\n getToNode: (from: DependencyNode) => DependencyNode;\n }[] = [];\n\n protected getEntrypoints: (node: DependencyNode) => DependencyNode[] = (node) => [node];\n\n constructor() {\n this.defaultHandler = async (node: DependencyNode) => {\n throw new Error(\n `This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,\n );\n };\n }\n\n public addNamedHandler<\n Node extends DependencyNode,\n Function extends (node: Node, ancestry: DependencyNode[]) => Promise<any>,\n >(node: DependencyNode, fn: Function) {\n if (this.namedHandlers[node.name]) {\n throw new Error(\n `Cannot define a named handler for an identifier that already has a handler: ${node.name}`,\n );\n }\n this.namedHandlers[node.name] = fn as unknown as (\n node: DependencyNode,\n ancestry: DependencyNode[],\n ) => Promise<any>;\n return this as RegistrarBuilder<\n _ReturnTypes & {\n ['namedHandlers']: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n\n /**\n * Add a function fn to be called on all nodes of class\n * @param nodeClass\n * @param identifier\n * @param fn\n */\n public addClassHandler<\n Identifier extends string,\n _NodeClass extends NodeClass,\n Function extends (node: InstanceType<_NodeClass>, ancestry: DependencyNode[]) => any,\n >(nodeClass: _NodeClass, identifier: Identifier, fn: Function) {\n if (this.classAliases.find(({ fromNodeClass }) => fromNodeClass === nodeClass)) {\n throw new Error(\n `Cannot define a handler for a class that is aliased to something else. This would be pointless`,\n );\n }\n\n let i = 0;\n for (const handler of this.classHandlers) {\n //check if we are overriding an existing handler\n if (handler.nodeClass === nodeClass) {\n this.classHandlers[i] = {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n };\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n //check if the new handler has an ordering requirement against an existing handler\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (handler.nodeClass.prototype.isPrototypeOf(nodeClass.prototype)) {\n this.classHandlers.splice(i, 0, {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n i++;\n }\n\n this.classHandlers.push({\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n\n /**\n * Alias some class *from*, such that any node of that class in the tree will be\n * treated as though it were some other node *to*. The *to* node is given as a\n * function returning a class instance, so that its dependencies, name, etc can be fixed.\n * The registrar will ignore the dependencies of the original node, and follow the alias's\n * dependencies instead.\n *\n * Additionally, this method does not affect the typing of the registrar. The output type\n * of the build() method will not reflect the alias.\n *\n * @param fromNodeClass alias instances of this class\n * @param getToNode method that returns the *to* part of the alias\n */\n public addClassAlias<FromNodeClass extends AbstractNodeClass>(\n fromNodeClass: FromNodeClass,\n getToNode: (fromNodeInstance: InstanceType<FromNodeClass>) => DependencyNode,\n ) {\n if (this.classHandlers.find(({ nodeClass }) => nodeClass === fromNodeClass)) {\n throw new Error(`Cannot define an alias for a class that already has a handler`);\n }\n\n let i = 0;\n for (const alias of this.classAliases) {\n //check if we are overriding an existing alias\n if (alias.fromNodeClass === fromNodeClass) {\n this.classAliases[i] = {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n };\n\n return this;\n }\n //check if the new alias has an ordering requirement against an existing alias\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (alias.fromNodeClass.prototype.isPrototypeOf(fromNodeClass.prototype)) {\n this.classAliases.splice(i, 0, {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n i++;\n }\n\n this.classAliases.push({\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n\n /**\n * Set a custom entrypoint getter function. By default, the entrypoint getter\n * returns the node passed in as the sole entrypoint.\n */\n public setEntrypointsGetter(fn: (node: DependencyNode) => DependencyNode[]) {\n this.getEntrypoints = fn;\n return this;\n }\n\n /**\n * Add a default function fn to be used on nodes whose types do not have handlers.\n * If this is not set, the default behaviour will be to throw\n * @param fn\n */\n public addDefault(fn: (node: DependencyNode, ancestry: DependencyNode[]) => any) {\n this.defaultHandler = fn;\n return this;\n }\n\n public build(): Registrar<_ReturnTypes> {\n const mergedNodePrehandler = (node: DependencyNode) => {\n for (const { fromNodeClass, getToNode: toNode } of this.classAliases) {\n if (node instanceof fromNodeClass) {\n return toNode(node);\n }\n }\n return node;\n };\n\n const mergedNodeHandler = async (node: DependencyNode, ancestry: DependencyNode[]) => {\n if (node.name === '_virtualEntrypoint') {\n return {\n key: node.name,\n value: undefined,\n };\n }\n\n const namedHandler = this.namedHandlers[node.name];\n if (namedHandler) {\n return {\n key: node.name,\n value: await namedHandler(node, ancestry),\n };\n }\n\n for (const { nodeClass, identifier, fn } of this.classHandlers) {\n if (node instanceof nodeClass) {\n return {\n key: identifier,\n value: await fn(node, ancestry),\n };\n }\n }\n\n return {\n key: 'default',\n value: await this.defaultHandler(node, ancestry),\n };\n };\n return {\n traverseDependencies: async (node: DependencyNode) => {\n const entrypoints = this.getEntrypoints(node);\n if (entrypoints.length === 1)\n return await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)();\n\n const dependencies = entrypoints.reduce((acc, cur) => {\n acc[cur.name] = cur;\n return acc;\n }, {} as Dependencies);\n\n class VirtualNode extends DependencyNode {}\n\n const virtualEntrypoint = new VirtualNode({\n name: '_virtualEntrypoint',\n dependencies,\n });\n\n return await dfs<_ReturnTypes>(\n virtualEntrypoint,\n mergedNodeHandler,\n mergedNodePrehandler,\n )();\n },\n };\n }\n}\n\nexport const multiplexOrderedRegistrars = <\n const T extends [name: string, registrar: Registrar<{}>][],\n>(\n ...registrars: T\n) => {\n const multiplexRegistrar: Registrar<{\n [K in Extract<keyof T, number> as T[K] extends readonly [infer Name extends string, any]\n ? Name\n : never]: T[K] extends readonly [any, Registrar<infer Ret>] ? Ret : never;\n }> = {\n traverseDependencies: async (\n ...params: Parameters<Registrar<any>['traverseDependencies']>\n ) => {\n const acc: Record<string, any> = {};\n for (const [name, registrar] of registrars) {\n const result = await registrar.traverseDependencies(...params);\n acc[name] = result;\n }\n\n return acc;\n },\n } as any;\n\n return multiplexRegistrar;\n};\n"]}
@@ -1,4 +1,5 @@
1
1
  import { __name } from './VUOMXK5T.js';
2
+ import { DependencyNode } from '@layerzerolabs/dependency-graph';
2
3
  import { dfs } from '@layerzerolabs/dfs';
3
4
 
4
5
  var RegistrarBuilder = class {
@@ -6,13 +7,24 @@ var RegistrarBuilder = class {
6
7
  __name(this, "RegistrarBuilder");
7
8
  }
8
9
  defaultHandler;
10
+ namedHandlers = {};
11
+ classHandlers = [];
12
+ classAliases = [];
13
+ getEntrypoints = /* @__PURE__ */ __name((node) => [
14
+ node
15
+ ], "getEntrypoints");
9
16
  constructor() {
10
17
  this.defaultHandler = async (node) => {
11
18
  throw new Error(`This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`);
12
19
  };
13
20
  }
14
- classHandlers = [];
15
- classAliases = [];
21
+ addNamedHandler(node, fn) {
22
+ if (this.namedHandlers[node.name]) {
23
+ throw new Error(`Cannot define a named handler for an identifier that already has a handler: ${node.name}`);
24
+ }
25
+ this.namedHandlers[node.name] = fn;
26
+ return this;
27
+ }
16
28
  /**
17
29
  * Add a function fn to be called on all nodes of class
18
30
  * @param nodeClass
@@ -91,6 +103,14 @@ var RegistrarBuilder = class {
91
103
  });
92
104
  return this;
93
105
  }
106
+ /**
107
+ * Set a custom entrypoint getter function. By default, the entrypoint getter
108
+ * returns the node passed in as the sole entrypoint.
109
+ */
110
+ setEntrypointsGetter(fn) {
111
+ this.getEntrypoints = fn;
112
+ return this;
113
+ }
94
114
  /**
95
115
  * Add a default function fn to be used on nodes whose types do not have handlers.
96
116
  * If this is not set, the default behaviour will be to throw
@@ -110,6 +130,19 @@ var RegistrarBuilder = class {
110
130
  return node;
111
131
  }, "mergedNodePrehandler");
112
132
  const mergedNodeHandler = /* @__PURE__ */ __name(async (node, ancestry) => {
133
+ if (node.name === "_virtualEntrypoint") {
134
+ return {
135
+ key: node.name,
136
+ value: void 0
137
+ };
138
+ }
139
+ const namedHandler = this.namedHandlers[node.name];
140
+ if (namedHandler) {
141
+ return {
142
+ key: node.name,
143
+ value: await namedHandler(node, ancestry)
144
+ };
145
+ }
113
146
  for (const { nodeClass, identifier, fn } of this.classHandlers) {
114
147
  if (node instanceof nodeClass) {
115
148
  return {
@@ -124,7 +157,24 @@ var RegistrarBuilder = class {
124
157
  };
125
158
  }, "mergedNodeHandler");
126
159
  return {
127
- traverseDependencies: /* @__PURE__ */ __name(async (node) => await dfs(node, mergedNodeHandler, mergedNodePrehandler)(), "traverseDependencies")
160
+ traverseDependencies: /* @__PURE__ */ __name(async (node) => {
161
+ const entrypoints = this.getEntrypoints(node);
162
+ if (entrypoints.length === 1) return await dfs(node, mergedNodeHandler, mergedNodePrehandler)();
163
+ const dependencies = entrypoints.reduce((acc, cur) => {
164
+ acc[cur.name] = cur;
165
+ return acc;
166
+ }, {});
167
+ let VirtualNode = class VirtualNode extends DependencyNode {
168
+ static {
169
+ __name(this, "VirtualNode");
170
+ }
171
+ };
172
+ const virtualEntrypoint = new VirtualNode({
173
+ name: "_virtualEntrypoint",
174
+ dependencies
175
+ });
176
+ return await dfs(virtualEntrypoint, mergedNodeHandler, mergedNodePrehandler)();
177
+ }, "traverseDependencies")
128
178
  };
129
179
  }
130
180
  };
@@ -143,5 +193,5 @@ var multiplexOrderedRegistrars = /* @__PURE__ */ __name((...registrars) => {
143
193
  }, "multiplexOrderedRegistrars");
144
194
 
145
195
  export { RegistrarBuilder, multiplexOrderedRegistrars };
146
- //# sourceMappingURL=4DUQS2FO.js.map
147
- //# sourceMappingURL=4DUQS2FO.js.map
196
+ //# sourceMappingURL=H6QNIXMU.js.map
197
+ //# sourceMappingURL=H6QNIXMU.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/registrarBuilder.ts"],"names":["RegistrarBuilder","defaultHandler","namedHandlers","classHandlers","classAliases","getEntrypoints","node","Error","JSON","stringify","addNamedHandler","fn","name","addClassHandler","nodeClass","identifier","find","fromNodeClass","i","handler","prototype","isPrototypeOf","splice","push","addClassAlias","getToNode","alias","setEntrypointsGetter","addDefault","build","mergedNodePrehandler","toNode","mergedNodeHandler","ancestry","key","value","undefined","namedHandler","traverseDependencies","entrypoints","length","dfs","dependencies","reduce","acc","cur","VirtualNode","DependencyNode","virtualEntrypoint","multiplexOrderedRegistrars","registrars","multiplexRegistrar","params","registrar","result"],"mappings":";;;;AAsBO,IAAMA,mBAAN,MAAMA;EArBb;;;AAsBcC,EAAAA,cAAAA;A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X,QAAA,CAAA,EAAG,EAAC,CAAA;AAEJ,QAAA,IAAME,WAAAA,GAAN,MAAMA,WAAAA,SAAoBC,cAAAA,CAAAA;UAnQ1C;;;AAmQ0D,SAAA;AAE1C,QAAA,MAAMC,iBAAAA,GAAoB,IAAIF,WAAAA,CAAY;UACtClC,IAAAA,EAAM,oBAAA;AACN8B,UAAAA;SACJ,CAAA;AAEA,QAAA,OAAO,MAAMD,GAAAA,CACTO,iBAAAA,EACAhB,iBAAAA,EACAF,oBAAAA,CAAAA,EAAAA;MAER,CAAA,EAtBsB,sBAAA;AAuB1B,KAAA;AACJ,EAAA;AACJ;AAEO,IAAMmB,0BAAAA,8BAGNC,UAAAA,KAAAA;AAEH,EAAA,MAAMC,kBAAAA,GAID;AACDb,IAAAA,oBAAAA,mCACOc,MAAAA,KAAAA;AAEH,MAAA,MAAMR,MAA2B,EAAC;AAClC,MAAA,KAAA,MAAW,CAAChC,IAAAA,EAAMyC,SAAAA,CAAAA,IAAcH,UAAAA,EAAY;AACxC,QAAA,MAAMI,MAAAA,GAAS,MAAMD,SAAAA,CAAUf,oBAAAA,CAAoB,GAAIc,MAAAA,CAAAA;AACvDR,QAAAA,GAAAA,CAAIhC,IAAAA,CAAAA,GAAQ0C,MAAAA;AAChB,MAAA;AAEA,MAAA,OAAOV,GAAAA;IACX,CAAA,EAVsB,sBAAA;AAW1B,GAAA;AAEA,EAAA,OAAOO,kBAAAA;AACX,CAAA,EAxB0C,4BAAA","file":"H6QNIXMU.js","sourcesContent":["import type { Dependencies } from '@layerzerolabs/dependency-graph';\nimport { DependencyNode } from '@layerzerolabs/dependency-graph';\nimport type { Registrar } from '@layerzerolabs/dfs';\nimport { dfs } from '@layerzerolabs/dfs';\nexport type { Registrar };\n\ntype NodeClass = new (...args: any[]) => DependencyNode;\ntype AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;\n\n/**\n * <!-- anchor:RegistrarBuilder -->\n * A builder class for a {@link Registrar}.\n * Builds a registrar that switches on node class--i.e., enables defining\n * specific registration behaviour for specific node classes.\n * Intelligently handles inheritance, always using the most specific\n * handler defined for any given class.\n * The registrar built shall return an object that maps from a handler ID,\n * to a map from node name to the return of each handler,\n * e.g., { objectNodes: { myObjectNode: 'my handler return' }}.\n * This can be used to extract a specific resolved value after resolving the tree,\n * given that the handlers return the resolved nodes.\n */\nexport class RegistrarBuilder<_ReturnTypes> {\n protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n\n protected namedHandlers: Record<\n string,\n (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>\n > = {};\n\n protected classHandlers: {\n nodeClass: AbstractNodeClass;\n identifier: string;\n fn: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n }[] = [];\n\n protected classAliases: {\n fromNodeClass: AbstractNodeClass;\n getToNode: (from: DependencyNode) => DependencyNode;\n }[] = [];\n\n protected getEntrypoints: (node: DependencyNode) => DependencyNode[] = (node) => [node];\n\n constructor() {\n this.defaultHandler = async (node: DependencyNode) => {\n throw new Error(\n `This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,\n );\n };\n }\n\n public addNamedHandler<\n Node extends DependencyNode,\n Function extends (node: Node, ancestry: DependencyNode[]) => Promise<any>,\n >(node: DependencyNode, fn: Function) {\n if (this.namedHandlers[node.name]) {\n throw new Error(\n `Cannot define a named handler for an identifier that already has a handler: ${node.name}`,\n );\n }\n this.namedHandlers[node.name] = fn as unknown as (\n node: DependencyNode,\n ancestry: DependencyNode[],\n ) => Promise<any>;\n return this as RegistrarBuilder<\n _ReturnTypes & {\n ['namedHandlers']: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n\n /**\n * Add a function fn to be called on all nodes of class\n * @param nodeClass\n * @param identifier\n * @param fn\n */\n public addClassHandler<\n Identifier extends string,\n _NodeClass extends NodeClass,\n Function extends (node: InstanceType<_NodeClass>, ancestry: DependencyNode[]) => any,\n >(nodeClass: _NodeClass, identifier: Identifier, fn: Function) {\n if (this.classAliases.find(({ fromNodeClass }) => fromNodeClass === nodeClass)) {\n throw new Error(\n `Cannot define a handler for a class that is aliased to something else. This would be pointless`,\n );\n }\n\n let i = 0;\n for (const handler of this.classHandlers) {\n //check if we are overriding an existing handler\n if (handler.nodeClass === nodeClass) {\n this.classHandlers[i] = {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n };\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n //check if the new handler has an ordering requirement against an existing handler\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (handler.nodeClass.prototype.isPrototypeOf(nodeClass.prototype)) {\n this.classHandlers.splice(i, 0, {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n i++;\n }\n\n this.classHandlers.push({\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n\n /**\n * Alias some class *from*, such that any node of that class in the tree will be\n * treated as though it were some other node *to*. The *to* node is given as a\n * function returning a class instance, so that its dependencies, name, etc can be fixed.\n * The registrar will ignore the dependencies of the original node, and follow the alias's\n * dependencies instead.\n *\n * Additionally, this method does not affect the typing of the registrar. The output type\n * of the build() method will not reflect the alias.\n *\n * @param fromNodeClass alias instances of this class\n * @param getToNode method that returns the *to* part of the alias\n */\n public addClassAlias<FromNodeClass extends AbstractNodeClass>(\n fromNodeClass: FromNodeClass,\n getToNode: (fromNodeInstance: InstanceType<FromNodeClass>) => DependencyNode,\n ) {\n if (this.classHandlers.find(({ nodeClass }) => nodeClass === fromNodeClass)) {\n throw new Error(`Cannot define an alias for a class that already has a handler`);\n }\n\n let i = 0;\n for (const alias of this.classAliases) {\n //check if we are overriding an existing alias\n if (alias.fromNodeClass === fromNodeClass) {\n this.classAliases[i] = {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n };\n\n return this;\n }\n //check if the new alias has an ordering requirement against an existing alias\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (alias.fromNodeClass.prototype.isPrototypeOf(fromNodeClass.prototype)) {\n this.classAliases.splice(i, 0, {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n i++;\n }\n\n this.classAliases.push({\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n\n /**\n * Set a custom entrypoint getter function. By default, the entrypoint getter\n * returns the node passed in as the sole entrypoint.\n */\n public setEntrypointsGetter(fn: (node: DependencyNode) => DependencyNode[]) {\n this.getEntrypoints = fn;\n return this;\n }\n\n /**\n * Add a default function fn to be used on nodes whose types do not have handlers.\n * If this is not set, the default behaviour will be to throw\n * @param fn\n */\n public addDefault(fn: (node: DependencyNode, ancestry: DependencyNode[]) => any) {\n this.defaultHandler = fn;\n return this;\n }\n\n public build(): Registrar<_ReturnTypes> {\n const mergedNodePrehandler = (node: DependencyNode) => {\n for (const { fromNodeClass, getToNode: toNode } of this.classAliases) {\n if (node instanceof fromNodeClass) {\n return toNode(node);\n }\n }\n return node;\n };\n\n const mergedNodeHandler = async (node: DependencyNode, ancestry: DependencyNode[]) => {\n if (node.name === '_virtualEntrypoint') {\n return {\n key: node.name,\n value: undefined,\n };\n }\n\n const namedHandler = this.namedHandlers[node.name];\n if (namedHandler) {\n return {\n key: node.name,\n value: await namedHandler(node, ancestry),\n };\n }\n\n for (const { nodeClass, identifier, fn } of this.classHandlers) {\n if (node instanceof nodeClass) {\n return {\n key: identifier,\n value: await fn(node, ancestry),\n };\n }\n }\n\n return {\n key: 'default',\n value: await this.defaultHandler(node, ancestry),\n };\n };\n return {\n traverseDependencies: async (node: DependencyNode) => {\n const entrypoints = this.getEntrypoints(node);\n if (entrypoints.length === 1)\n return await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)();\n\n const dependencies = entrypoints.reduce((acc, cur) => {\n acc[cur.name] = cur;\n return acc;\n }, {} as Dependencies);\n\n class VirtualNode extends DependencyNode {}\n\n const virtualEntrypoint = new VirtualNode({\n name: '_virtualEntrypoint',\n dependencies,\n });\n\n return await dfs<_ReturnTypes>(\n virtualEntrypoint,\n mergedNodeHandler,\n mergedNodePrehandler,\n )();\n },\n };\n }\n}\n\nexport const multiplexOrderedRegistrars = <\n const T extends [name: string, registrar: Registrar<{}>][],\n>(\n ...registrars: T\n) => {\n const multiplexRegistrar: Registrar<{\n [K in Extract<keyof T, number> as T[K] extends readonly [infer Name extends string, any]\n ? Name\n : never]: T[K] extends readonly [any, Registrar<infer Ret>] ? Ret : never;\n }> = {\n traverseDependencies: async (\n ...params: Parameters<Registrar<any>['traverseDependencies']>\n ) => {\n const acc: Record<string, any> = {};\n for (const [name, registrar] of registrars) {\n const result = await registrar.traverseDependencies(...params);\n acc[name] = result;\n }\n\n return acc;\n },\n } as any;\n\n return multiplexRegistrar;\n};\n"]}
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var JKTGUQY4_cjs = require('./JKTGUQY4.cjs');
4
- var O7HENH25_cjs = require('./O7HENH25.cjs');
4
+ var _3AKKEY5J_cjs = require('./3AKKEY5J.cjs');
5
5
  var OIBEOODK_cjs = require('./OIBEOODK.cjs');
6
6
  var VTYVOFLM_cjs = require('./VTYVOFLM.cjs');
7
7
  require('./YJF4D23A.cjs');
@@ -14,11 +14,11 @@ Object.defineProperty(exports, "DependencyStore", {
14
14
  });
15
15
  Object.defineProperty(exports, "RegistrarBuilder", {
16
16
  enumerable: true,
17
- get: function () { return O7HENH25_cjs.RegistrarBuilder; }
17
+ get: function () { return _3AKKEY5J_cjs.RegistrarBuilder; }
18
18
  });
19
19
  Object.defineProperty(exports, "multiplexOrderedRegistrars", {
20
20
  enumerable: true,
21
- get: function () { return O7HENH25_cjs.multiplexOrderedRegistrars; }
21
+ get: function () { return _3AKKEY5J_cjs.multiplexOrderedRegistrars; }
22
22
  });
23
23
  Object.defineProperty(exports, "extractSchemaFromFactory", {
24
24
  enumerable: true,
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { DependencyStore } from './JJBD65HU.js';
2
- export { RegistrarBuilder, multiplexOrderedRegistrars } from './4DUQS2FO.js';
2
+ export { RegistrarBuilder, multiplexOrderedRegistrars } from './H6QNIXMU.js';
3
3
  export { extractSchemaFromFactory } from './EAPC6542.js';
4
4
  export { FactoryDefinition, ObjectDefinition, deserializeDimensions, serializeDimensions } from './NQBJGAQC.js';
5
5
  import './VUOMXK5T.js';
@@ -1,17 +1,17 @@
1
1
  'use strict';
2
2
 
3
- var O7HENH25_cjs = require('./O7HENH25.cjs');
3
+ var _3AKKEY5J_cjs = require('./3AKKEY5J.cjs');
4
4
  require('./YJF4D23A.cjs');
5
5
 
6
6
 
7
7
 
8
8
  Object.defineProperty(exports, "RegistrarBuilder", {
9
9
  enumerable: true,
10
- get: function () { return O7HENH25_cjs.RegistrarBuilder; }
10
+ get: function () { return _3AKKEY5J_cjs.RegistrarBuilder; }
11
11
  });
12
12
  Object.defineProperty(exports, "multiplexOrderedRegistrars", {
13
13
  enumerable: true,
14
- get: function () { return O7HENH25_cjs.multiplexOrderedRegistrars; }
14
+ get: function () { return _3AKKEY5J_cjs.multiplexOrderedRegistrars; }
15
15
  });
16
16
  //# sourceMappingURL=registrarBuilder.cjs.map
17
17
  //# sourceMappingURL=registrarBuilder.cjs.map
@@ -1,4 +1,4 @@
1
- import type { DependencyNode } from '@layerzerolabs/dependency-graph';
1
+ import { DependencyNode } from '@layerzerolabs/dependency-graph';
2
2
  import type { Registrar } from '@layerzerolabs/dfs';
3
3
  export type { Registrar };
4
4
  type NodeClass = new (...args: any[]) => DependencyNode;
@@ -18,7 +18,7 @@ type AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;
18
18
  */
19
19
  export declare class RegistrarBuilder<_ReturnTypes> {
20
20
  protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;
21
- constructor();
21
+ protected namedHandlers: Record<string, (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>>;
22
22
  protected classHandlers: {
23
23
  nodeClass: AbstractNodeClass;
24
24
  identifier: string;
@@ -28,6 +28,13 @@ export declare class RegistrarBuilder<_ReturnTypes> {
28
28
  fromNodeClass: AbstractNodeClass;
29
29
  getToNode: (from: DependencyNode) => DependencyNode;
30
30
  }[];
31
+ protected getEntrypoints: (node: DependencyNode) => DependencyNode[];
32
+ constructor();
33
+ addNamedHandler<Node extends DependencyNode, Function extends (node: Node, ancestry: DependencyNode[]) => Promise<any>>(node: DependencyNode, fn: Function): RegistrarBuilder<_ReturnTypes & {
34
+ ["namedHandlers"]: {
35
+ [nodeName: string]: Awaited<ReturnType<Function>>;
36
+ };
37
+ }>;
31
38
  /**
32
39
  * Add a function fn to be called on all nodes of class
33
40
  * @param nodeClass
@@ -51,6 +58,11 @@ export declare class RegistrarBuilder<_ReturnTypes> {
51
58
  * @param getToNode method that returns the *to* part of the alias
52
59
  */
53
60
  addClassAlias<FromNodeClass extends AbstractNodeClass>(fromNodeClass: FromNodeClass, getToNode: (fromNodeInstance: InstanceType<FromNodeClass>) => DependencyNode): this;
61
+ /**
62
+ * Set a custom entrypoint getter function. By default, the entrypoint getter
63
+ * returns the node passed in as the sole entrypoint.
64
+ */
65
+ setEntrypointsGetter(fn: (node: DependencyNode) => DependencyNode[]): this;
54
66
  /**
55
67
  * Add a default function fn to be used on nodes whose types do not have handlers.
56
68
  * If this is not set, the default behaviour will be to throw
@@ -1 +1 @@
1
- {"version":3,"file":"registrarBuilder.d.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cAAc;IAuChF;;;OAGG;IACI,oBAAoB,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,cAAc,EAAE;IAK1E;;;;OAIG;IACI,UAAU,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,cAAc,EAAE,QAAQ,EAAE,cAAc,EAAE,KAAK,GAAG;IAKxE,KAAK,IAAI,SAAS,CAAC,YAAY,CAAC;CAkE1C;AAED,eAAO,MAAM,0BAA0B,GACnC,KAAK,CAAC,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,EAE1D,GAAG,YAAY,CAAC,kBAGX,CAAC,6DAA6D,IAAI,mFAET,GAAG,oBAgBpE,CAAC"}
@@ -1,4 +1,4 @@
1
- export { RegistrarBuilder, multiplexOrderedRegistrars } from './4DUQS2FO.js';
1
+ export { RegistrarBuilder, multiplexOrderedRegistrars } from './H6QNIXMU.js';
2
2
  import './VUOMXK5T.js';
3
3
  //# sourceMappingURL=registrarBuilder.js.map
4
4
  //# sourceMappingURL=registrarBuilder.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@layerzerolabs/base-definitions",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -15,16 +15,16 @@
15
15
  "types": "./dist/index.d.ts",
16
16
  "dependencies": {
17
17
  "zod": "3.25.56",
18
- "@layerzerolabs/dependency-graph": "0.2.6",
19
- "@layerzerolabs/dfs": "0.2.6",
20
- "@layerzerolabs/common-utils": "0.2.6",
21
- "@layerzerolabs/typescript-utils": "0.2.6"
18
+ "@layerzerolabs/common-utils": "0.2.7",
19
+ "@layerzerolabs/dependency-graph": "0.2.7",
20
+ "@layerzerolabs/dfs": "0.2.7",
21
+ "@layerzerolabs/typescript-utils": "0.2.7"
22
22
  },
23
23
  "devDependencies": {
24
24
  "tsup": "^8.4.0",
25
25
  "vitest": "^3.2.3",
26
- "@layerzerolabs/tsup-configuration": "0.2.6",
27
- "@layerzerolabs/typescript-configuration": "0.2.6"
26
+ "@layerzerolabs/tsup-configuration": "0.2.7",
27
+ "@layerzerolabs/typescript-configuration": "0.2.7"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "restricted",
@@ -1,4 +1,5 @@
1
- import type { DependencyNode } from '@layerzerolabs/dependency-graph';
1
+ import type { Dependencies } from '@layerzerolabs/dependency-graph';
2
+ import { DependencyNode } from '@layerzerolabs/dependency-graph';
2
3
  import type { Registrar } from '@layerzerolabs/dfs';
3
4
  import { dfs } from '@layerzerolabs/dfs';
4
5
  export type { Registrar };
@@ -21,13 +22,11 @@ type AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;
21
22
  */
22
23
  export class RegistrarBuilder<_ReturnTypes> {
23
24
  protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;
24
- constructor() {
25
- this.defaultHandler = async (node: DependencyNode) => {
26
- throw new Error(
27
- `This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,
28
- );
29
- };
30
- }
25
+
26
+ protected namedHandlers: Record<
27
+ string,
28
+ (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>
29
+ > = {};
31
30
 
32
31
  protected classHandlers: {
33
32
  nodeClass: AbstractNodeClass;
@@ -40,6 +39,36 @@ export class RegistrarBuilder<_ReturnTypes> {
40
39
  getToNode: (from: DependencyNode) => DependencyNode;
41
40
  }[] = [];
42
41
 
42
+ protected getEntrypoints: (node: DependencyNode) => DependencyNode[] = (node) => [node];
43
+
44
+ constructor() {
45
+ this.defaultHandler = async (node: DependencyNode) => {
46
+ throw new Error(
47
+ `This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,
48
+ );
49
+ };
50
+ }
51
+
52
+ public addNamedHandler<
53
+ Node extends DependencyNode,
54
+ Function extends (node: Node, ancestry: DependencyNode[]) => Promise<any>,
55
+ >(node: DependencyNode, fn: Function) {
56
+ if (this.namedHandlers[node.name]) {
57
+ throw new Error(
58
+ `Cannot define a named handler for an identifier that already has a handler: ${node.name}`,
59
+ );
60
+ }
61
+ this.namedHandlers[node.name] = fn as unknown as (
62
+ node: DependencyNode,
63
+ ancestry: DependencyNode[],
64
+ ) => Promise<any>;
65
+ return this as RegistrarBuilder<
66
+ _ReturnTypes & {
67
+ ['namedHandlers']: { [nodeName: string]: Awaited<ReturnType<Function>> };
68
+ }
69
+ >;
70
+ }
71
+
43
72
  /**
44
73
  * Add a function fn to be called on all nodes of class
45
74
  * @param nodeClass
@@ -159,6 +188,15 @@ export class RegistrarBuilder<_ReturnTypes> {
159
188
  return this;
160
189
  }
161
190
 
191
+ /**
192
+ * Set a custom entrypoint getter function. By default, the entrypoint getter
193
+ * returns the node passed in as the sole entrypoint.
194
+ */
195
+ public setEntrypointsGetter(fn: (node: DependencyNode) => DependencyNode[]) {
196
+ this.getEntrypoints = fn;
197
+ return this;
198
+ }
199
+
162
200
  /**
163
201
  * Add a default function fn to be used on nodes whose types do not have handlers.
164
202
  * If this is not set, the default behaviour will be to throw
@@ -180,6 +218,21 @@ export class RegistrarBuilder<_ReturnTypes> {
180
218
  };
181
219
 
182
220
  const mergedNodeHandler = async (node: DependencyNode, ancestry: DependencyNode[]) => {
221
+ if (node.name === '_virtualEntrypoint') {
222
+ return {
223
+ key: node.name,
224
+ value: undefined,
225
+ };
226
+ }
227
+
228
+ const namedHandler = this.namedHandlers[node.name];
229
+ if (namedHandler) {
230
+ return {
231
+ key: node.name,
232
+ value: await namedHandler(node, ancestry),
233
+ };
234
+ }
235
+
183
236
  for (const { nodeClass, identifier, fn } of this.classHandlers) {
184
237
  if (node instanceof nodeClass) {
185
238
  return {
@@ -195,8 +248,29 @@ export class RegistrarBuilder<_ReturnTypes> {
195
248
  };
196
249
  };
197
250
  return {
198
- traverseDependencies: async (node: DependencyNode) =>
199
- await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)(),
251
+ traverseDependencies: async (node: DependencyNode) => {
252
+ const entrypoints = this.getEntrypoints(node);
253
+ if (entrypoints.length === 1)
254
+ return await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)();
255
+
256
+ const dependencies = entrypoints.reduce((acc, cur) => {
257
+ acc[cur.name] = cur;
258
+ return acc;
259
+ }, {} as Dependencies);
260
+
261
+ class VirtualNode extends DependencyNode {}
262
+
263
+ const virtualEntrypoint = new VirtualNode({
264
+ name: '_virtualEntrypoint',
265
+ dependencies,
266
+ });
267
+
268
+ return await dfs<_ReturnTypes>(
269
+ virtualEntrypoint,
270
+ mergedNodeHandler,
271
+ mergedNodePrehandler,
272
+ )();
273
+ },
200
274
  };
201
275
  }
202
276
  }
@@ -120,3 +120,56 @@ describe('Registrar builder', () => {
120
120
  });
121
121
  });
122
122
  });
123
+
124
+ describe('Named handlers', () => {
125
+ test('Named handlers should take priority over class handlers', async () => {
126
+ let classHandlerCalled = false;
127
+ let namedHandlerCalled = false;
128
+
129
+ const builder = new RegistrarBuilder()
130
+ .addClassHandler(SimpleClassA, 'simpleA', async () => {
131
+ classHandlerCalled = true;
132
+ return 'from-class';
133
+ })
134
+ .addNamedHandler(mySimpleClassA1, async () => {
135
+ namedHandlerCalled = true;
136
+ return 'from-named';
137
+ })
138
+ .build();
139
+
140
+ await builder.traverseDependencies(mySimpleClassA1);
141
+
142
+ expect(namedHandlerCalled).toBe(true);
143
+ expect(classHandlerCalled).toBe(false);
144
+ });
145
+
146
+ test('Named handlers should be invoked with correct arguments and return values', async () => {
147
+ let receivedNode: DependencyNode | undefined;
148
+ let receivedAncestry: DependencyNode[] | undefined;
149
+
150
+ const builder = new RegistrarBuilder()
151
+ .addNamedHandler(mySimpleClassA1, async (node, ancestry) => {
152
+ receivedNode = node;
153
+ receivedAncestry = ancestry;
154
+ return 'named-result';
155
+ })
156
+ .build();
157
+
158
+ const result = await builder.traverseDependencies(mySimpleClassA1);
159
+
160
+ expect(receivedNode).toBe(mySimpleClassA1);
161
+ expect(receivedAncestry).toStrictEqual([]);
162
+ // Result is grouped by handler identifier (node name for named handlers)
163
+ expect(result).toStrictEqual({ MySimpleClassA: { MySimpleClassA: 'named-result' } });
164
+ });
165
+
166
+ test('Registering duplicate named handlers should throw', () => {
167
+ expect(() => {
168
+ new RegistrarBuilder()
169
+ .addNamedHandler(mySimpleClassA1, async () => 'first')
170
+ .addNamedHandler(mySimpleClassA1, async () => 'second');
171
+ }).toThrow(
172
+ 'Cannot define a named handler for an identifier that already has a handler: MySimpleClassA',
173
+ );
174
+ });
175
+ });
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/registrarBuilder.ts"],"names":["RegistrarBuilder","defaultHandler","node","Error","JSON","stringify","classHandlers","classAliases","addClassHandler","nodeClass","identifier","fn","find","fromNodeClass","i","handler","prototype","isPrototypeOf","splice","push","addClassAlias","getToNode","alias","addDefault","build","mergedNodePrehandler","toNode","mergedNodeHandler","ancestry","key","value","traverseDependencies","dfs","multiplexOrderedRegistrars","registrars","multiplexRegistrar","params","acc","name","registrar","result"],"mappings":";;;AAqBO,IAAMA,mBAAN,MAAMA;EAnBb;;;AAoBcC,EAAAA,cAAAA;EACV,WAAA,GAAc;AACV,IAAA,IAAA,CAAKA,cAAAA,GAAiB,OAAOC,IAAAA,KAAAA;AACzB,MAAA,MAAM,IAAIC,KAAAA,CACN,CAAA,0EAAA,EAA6EC,KAAKC,SAAAA,CAAUH,IAAAA,CAAAA,CAAAA,CAAO,CAAA;AAE3G,IAAA,CAAA;AACJ,EAAA;AAEUI,EAAAA,aAAAA,GAIJ,EAAA;AAEIC,EAAAA,YAAAA,GAGJ,EAAA;;;;;;;EAQCC,eAAAA,CAILC,SAAAA,EAAuBC,YAAwBC,EAAAA,EAAc;AAC3D,IAAA,IAAI,IAAA,CAAKJ,aAAaK,IAAAA,CAAK,CAAC,EAAEC,aAAAA,EAAa,KAAOA,aAAAA,KAAkBJ,SAAAA,CAAAA,EAAY;AAC5E,MAAA,MAAM,IAA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AAE,IAAM,KAAKL,aAAAA,EAAe;AAC5D,QAAA,IAAIJ,gBAAgBO,SAAAA,EAAW;AAC3B,UAAA,OAAO;YACHoB,GAAAA,EAAKnB,UAAAA;YACLoB,KAAAA,EAAO,MAAMnB,EAAAA,CAAGT,IAAAA,EAAM0B,QAAAA;AAC1B,WAAA;AACJ,QAAA;AACJ,MAAA;AAEA,MAAA,OAAO;QACHC,GAAAA,EAAK,SAAA;AACLC,QAAAA,KAAAA,EAAO,MAAM,IAAA,CAAK7B,cAAAA,CAAeC,IAAAA,EAAM0B,QAAAA;AAC3C,OAAA;IACJ,CAAA,EAd0B,mBAAA,CAAA;AAe1B,IAAA,OAAO;MACHG,oBAAAA,kBAAsB,MAAA,CAAA,OAAO7B,SACzB,MAAM8B,GAAAA,CAAkB9B,MAAMyB,iBAAAA,EAAmBF,oBAAAA,GAAAA,EAD/B,sBAAA;AAE1B,KAAA;AACJ,EAAA;AACJ;AAEO,IAAMQ,0BAAAA,8BAGNC,UAAAA,KAAAA;AAEH,EAAA,MAAMC,kBAAAA,GAID;AACDJ,IAAAA,oBAAAA,mCACOK,MAAAA,KAAAA;AAEH,MAAA,MAAMC,MAA2B,EAAC;AAClC,MAAA,KAAA,MAAW,CAACC,IAAAA,EAAMC,SAAAA,CAAAA,IAAcL,UAAAA,EAAY;AACxC,QAAA,MAAMM,MAAAA,GAAS,MAAMD,SAAAA,CAAUR,oBAAAA,CAAoB,GAAIK,MAAAA,CAAAA;AACvDC,QAAAA,GAAAA,CAAIC,IAAAA,CAAAA,GAAQE,MAAAA;AAChB,MAAA;AAEA,MAAA,OAAOH,GAAAA;IACX,CAAA,EAVsB,sBAAA;AAW1B,GAAA;AAEA,EAAA,OAAOF,kBAAAA;AACX,CAAA,EAxB0C,4BAAA","file":"4DUQS2FO.js","sourcesContent":["import type { DependencyNode } from '@layerzerolabs/dependency-graph';\nimport type { Registrar } from '@layerzerolabs/dfs';\nimport { dfs } from '@layerzerolabs/dfs';\nexport type { Registrar };\n\ntype NodeClass = new (...args: any[]) => DependencyNode;\ntype AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;\n\n/**\n * <!-- anchor:RegistrarBuilder -->\n * A builder class for a {@link Registrar}.\n * Builds a registrar that switches on node class--i.e., enables defining\n * specific registration behaviour for specific node classes.\n * Intelligently handles inheritance, always using the most specific\n * handler defined for any given class.\n * The registrar built shall return an object that maps from a handler ID,\n * to a map from node name to the return of each handler,\n * e.g., { objectNodes: { myObjectNode: 'my handler return' }}.\n * This can be used to extract a specific resolved value after resolving the tree,\n * given that the handlers return the resolved nodes.\n */\nexport class RegistrarBuilder<_ReturnTypes> {\n protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n constructor() {\n this.defaultHandler = async (node: DependencyNode) => {\n throw new Error(\n `This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,\n );\n };\n }\n\n protected classHandlers: {\n nodeClass: AbstractNodeClass;\n identifier: string;\n fn: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n }[] = [];\n\n protected classAliases: {\n fromNodeClass: AbstractNodeClass;\n getToNode: (from: DependencyNode) => DependencyNode;\n }[] = [];\n\n /**\n * Add a function fn to be called on all nodes of class\n * @param nodeClass\n * @param identifier\n * @param fn\n */\n public addClassHandler<\n Identifier extends string,\n _NodeClass extends NodeClass,\n Function extends (node: InstanceType<_NodeClass>, ancestry: DependencyNode[]) => any,\n >(nodeClass: _NodeClass, identifier: Identifier, fn: Function) {\n if (this.classAliases.find(({ fromNodeClass }) => fromNodeClass === nodeClass)) {\n throw new Error(\n `Cannot define a handler for a class that is aliased to something else. This would be pointless`,\n );\n }\n\n let i = 0;\n for (const handler of this.classHandlers) {\n //check if we are overriding an existing handler\n if (handler.nodeClass === nodeClass) {\n this.classHandlers[i] = {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n };\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n //check if the new handler has an ordering requirement against an existing handler\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (handler.nodeClass.prototype.isPrototypeOf(nodeClass.prototype)) {\n this.classHandlers.splice(i, 0, {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n i++;\n }\n\n this.classHandlers.push({\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n\n /**\n * Alias some class *from*, such that any node of that class in the tree will be\n * treated as though it were some other node *to*. The *to* node is given as a\n * function returning a class instance, so that its dependencies, name, etc can be fixed.\n * The registrar will ignore the dependencies of the original node, and follow the alias's\n * dependencies instead.\n *\n * Additionally, this method does not affect the typing of the registrar. The output type\n * of the build() method will not reflect the alias.\n *\n * @param fromNodeClass alias instances of this class\n * @param getToNode method that returns the *to* part of the alias\n */\n public addClassAlias<FromNodeClass extends AbstractNodeClass>(\n fromNodeClass: FromNodeClass,\n getToNode: (fromNodeInstance: InstanceType<FromNodeClass>) => DependencyNode,\n ) {\n if (this.classHandlers.find(({ nodeClass }) => nodeClass === fromNodeClass)) {\n throw new Error(`Cannot define an alias for a class that already has a handler`);\n }\n\n let i = 0;\n for (const alias of this.classAliases) {\n //check if we are overriding an existing alias\n if (alias.fromNodeClass === fromNodeClass) {\n this.classAliases[i] = {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n };\n\n return this;\n }\n //check if the new alias has an ordering requirement against an existing alias\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (alias.fromNodeClass.prototype.isPrototypeOf(fromNodeClass.prototype)) {\n this.classAliases.splice(i, 0, {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n i++;\n }\n\n this.classAliases.push({\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n\n /**\n * Add a default function fn to be used on nodes whose types do not have handlers.\n * If this is not set, the default behaviour will be to throw\n * @param fn\n */\n public addDefault(fn: (node: DependencyNode, ancestry: DependencyNode[]) => any) {\n this.defaultHandler = fn;\n return this;\n }\n\n public build(): Registrar<_ReturnTypes> {\n const mergedNodePrehandler = (node: DependencyNode) => {\n for (const { fromNodeClass, getToNode: toNode } of this.classAliases) {\n if (node instanceof fromNodeClass) {\n return toNode(node);\n }\n }\n return node;\n };\n\n const mergedNodeHandler = async (node: DependencyNode, ancestry: DependencyNode[]) => {\n for (const { nodeClass, identifier, fn } of this.classHandlers) {\n if (node instanceof nodeClass) {\n return {\n key: identifier,\n value: await fn(node, ancestry),\n };\n }\n }\n\n return {\n key: 'default',\n value: await this.defaultHandler(node, ancestry),\n };\n };\n return {\n traverseDependencies: async (node: DependencyNode) =>\n await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)(),\n };\n }\n}\n\nexport const multiplexOrderedRegistrars = <\n const T extends [name: string, registrar: Registrar<{}>][],\n>(\n ...registrars: T\n) => {\n const multiplexRegistrar: Registrar<{\n [K in Extract<keyof T, number> as T[K] extends readonly [infer Name extends string, any]\n ? Name\n : never]: T[K] extends readonly [any, Registrar<infer Ret>] ? Ret : never;\n }> = {\n traverseDependencies: async (\n ...params: Parameters<Registrar<any>['traverseDependencies']>\n ) => {\n const acc: Record<string, any> = {};\n for (const [name, registrar] of registrars) {\n const result = await registrar.traverseDependencies(...params);\n acc[name] = result;\n }\n\n return acc;\n },\n } as any;\n\n return multiplexRegistrar;\n};\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/registrarBuilder.ts"],"names":["RegistrarBuilder","defaultHandler","node","Error","JSON","stringify","classHandlers","classAliases","addClassHandler","nodeClass","identifier","fn","find","fromNodeClass","i","handler","prototype","isPrototypeOf","splice","push","addClassAlias","getToNode","alias","addDefault","build","mergedNodePrehandler","toNode","mergedNodeHandler","__name","ancestry","key","value","traverseDependencies","dfs","multiplexOrderedRegistrars","registrars","multiplexRegistrar","params","acc","name","registrar","result"],"mappings":";;;;;AAqBO,IAAMA,mBAAN,MAAMA;EAnBb;;;AAoBcC,EAAAA,cAAAA;EACV,WAAA,GAAc;AACV,IAAA,IAAA,CAAKA,cAAAA,GAAiB,OAAOC,IAAAA,KAAAA;AACzB,MAAA,MAAM,IAAIC,KAAAA,CACN,CAAA,0EAAA,EAA6EC,KAAKC,SAAAA,CAAUH,IAAAA,CAAAA,CAAAA,CAAO,CAAA;AAE3G,IAAA,CAAA;AACJ,EAAA;AAEUI,EAAAA,aAAAA,GAIJ,EAAA;AAEIC,EAAAA,YAAAA,GAGJ,EAAA;;;;;;;EAQCC,eAAAA,CAILC,SAAAA,EAAuBC,YAAwBC,EAAAA,EAAc;AAC3D,IAAA,IAAI,IAAA,CAAKJ,aAAaK,IAAAA,CAAK,CAAC,EAAEC,aAAAA,EAAa,KAAOA,aAAAA,KAAkBJ,SAAAA,CAAAA,EAAY;AAC5E,MAAA,MAAM,IAAIN,MACN,CAAA,8FAAA,CAAgG,CAAA;AAExG,IAAA;AAEA,IAAA,IAAIW,CAAAA,GAAI,CAAA;AACR,IAAA,KAAA,MAAWC,OAAAA,IAAW,KAAKT,aAAAA,EAAe;AAEtC,MAAA,IAAIS,OAAAA,CAAQN,cAAcA,SAAAA,EAAW;AACjC,QAAA,IAAA,CAAKH,aAAAA,CAAcQ,CAAAA,CAAAA,GAAK;AACpBL,UAAAA,SAAAA;AACAC,UAAAA,UAAAA;AACAC,UAAAA;AACJ,SAAA;AAEA,QAAA,OAAO,IAAA;AAKX,MAAA;AAIA,MAAA,IAAII,QAAQN,SAAAA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AA,CAAA;AAe1B,IAAA,OAAO;MACHG,oBAAAA,kBAAsBJ,mBAAA,CAAA,OAAO1B,SACzB,MAAM+B,OAAAA,CAAkB/B,MAAMyB,iBAAAA,EAAmBF,oBAAAA,GAAAA,EAD/B,sBAAA;AAE1B,KAAA;AACJ,EAAA;AACJ;AAEO,IAAMS,0BAAAA,2CAGNC,UAAAA,KAAAA;AAEH,EAAA,MAAMC,kBAAAA,GAID;AACDJ,IAAAA,oBAAAA,gDACOK,MAAAA,KAAAA;AAEH,MAAA,MAAMC,MAA2B,EAAC;AAClC,MAAA,KAAA,MAAW,CAACC,IAAAA,EAAMC,SAAAA,CAAAA,IAAcL,UAAAA,EAAY;AACxC,QAAA,MAAMM,MAAAA,GAAS,MAAMD,SAAAA,CAAUR,oBAAAA,CAAoB,GAAIK,MAAAA,CAAAA;AACvDC,QAAAA,GAAAA,CAAIC,IAAAA,CAAAA,GAAQE,MAAAA;AAChB,MAAA;AAEA,MAAA,OAAOH,GAAAA;IACX,CAAA,EAVsB,sBAAA;AAW1B,GAAA;AAEA,EAAA,OAAOF,kBAAAA;AACX,CAAA,EAxB0C,4BAAA","file":"O7HENH25.cjs","sourcesContent":["import type { DependencyNode } from '@layerzerolabs/dependency-graph';\nimport type { Registrar } from '@layerzerolabs/dfs';\nimport { dfs } from '@layerzerolabs/dfs';\nexport type { Registrar };\n\ntype NodeClass = new (...args: any[]) => DependencyNode;\ntype AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;\n\n/**\n * <!-- anchor:RegistrarBuilder -->\n * A builder class for a {@link Registrar}.\n * Builds a registrar that switches on node class--i.e., enables defining\n * specific registration behaviour for specific node classes.\n * Intelligently handles inheritance, always using the most specific\n * handler defined for any given class.\n * The registrar built shall return an object that maps from a handler ID,\n * to a map from node name to the return of each handler,\n * e.g., { objectNodes: { myObjectNode: 'my handler return' }}.\n * This can be used to extract a specific resolved value after resolving the tree,\n * given that the handlers return the resolved nodes.\n */\nexport class RegistrarBuilder<_ReturnTypes> {\n protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n constructor() {\n this.defaultHandler = async (node: DependencyNode) => {\n throw new Error(\n `This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,\n );\n };\n }\n\n protected classHandlers: {\n nodeClass: AbstractNodeClass;\n identifier: string;\n fn: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;\n }[] = [];\n\n protected classAliases: {\n fromNodeClass: AbstractNodeClass;\n getToNode: (from: DependencyNode) => DependencyNode;\n }[] = [];\n\n /**\n * Add a function fn to be called on all nodes of class\n * @param nodeClass\n * @param identifier\n * @param fn\n */\n public addClassHandler<\n Identifier extends string,\n _NodeClass extends NodeClass,\n Function extends (node: InstanceType<_NodeClass>, ancestry: DependencyNode[]) => any,\n >(nodeClass: _NodeClass, identifier: Identifier, fn: Function) {\n if (this.classAliases.find(({ fromNodeClass }) => fromNodeClass === nodeClass)) {\n throw new Error(\n `Cannot define a handler for a class that is aliased to something else. This would be pointless`,\n );\n }\n\n let i = 0;\n for (const handler of this.classHandlers) {\n //check if we are overriding an existing handler\n if (handler.nodeClass === nodeClass) {\n this.classHandlers[i] = {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n };\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n //check if the new handler has an ordering requirement against an existing handler\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (handler.nodeClass.prototype.isPrototypeOf(nodeClass.prototype)) {\n this.classHandlers.splice(i, 0, {\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n i++;\n }\n\n this.classHandlers.push({\n nodeClass,\n identifier,\n fn: fn as unknown as (node: DependencyNode) => Promise<any>,\n });\n\n return this as RegistrarBuilder<\n _ReturnTypes & {\n [_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };\n }\n >;\n }\n\n /**\n * Alias some class *from*, such that any node of that class in the tree will be\n * treated as though it were some other node *to*. The *to* node is given as a\n * function returning a class instance, so that its dependencies, name, etc can be fixed.\n * The registrar will ignore the dependencies of the original node, and follow the alias's\n * dependencies instead.\n *\n * Additionally, this method does not affect the typing of the registrar. The output type\n * of the build() method will not reflect the alias.\n *\n * @param fromNodeClass alias instances of this class\n * @param getToNode method that returns the *to* part of the alias\n */\n public addClassAlias<FromNodeClass extends AbstractNodeClass>(\n fromNodeClass: FromNodeClass,\n getToNode: (fromNodeInstance: InstanceType<FromNodeClass>) => DependencyNode,\n ) {\n if (this.classHandlers.find(({ nodeClass }) => nodeClass === fromNodeClass)) {\n throw new Error(`Cannot define an alias for a class that already has a handler`);\n }\n\n let i = 0;\n for (const alias of this.classAliases) {\n //check if we are overriding an existing alias\n if (alias.fromNodeClass === fromNodeClass) {\n this.classAliases[i] = {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n };\n\n return this;\n }\n //check if the new alias has an ordering requirement against an existing alias\n //(we always want to use more specific handlers if they exist)\n // eslint-disable-next-line no-prototype-builtins\n if (alias.fromNodeClass.prototype.isPrototypeOf(fromNodeClass.prototype)) {\n this.classAliases.splice(i, 0, {\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n i++;\n }\n\n this.classAliases.push({\n fromNodeClass,\n getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,\n });\n\n return this;\n }\n\n /**\n * Add a default function fn to be used on nodes whose types do not have handlers.\n * If this is not set, the default behaviour will be to throw\n * @param fn\n */\n public addDefault(fn: (node: DependencyNode, ancestry: DependencyNode[]) => any) {\n this.defaultHandler = fn;\n return this;\n }\n\n public build(): Registrar<_ReturnTypes> {\n const mergedNodePrehandler = (node: DependencyNode) => {\n for (const { fromNodeClass, getToNode: toNode } of this.classAliases) {\n if (node instanceof fromNodeClass) {\n return toNode(node);\n }\n }\n return node;\n };\n\n const mergedNodeHandler = async (node: DependencyNode, ancestry: DependencyNode[]) => {\n for (const { nodeClass, identifier, fn } of this.classHandlers) {\n if (node instanceof nodeClass) {\n return {\n key: identifier,\n value: await fn(node, ancestry),\n };\n }\n }\n\n return {\n key: 'default',\n value: await this.defaultHandler(node, ancestry),\n };\n };\n return {\n traverseDependencies: async (node: DependencyNode) =>\n await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)(),\n };\n }\n}\n\nexport const multiplexOrderedRegistrars = <\n const T extends [name: string, registrar: Registrar<{}>][],\n>(\n ...registrars: T\n) => {\n const multiplexRegistrar: Registrar<{\n [K in Extract<keyof T, number> as T[K] extends readonly [infer Name extends string, any]\n ? Name\n : never]: T[K] extends readonly [any, Registrar<infer Ret>] ? Ret : never;\n }> = {\n traverseDependencies: async (\n ...params: Parameters<Registrar<any>['traverseDependencies']>\n ) => {\n const acc: Record<string, any> = {};\n for (const [name, registrar] of registrars) {\n const result = await registrar.traverseDependencies(...params);\n acc[name] = result;\n }\n\n return acc;\n },\n } as any;\n\n return multiplexRegistrar;\n};\n"]}