@proto-kit/common 0.1.1-develop.165 → 0.1.1-develop.186
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/dist/config/ModuleContainer.d.ts +6 -0
- package/dist/config/ModuleContainer.d.ts.map +1 -1
- package/dist/config/ModuleContainer.js +9 -0
- package/dist/dependencyFactory/DependencyFactory.d.ts +24 -0
- package/dist/dependencyFactory/DependencyFactory.d.ts.map +1 -0
- package/dist/dependencyFactory/DependencyFactory.js +96 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +7 -0
- package/package.json +3 -3
- package/src/config/ModuleContainer.ts +13 -0
- package/src/dependencyFactory/DependencyFactory.ts +132 -0
- package/src/index.ts +1 -0
- package/src/utils.ts +8 -0
- package/tsconfig.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { InjectionToken } from "tsyringe";
|
|
3
3
|
import { StringKeyOf, TypedClass } from "../types";
|
|
4
|
+
import { DependencyFactory } from "../dependencyFactory/DependencyFactory";
|
|
4
5
|
import { Configurable, ConfigurableModule } from "./ConfigurableModule";
|
|
5
6
|
export declare const ModuleContainerErrors: {
|
|
6
7
|
configNotSetInContainer: (moduleName: string) => Error;
|
|
@@ -63,6 +64,11 @@ export declare class ModuleContainer<Modules extends ModulesRecord> extends Conf
|
|
|
63
64
|
* @param modules
|
|
64
65
|
*/
|
|
65
66
|
protected registerModules(modules: Modules): void;
|
|
67
|
+
/**
|
|
68
|
+
* Inject a set of dependencies using the given list of DependencyFactories
|
|
69
|
+
* This method should be called during startup
|
|
70
|
+
*/
|
|
71
|
+
protected registerDependencyFactories(factories: TypedClass<DependencyFactory>[]): void;
|
|
66
72
|
/**
|
|
67
73
|
* Register a non-module value into the current container
|
|
68
74
|
* @param modules
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ModuleContainer.d.ts","sourceRoot":"","sources":["../../src/config/ModuleContainer.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAwB,cAAc,EAAa,MAAM,UAAU,CAAC;AAG3E,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"ModuleContainer.d.ts","sourceRoot":"","sources":["../../src/config/ModuleContainer.ts"],"names":[],"mappings":"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"}
|
|
@@ -87,6 +87,15 @@ export class ModuleContainer extends ConfigurableModule {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Inject a set of dependencies using the given list of DependencyFactories
|
|
92
|
+
* This method should be called during startup
|
|
93
|
+
*/
|
|
94
|
+
registerDependencyFactories(factories) {
|
|
95
|
+
factories.forEach((factory) => {
|
|
96
|
+
this.container.resolve(factory).initDependencies(this.container);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
90
99
|
/**
|
|
91
100
|
* Register a non-module value into the current container
|
|
92
101
|
* @param modules
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DependencyContainer } from "tsyringe";
|
|
2
|
+
import { TypedClass } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* This is an abstract class for creating DependencyFactories, a pattern
|
|
5
|
+
* to bundle multiple smaller services into one and register them into the
|
|
6
|
+
* injection context.
|
|
7
|
+
*
|
|
8
|
+
* This can for example be a StorageDependencyFactory that creates dependencies
|
|
9
|
+
* like StateService, MerkleWitnessService, etc. So in general, services that
|
|
10
|
+
* are not ConfigurableModules, but still are their own logical unit.
|
|
11
|
+
*
|
|
12
|
+
* DependencyFactories are designed to only be used statically for sets of
|
|
13
|
+
* deps that are necessary for the sequencer to work.
|
|
14
|
+
*
|
|
15
|
+
* Every Factory need the @dependencyFactory annotation (which basically
|
|
16
|
+
* proxies @injectable()) and every method that returns a dependency has to be
|
|
17
|
+
* of the format `() => Dependency` and be annotated with @dependency.
|
|
18
|
+
*/
|
|
19
|
+
export declare abstract class DependencyFactory {
|
|
20
|
+
initDependencies(container: DependencyContainer): void;
|
|
21
|
+
}
|
|
22
|
+
export declare function dependency(): <Target extends DependencyFactory, Dependency>(target: Target, key: string, descriptor: TypedPropertyDescriptor<() => Dependency>) => TypedPropertyDescriptor<() => Dependency>;
|
|
23
|
+
export declare function dependencyFactory(): <Target extends DependencyFactory>(target: TypedClass<Target>) => void;
|
|
24
|
+
//# sourceMappingURL=DependencyFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DependencyFactory.d.ts","sourceRoot":"","sources":["../../src/dependencyFactory/DependencyFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAyB,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAoBtC;;;;;;;;;;;;;;;GAeG;AACH,8BAAsB,iBAAiB;IAC9B,gBAAgB,CAAC,SAAS,EAAE,mBAAmB;CAqBvD;AAuCD,wBAAgB,UAAU,wEAGjB,MAAM,sGAqBd;AAED,wBAAgB,iBAAiB,2EAMhC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { injectable, Lifecycle } from "tsyringe";
|
|
2
|
+
const errors = {
|
|
3
|
+
descriptorUndefined: () => new Error("Descriptor of that dependency is undefined!"),
|
|
4
|
+
dependencyFactoryCalledDirectly: () => new Error("You cannot access the depdendency method directly, use container.resolve"),
|
|
5
|
+
};
|
|
6
|
+
const globalFactoryDependencies = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* This is an abstract class for creating DependencyFactories, a pattern
|
|
9
|
+
* to bundle multiple smaller services into one and register them into the
|
|
10
|
+
* injection context.
|
|
11
|
+
*
|
|
12
|
+
* This can for example be a StorageDependencyFactory that creates dependencies
|
|
13
|
+
* like StateService, MerkleWitnessService, etc. So in general, services that
|
|
14
|
+
* are not ConfigurableModules, but still are their own logical unit.
|
|
15
|
+
*
|
|
16
|
+
* DependencyFactories are designed to only be used statically for sets of
|
|
17
|
+
* deps that are necessary for the sequencer to work.
|
|
18
|
+
*
|
|
19
|
+
* Every Factory need the @dependencyFactory annotation (which basically
|
|
20
|
+
* proxies @injectable()) and every method that returns a dependency has to be
|
|
21
|
+
* of the format `() => Dependency` and be annotated with @dependency.
|
|
22
|
+
*/
|
|
23
|
+
export class DependencyFactory {
|
|
24
|
+
initDependencies(container) {
|
|
25
|
+
const dependencies = globalFactoryDependencies.get(this.constructor.name) ?? {};
|
|
26
|
+
globalFactoryDependencies.delete(this.constructor.name);
|
|
27
|
+
for (const [key, useFactory] of Object.entries(dependencies)) {
|
|
28
|
+
container.register(`${key}_singleton-prototype`, {
|
|
29
|
+
useFactory: useFactory.bind(this),
|
|
30
|
+
});
|
|
31
|
+
const upperCaseKey = key.charAt(0).toUpperCase() + key.slice(1);
|
|
32
|
+
container.register(upperCaseKey, {
|
|
33
|
+
useToken: `${key}_singleton-prototype`,
|
|
34
|
+
}, { lifecycle: Lifecycle.ContainerScoped });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// type DTypes = { [key: string]: TypedClass<unknown> }
|
|
39
|
+
// type Factories<T extends DTypes> = {
|
|
40
|
+
// [key in keyof T]: T[key] extends TypedClass<infer R> ? () => R : never
|
|
41
|
+
// }
|
|
42
|
+
//
|
|
43
|
+
// export abstract class DF2<Types extends DTypes> {
|
|
44
|
+
// public constructor(private factories: Factories<Types>) {
|
|
45
|
+
//
|
|
46
|
+
// }
|
|
47
|
+
// generateDependencies(): Types {
|
|
48
|
+
// let x = this.factories;
|
|
49
|
+
// return {} as Types;
|
|
50
|
+
// }
|
|
51
|
+
// }
|
|
52
|
+
// export function DF2C<T extends Types>(object: Factories<T>): T {
|
|
53
|
+
// const c = class C extends DF2<T>{
|
|
54
|
+
// generateDependencies(): T {
|
|
55
|
+
// return undefined;
|
|
56
|
+
// }
|
|
57
|
+
// }
|
|
58
|
+
// return new c();
|
|
59
|
+
// }
|
|
60
|
+
// class DF2I extends DF2<{x: typeof ConfigurableModule}> {
|
|
61
|
+
// constructor() {
|
|
62
|
+
// super({
|
|
63
|
+
// x: this.x
|
|
64
|
+
// });
|
|
65
|
+
// }
|
|
66
|
+
//
|
|
67
|
+
// x(): ConfigurableModule<any> {
|
|
68
|
+
// return {} as ConfigurableModule<any>;
|
|
69
|
+
// }
|
|
70
|
+
//
|
|
71
|
+
// }
|
|
72
|
+
export function dependency() {
|
|
73
|
+
return function decorator(target, key, descriptor) {
|
|
74
|
+
if (descriptor.value !== undefined) {
|
|
75
|
+
const className = target.constructor.name;
|
|
76
|
+
if (!globalFactoryDependencies.has(className)) {
|
|
77
|
+
globalFactoryDependencies.set(className, {});
|
|
78
|
+
}
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
80
|
+
globalFactoryDependencies.get(target.constructor.name)[key] =
|
|
81
|
+
descriptor.value;
|
|
82
|
+
descriptor.value = () => {
|
|
83
|
+
throw errors.dependencyFactoryCalledDirectly();
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
throw errors.descriptorUndefined();
|
|
88
|
+
}
|
|
89
|
+
return descriptor;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export function dependencyFactory() {
|
|
93
|
+
return function decorator(target) {
|
|
94
|
+
injectable()(target);
|
|
95
|
+
};
|
|
96
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export * from "./zkProgrammable/ZkProgrammable";
|
|
|
5
5
|
export * from "./zkProgrammable/ProvableMethodExecutionContext";
|
|
6
6
|
export * from "./zkProgrammable/provableMethod";
|
|
7
7
|
export * from "./utils";
|
|
8
|
+
export * from "./dependencyFactory/DependencyFactory";
|
|
8
9
|
export { default as log } from "loglevel";
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,SAAS,CAAC;AACxB,cAAc,iCAAiC,CAAC;AAChD,cAAc,iDAAiD,CAAC;AAChE,cAAc,iCAAiC,CAAC;AAChD,cAAc,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,SAAS,CAAC;AACxB,cAAc,iCAAiC,CAAC;AAChD,cAAc,iDAAiD,CAAC;AAChE,cAAc,iCAAiC,CAAC;AAChD,cAAc,SAAS,CAAC;AACxB,cAAc,uCAAuC,CAAC;AAEtD,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,5 +5,6 @@ export * from "./zkProgrammable/ZkProgrammable";
|
|
|
5
5
|
export * from "./zkProgrammable/ProvableMethodExecutionContext";
|
|
6
6
|
export * from "./zkProgrammable/provableMethod";
|
|
7
7
|
export * from "./utils";
|
|
8
|
+
export * from "./dependencyFactory/DependencyFactory";
|
|
8
9
|
// eslint-disable-next-line import/no-unused-modules
|
|
9
10
|
export { default as log } from "loglevel";
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CACzB,SAAS,EAAE,OAAO,EAClB,eAAe,EAAE,KAAK,GAAG,CAAC,MAAM,KAAK,CAAC,GACrC,IAAI,CAMN"}
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CACzB,SAAS,EAAE,OAAO,EAClB,eAAe,EAAE,KAAK,GAAG,CAAC,MAAM,KAAK,CAAC,GACrC,IAAI,CAMN;AAED,wBAAgB,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,EAAE,CAM3E"}
|
package/dist/utils.js
CHANGED
|
@@ -5,3 +5,10 @@ export function requireTrue(condition, errorOrFunction) {
|
|
|
5
5
|
: errorOrFunction;
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
+
export function range(startOrEnd, end) {
|
|
9
|
+
if (end === undefined) {
|
|
10
|
+
end = startOrEnd;
|
|
11
|
+
startOrEnd = 0;
|
|
12
|
+
}
|
|
13
|
+
return Array.from({ length: end - startOrEnd }, (ignored, index) => index);
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
-
"version": "0.1.1-develop.
|
|
6
|
+
"version": "0.1.1-develop.186+0f36686",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc -p tsconfig.json",
|
|
9
9
|
"dev": "tsc -p tsconfig.json --watch",
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
"typescript-memoize": "^1.1.1"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"snarkyjs": "0.
|
|
26
|
+
"snarkyjs": "0.12.0",
|
|
27
27
|
"tsyringe": "^4.7.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@jest/globals": "^29.5.0",
|
|
31
31
|
"@types/lodash": "^4.14.194"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "0f3668667d29ddeb4fd6aaff1d510c4da9a7743c"
|
|
34
34
|
}
|
|
@@ -4,6 +4,7 @@ import { container, Frequency, InjectionToken, Lifecycle } from "tsyringe";
|
|
|
4
4
|
import log from "loglevel";
|
|
5
5
|
|
|
6
6
|
import { StringKeyOf, TypedClass } from "../types";
|
|
7
|
+
import { DependencyFactory } from "../dependencyFactory/DependencyFactory";
|
|
7
8
|
|
|
8
9
|
import { Configurable, ConfigurableModule } from "./ConfigurableModule";
|
|
9
10
|
|
|
@@ -171,6 +172,18 @@ export class ModuleContainer<
|
|
|
171
172
|
}
|
|
172
173
|
}
|
|
173
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Inject a set of dependencies using the given list of DependencyFactories
|
|
177
|
+
* This method should be called during startup
|
|
178
|
+
*/
|
|
179
|
+
protected registerDependencyFactories(
|
|
180
|
+
factories: TypedClass<DependencyFactory>[]
|
|
181
|
+
) {
|
|
182
|
+
factories.forEach((factory) => {
|
|
183
|
+
this.container.resolve(factory).initDependencies(this.container);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
174
187
|
/**
|
|
175
188
|
* Register a non-module value into the current container
|
|
176
189
|
* @param modules
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { DependencyContainer, injectable, Lifecycle } from "tsyringe";
|
|
2
|
+
|
|
3
|
+
import { TypedClass } from "../types";
|
|
4
|
+
|
|
5
|
+
const errors = {
|
|
6
|
+
descriptorUndefined: () =>
|
|
7
|
+
new Error("Descriptor of that dependency is undefined!"),
|
|
8
|
+
|
|
9
|
+
dependencyFactoryCalledDirectly: () =>
|
|
10
|
+
new Error(
|
|
11
|
+
"You cannot access the depdendency method directly, use container.resolve"
|
|
12
|
+
),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const globalFactoryDependencies = new Map<
|
|
16
|
+
string,
|
|
17
|
+
| {
|
|
18
|
+
[key: string]: () => unknown;
|
|
19
|
+
}
|
|
20
|
+
| undefined
|
|
21
|
+
>();
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* This is an abstract class for creating DependencyFactories, a pattern
|
|
25
|
+
* to bundle multiple smaller services into one and register them into the
|
|
26
|
+
* injection context.
|
|
27
|
+
*
|
|
28
|
+
* This can for example be a StorageDependencyFactory that creates dependencies
|
|
29
|
+
* like StateService, MerkleWitnessService, etc. So in general, services that
|
|
30
|
+
* are not ConfigurableModules, but still are their own logical unit.
|
|
31
|
+
*
|
|
32
|
+
* DependencyFactories are designed to only be used statically for sets of
|
|
33
|
+
* deps that are necessary for the sequencer to work.
|
|
34
|
+
*
|
|
35
|
+
* Every Factory need the @dependencyFactory annotation (which basically
|
|
36
|
+
* proxies @injectable()) and every method that returns a dependency has to be
|
|
37
|
+
* of the format `() => Dependency` and be annotated with @dependency.
|
|
38
|
+
*/
|
|
39
|
+
export abstract class DependencyFactory {
|
|
40
|
+
public initDependencies(container: DependencyContainer) {
|
|
41
|
+
const dependencies =
|
|
42
|
+
globalFactoryDependencies.get(this.constructor.name) ?? {};
|
|
43
|
+
globalFactoryDependencies.delete(this.constructor.name);
|
|
44
|
+
|
|
45
|
+
for (const [key, useFactory] of Object.entries(dependencies)) {
|
|
46
|
+
container.register(`${key}_singleton-prototype`, {
|
|
47
|
+
useFactory: useFactory.bind(this),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const upperCaseKey = key.charAt(0).toUpperCase() + key.slice(1);
|
|
51
|
+
|
|
52
|
+
container.register(
|
|
53
|
+
upperCaseKey,
|
|
54
|
+
{
|
|
55
|
+
useToken: `${key}_singleton-prototype`,
|
|
56
|
+
},
|
|
57
|
+
{ lifecycle: Lifecycle.ContainerScoped }
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// type DTypes = { [key: string]: TypedClass<unknown> }
|
|
64
|
+
// type Factories<T extends DTypes> = {
|
|
65
|
+
// [key in keyof T]: T[key] extends TypedClass<infer R> ? () => R : never
|
|
66
|
+
// }
|
|
67
|
+
//
|
|
68
|
+
// export abstract class DF2<Types extends DTypes> {
|
|
69
|
+
// public constructor(private factories: Factories<Types>) {
|
|
70
|
+
//
|
|
71
|
+
// }
|
|
72
|
+
// generateDependencies(): Types {
|
|
73
|
+
// let x = this.factories;
|
|
74
|
+
// return {} as Types;
|
|
75
|
+
// }
|
|
76
|
+
// }
|
|
77
|
+
|
|
78
|
+
// export function DF2C<T extends Types>(object: Factories<T>): T {
|
|
79
|
+
// const c = class C extends DF2<T>{
|
|
80
|
+
// generateDependencies(): T {
|
|
81
|
+
// return undefined;
|
|
82
|
+
// }
|
|
83
|
+
// }
|
|
84
|
+
// return new c();
|
|
85
|
+
// }
|
|
86
|
+
|
|
87
|
+
// class DF2I extends DF2<{x: typeof ConfigurableModule}> {
|
|
88
|
+
// constructor() {
|
|
89
|
+
// super({
|
|
90
|
+
// x: this.x
|
|
91
|
+
// });
|
|
92
|
+
// }
|
|
93
|
+
//
|
|
94
|
+
// x(): ConfigurableModule<any> {
|
|
95
|
+
// return {} as ConfigurableModule<any>;
|
|
96
|
+
// }
|
|
97
|
+
//
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
export function dependency() {
|
|
101
|
+
return function decorator<Target extends DependencyFactory, Dependency>(
|
|
102
|
+
target: Target,
|
|
103
|
+
key: string,
|
|
104
|
+
descriptor: TypedPropertyDescriptor<() => Dependency>
|
|
105
|
+
) {
|
|
106
|
+
if (descriptor.value !== undefined) {
|
|
107
|
+
const className = target.constructor.name;
|
|
108
|
+
if (!globalFactoryDependencies.has(className)) {
|
|
109
|
+
globalFactoryDependencies.set(className, {});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
113
|
+
globalFactoryDependencies.get(target.constructor.name)![key] =
|
|
114
|
+
descriptor.value;
|
|
115
|
+
|
|
116
|
+
descriptor.value = () => {
|
|
117
|
+
throw errors.dependencyFactoryCalledDirectly();
|
|
118
|
+
};
|
|
119
|
+
} else {
|
|
120
|
+
throw errors.descriptorUndefined();
|
|
121
|
+
}
|
|
122
|
+
return descriptor;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function dependencyFactory() {
|
|
127
|
+
return function decorator<Target extends DependencyFactory>(
|
|
128
|
+
target: TypedClass<Target>
|
|
129
|
+
) {
|
|
130
|
+
injectable()(target);
|
|
131
|
+
};
|
|
132
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,5 +5,6 @@ export * from "./zkProgrammable/ZkProgrammable";
|
|
|
5
5
|
export * from "./zkProgrammable/ProvableMethodExecutionContext";
|
|
6
6
|
export * from "./zkProgrammable/provableMethod";
|
|
7
7
|
export * from "./utils";
|
|
8
|
+
export * from "./dependencyFactory/DependencyFactory";
|
|
8
9
|
// eslint-disable-next-line import/no-unused-modules
|
|
9
10
|
export { default as log } from "loglevel";
|
package/src/utils.ts
CHANGED
|
@@ -8,3 +8,11 @@ export function requireTrue(
|
|
|
8
8
|
: errorOrFunction;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
+
|
|
12
|
+
export function range(startOrEnd: number, end: number | undefined): number[] {
|
|
13
|
+
if (end === undefined) {
|
|
14
|
+
end = startOrEnd;
|
|
15
|
+
startOrEnd = 0;
|
|
16
|
+
}
|
|
17
|
+
return Array.from({ length: end - startOrEnd }, (ignored, index) => index);
|
|
18
|
+
}
|