@bluelibs/runner 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +231 -12
  2. package/dist/DependencyProcessor.d.ts +2 -2
  3. package/dist/DependencyProcessor.js +3 -3
  4. package/dist/DependencyProcessor.js.map +1 -1
  5. package/dist/EventManager.d.ts +4 -0
  6. package/dist/EventManager.js +15 -0
  7. package/dist/EventManager.js.map +1 -1
  8. package/dist/Store.d.ts +30 -2
  9. package/dist/Store.js +134 -29
  10. package/dist/Store.js.map +1 -1
  11. package/dist/TaskRunner.d.ts +3 -0
  12. package/dist/TaskRunner.js +3 -0
  13. package/dist/TaskRunner.js.map +1 -1
  14. package/dist/define.js +2 -7
  15. package/dist/define.js.map +1 -1
  16. package/dist/defs.d.ts +15 -4
  17. package/dist/errors.d.ts +2 -0
  18. package/dist/errors.js +2 -0
  19. package/dist/errors.js.map +1 -1
  20. package/dist/globalEvents.d.ts +2 -0
  21. package/dist/globalEvents.js +3 -0
  22. package/dist/globalEvents.js.map +1 -1
  23. package/dist/globalResources.d.ts +8 -6
  24. package/dist/globalResources.js +9 -4
  25. package/dist/globalResources.js.map +1 -1
  26. package/dist/index.d.ts +8 -6
  27. package/dist/index.js +3 -3
  28. package/dist/index.js.map +1 -1
  29. package/dist/models/DependencyProcessor.d.ts +49 -0
  30. package/dist/models/DependencyProcessor.js +178 -0
  31. package/dist/models/DependencyProcessor.js.map +1 -0
  32. package/dist/models/EventManager.d.ts +17 -0
  33. package/dist/models/EventManager.js +73 -0
  34. package/dist/models/EventManager.js.map +1 -0
  35. package/dist/models/Logger.d.ts +33 -0
  36. package/dist/models/Logger.js +76 -0
  37. package/dist/models/Logger.js.map +1 -0
  38. package/dist/models/ResourceInitializer.d.ts +13 -0
  39. package/dist/models/ResourceInitializer.js +54 -0
  40. package/dist/models/ResourceInitializer.js.map +1 -0
  41. package/dist/models/Store.d.ts +90 -0
  42. package/dist/models/Store.js +302 -0
  43. package/dist/models/Store.js.map +1 -0
  44. package/dist/models/TaskRunner.d.ts +25 -0
  45. package/dist/models/TaskRunner.js +96 -0
  46. package/dist/models/TaskRunner.js.map +1 -0
  47. package/dist/models/index.d.ts +5 -0
  48. package/dist/models/index.js +22 -0
  49. package/dist/models/index.js.map +1 -0
  50. package/dist/run.d.ts +4 -4
  51. package/dist/run.js +14 -7
  52. package/dist/run.js.map +1 -1
  53. package/package.json +1 -2
  54. package/src/__tests__/index.ts +8 -4
  55. package/src/__tests__/{EventManager.test.ts → models/EventManager.test.ts} +20 -2
  56. package/src/__tests__/models/Logger.test.ts +140 -0
  57. package/src/__tests__/{ResourceInitializer.test.ts → models/ResourceInitializer.test.ts} +4 -4
  58. package/src/__tests__/models/Store.test.ts +128 -0
  59. package/src/__tests__/{TaskRunner.test.ts → models/TaskRunner.test.ts} +5 -5
  60. package/src/__tests__/run.overrides.test.ts +392 -0
  61. package/src/__tests__/run.test.ts +28 -1
  62. package/src/define.ts +4 -8
  63. package/src/defs.ts +20 -4
  64. package/src/errors.ts +6 -0
  65. package/src/globalEvents.ts +4 -0
  66. package/src/globalResources.ts +18 -11
  67. package/src/index.ts +3 -3
  68. package/src/{DependencyProcessor.ts → models/DependencyProcessor.ts} +6 -6
  69. package/src/{EventManager.ts → models/EventManager.ts} +21 -1
  70. package/src/models/Logger.ts +100 -0
  71. package/src/{ResourceInitializer.ts → models/ResourceInitializer.ts} +2 -2
  72. package/src/{Store.ts → models/Store.ts} +181 -41
  73. package/src/{TaskRunner.ts → models/TaskRunner.ts} +6 -3
  74. package/src/models/index.ts +5 -0
  75. package/src/run.ts +17 -9
  76. package/src/__tests__/Store.test.ts +0 -143
@@ -0,0 +1,302 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Store = void 0;
4
+ const defs_1 = require("../defs");
5
+ const utils = require("../define");
6
+ const globalEvents_1 = require("../globalEvents");
7
+ const errors_1 = require("../errors");
8
+ const globalResources_1 = require("../globalResources");
9
+ /**
10
+ * Store class which is used to store all the resources, tasks, middleware and events.
11
+ */
12
+ class Store {
13
+ eventManager;
14
+ root;
15
+ tasks = new Map();
16
+ resources = new Map();
17
+ events = new Map();
18
+ middlewares = new Map();
19
+ overrides = new Map();
20
+ #isLocked = false;
21
+ #isInitialized = false;
22
+ constructor(eventManager) {
23
+ this.eventManager = eventManager;
24
+ }
25
+ get isLocked() {
26
+ return this.#isLocked;
27
+ }
28
+ lock() {
29
+ this.#isLocked = true;
30
+ this.eventManager.lock();
31
+ }
32
+ checkLock() {
33
+ if (this.#isLocked) {
34
+ throw new Error("Cannot modify the Store when it is locked.");
35
+ }
36
+ }
37
+ /**
38
+ * Store the root before beginning registration
39
+ * @param root
40
+ * @param config
41
+ */
42
+ initializeStore(root, config) {
43
+ if (this.#isInitialized) {
44
+ throw errors_1.Errors.storeAlreadyInitialized();
45
+ }
46
+ this.storeGenericItem(globalResources_1.globalResources.eventManager.with(this.eventManager));
47
+ this.storeGenericItem(globalResources_1.globalResources.store.with(this));
48
+ root.dependencies =
49
+ typeof root.dependencies === "function"
50
+ ? root.dependencies(config)
51
+ : root.dependencies;
52
+ this.root = {
53
+ resource: root,
54
+ computedDependencies: {},
55
+ config,
56
+ value: undefined,
57
+ isInitialized: false,
58
+ };
59
+ // register global events
60
+ globalEvents_1.globalEventsArray.forEach((event) => {
61
+ this.storeEvent(event);
62
+ });
63
+ this.computeRegistrationDeeply(root, config);
64
+ this.resources.set(root.id, this.root);
65
+ for (const resource of this.resources.values()) {
66
+ this.storeOverridesDeeply(resource.resource);
67
+ }
68
+ this.#isInitialized = true;
69
+ }
70
+ /**
71
+ * Beginning with the root, we perform registering to the container all the resources, tasks, middleware and events.
72
+ * @param element
73
+ * @param config
74
+ */
75
+ computeRegistrationDeeply(element, config) {
76
+ const items = typeof element.register === "function"
77
+ ? element.register(config)
78
+ : element.register;
79
+ for (const item of items) {
80
+ // will call registration if it detects another resource.
81
+ this.storeGenericItem(item);
82
+ }
83
+ }
84
+ /**
85
+ * @param element
86
+ */
87
+ storeOverridesDeeply(element) {
88
+ element.overrides.forEach((override) => {
89
+ // the one on top has priority of setting the last override.
90
+ if (utils.isResource(override)) {
91
+ this.storeOverridesDeeply(override);
92
+ }
93
+ if (utils.isResourceWithConfig(override)) {
94
+ this.storeOverridesDeeply(override.resource);
95
+ this.overrides.set(override.resource.id, override);
96
+ }
97
+ else {
98
+ this.overrides.set(override.id, override);
99
+ }
100
+ });
101
+ }
102
+ /**
103
+ * middlewares are already stored in their final form and the check for them would be redundant
104
+ * @param id
105
+ */
106
+ checkIfIDExists(id) {
107
+ if (this.tasks.has(id)) {
108
+ throw errors_1.Errors.duplicateRegistration("Task", id);
109
+ }
110
+ if (this.resources.has(id)) {
111
+ throw errors_1.Errors.duplicateRegistration("Resource", id);
112
+ }
113
+ if (this.events.has(id)) {
114
+ throw errors_1.Errors.duplicateRegistration("Event", id);
115
+ }
116
+ if (this.middlewares.has(id)) {
117
+ throw errors_1.Errors.duplicateRegistration("Middleware", id);
118
+ }
119
+ }
120
+ /**
121
+ * Cleanup
122
+ */
123
+ async dispose() {
124
+ for (const resource of this.resources.values()) {
125
+ if (resource.resource.dispose) {
126
+ await resource.resource.dispose(resource.value, resource.config, resource.computedDependencies);
127
+ }
128
+ }
129
+ }
130
+ /**
131
+ * When this is called, all overrides should have been stored in the "overrides" store.
132
+ */
133
+ processOverrides() {
134
+ // If we are trying to use override on something that wasn't previously registered, we throw an error.
135
+ for (const override of this.overrides.values()) {
136
+ let hasAnyItem = false;
137
+ if (utils.isTask(override)) {
138
+ hasAnyItem = this.tasks.has(override.id);
139
+ }
140
+ else if (utils.isResource(override)) {
141
+ hasAnyItem = this.resources.has(override.id);
142
+ }
143
+ else if (utils.isMiddleware(override)) {
144
+ hasAnyItem = this.middlewares.has(override.id);
145
+ }
146
+ else if (utils.isResourceWithConfig(override)) {
147
+ hasAnyItem = this.resources.has(override.resource.id);
148
+ }
149
+ if (!hasAnyItem) {
150
+ const id = utils.isResourceWithConfig(override)
151
+ ? override.resource.id
152
+ : override.id;
153
+ throw errors_1.Errors.dependencyNotFound(id);
154
+ }
155
+ }
156
+ for (const override of this.overrides.values()) {
157
+ if (utils.isTask(override)) {
158
+ this.storeTask(override, false);
159
+ }
160
+ else if (utils.isResource(override)) {
161
+ this.storeResource(override, false);
162
+ }
163
+ else if (utils.isMiddleware(override)) {
164
+ this.storeMiddleware(override, false);
165
+ }
166
+ else if (utils.isResourceWithConfig(override)) {
167
+ this.storeResourceWithConfig(override, false);
168
+ }
169
+ }
170
+ }
171
+ getGlobalMiddlewares(excludingIds) {
172
+ return Array.from(this.middlewares.values())
173
+ .filter((x) => x.middleware[defs_1.symbols.middlewareGlobal])
174
+ .filter((x) => !excludingIds.includes(x.middleware.id))
175
+ .map((x) => x.middleware);
176
+ }
177
+ /**
178
+ * If you want to register something to the store you can use this function.
179
+ * @param item
180
+ */
181
+ storeGenericItem(item) {
182
+ if (utils.isTask(item)) {
183
+ this.storeTask(item);
184
+ }
185
+ else if (utils.isResource(item)) {
186
+ // Registration a simple resource, which is interpreted as a resource with no configuration.
187
+ this.storeResource(item);
188
+ }
189
+ else if (utils.isEvent(item)) {
190
+ this.storeEvent(item);
191
+ }
192
+ else if (utils.isMiddleware(item)) {
193
+ this.storeMiddleware(item);
194
+ }
195
+ else if (utils.isResourceWithConfig(item)) {
196
+ this.storeResourceWithConfig(item);
197
+ }
198
+ else {
199
+ throw errors_1.Errors.unknownItemType(item);
200
+ }
201
+ }
202
+ storeMiddleware(item, check = true) {
203
+ check && this.checkIfIDExists(item.id);
204
+ item.dependencies =
205
+ typeof item.dependencies === "function"
206
+ ? item.dependencies()
207
+ : item.dependencies;
208
+ this.middlewares.set(item.id, {
209
+ middleware: item,
210
+ computedDependencies: {},
211
+ });
212
+ }
213
+ storeEvent(item) {
214
+ this.checkIfIDExists(item.id);
215
+ this.events.set(item.id, { event: item });
216
+ }
217
+ storeResourceWithConfig(item, check = true) {
218
+ check && this.checkIfIDExists(item.resource.id);
219
+ this.prepareResource(item.resource, item.config);
220
+ this.resources.set(item.resource.id, {
221
+ resource: item.resource,
222
+ config: item.config,
223
+ value: undefined,
224
+ isInitialized: false,
225
+ });
226
+ this.computeRegistrationDeeply(item.resource, item.config);
227
+ }
228
+ /**
229
+ * This is for storing a resource without a config.
230
+ * @param item
231
+ */
232
+ storeResource(item, check = true) {
233
+ check && this.checkIfIDExists(item.id);
234
+ this.prepareResource(item, {});
235
+ this.resources.set(item.id, {
236
+ resource: item,
237
+ config: {},
238
+ value: undefined,
239
+ isInitialized: false,
240
+ });
241
+ this.computeRegistrationDeeply(item, {});
242
+ }
243
+ storeEventsForAllTasks() {
244
+ for (const task of this.tasks.values()) {
245
+ this.storeEvent(task.task.events.beforeRun);
246
+ this.storeEvent(task.task.events.afterRun);
247
+ this.storeEvent(task.task.events.onError);
248
+ }
249
+ for (const resource of this.resources.values()) {
250
+ this.storeEvent(resource.resource.events.beforeInit);
251
+ this.storeEvent(resource.resource.events.afterInit);
252
+ this.storeEvent(resource.resource.events.onError);
253
+ }
254
+ }
255
+ /**
256
+ * This is for storing a resource without a config.
257
+ * @param item
258
+ */
259
+ prepareResource(item, config) {
260
+ item.dependencies =
261
+ typeof item.dependencies === "function"
262
+ ? item.dependencies(config)
263
+ : item.dependencies;
264
+ return item;
265
+ }
266
+ storeTask(item, check = true) {
267
+ check && this.checkIfIDExists(item.id);
268
+ item.dependencies =
269
+ typeof item.dependencies === "function"
270
+ ? item.dependencies()
271
+ : item.dependencies;
272
+ this.tasks.set(item.id, {
273
+ task: item,
274
+ computedDependencies: {},
275
+ isInitialized: false,
276
+ });
277
+ }
278
+ getDependentNodes() {
279
+ const depenedants = [];
280
+ for (const task of this.tasks.values()) {
281
+ depenedants.push({
282
+ id: task.task.id,
283
+ dependencies: task.task.dependencies,
284
+ });
285
+ }
286
+ for (const middleware of this.middlewares.values()) {
287
+ depenedants.push({
288
+ id: middleware.middleware.id,
289
+ dependencies: middleware.middleware.dependencies,
290
+ });
291
+ }
292
+ for (const resource of this.resources.values()) {
293
+ depenedants.push({
294
+ id: resource.resource.id,
295
+ dependencies: resource.resource.dependencies || {},
296
+ });
297
+ }
298
+ return depenedants;
299
+ }
300
+ }
301
+ exports.Store = Store;
302
+ //# sourceMappingURL=Store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Store.js","sourceRoot":"","sources":["../../src/models/Store.ts"],"names":[],"mappings":";;;AAAA,kCAWiB;AACjB,mCAAmC;AAEnC,kDAAoD;AACpD,sCAAmC;AACnC,wDAAqD;AAoCrD;;GAEG;AACH,MAAa,KAAK;IAce;IAb/B,IAAI,CAA4B;IACzB,KAAK,GAAsC,IAAI,GAAG,EAAE,CAAC;IACrD,SAAS,GAA0C,IAAI,GAAG,EAAE,CAAC;IAC7D,MAAM,GAAuC,IAAI,GAAG,EAAE,CAAC;IACvD,WAAW,GAA4C,IAAI,GAAG,EAAE,CAAC;IACjE,SAAS,GAGZ,IAAI,GAAG,EAAE,CAAC;IAEd,SAAS,GAAG,KAAK,CAAC;IAClB,cAAc,GAAG,KAAK,CAAC;IAEvB,YAA+B,YAA0B;QAA1B,iBAAY,GAAZ,YAAY,CAAc;IAAG,CAAC;IAE7D,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI;QACF,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;IAED,SAAS;QACP,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,eAAe,CAAC,IAAoB,EAAE,MAAW;QAC/C,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,MAAM,eAAM,CAAC,uBAAuB,EAAE,CAAC;QACzC,CAAC;QAED,I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ependencyMapType, DependencyValuesType, ITask } from "../defs";
2
+ import { EventManager } from "./EventManager";
3
+ import { Store } from "./Store";
4
+ export declare class TaskRunner {
5
+ protected readonly store: Store;
6
+ protected readonly eventManager: EventManager;
7
+ protected readonly runnerStore: Map<string, (input: any) => Promise<any>>;
8
+ constructor(store: Store, eventManager: EventManager);
9
+ /**
10
+ * Begins the execution of an task. These are registered tasks and all sanity checks have been performed at this stage to ensure consistency of the object.
11
+ * This function can throw only if any of the event listeners or run function throws
12
+ * @param task the task to be run
13
+ * @param input the input to be passed to the task
14
+ * @param taskDependencies optional dependencies to be passed to the task, if not provided, the dependencies will be the ones already computed from the store.
15
+ */
16
+ run<TInput, TOutput extends Promise<any>, TDeps extends DependencyMapType>(task: ITask<TInput, TOutput, TDeps>, input: TInput, taskDependencies?: DependencyValuesType<TDeps>): Promise<TOutput | undefined>;
17
+ /**
18
+ * Creates the function with the chain of middleware.
19
+ * @param task
20
+ * @param input
21
+ * @param taskDependencies
22
+ * @returns
23
+ */
24
+ protected createRunnerWithMiddleware<TInput, TOutput extends Promise<any>, TDeps extends DependencyMapType>(task: ITask<TInput, TOutput, TDeps>, taskDependencies: DependencyValuesType<{}>): (input: any) => Promise<TOutput>;
25
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskRunner = void 0;
4
+ const globalEvents_1 = require("../globalEvents");
5
+ class TaskRunner {
6
+ store;
7
+ eventManager;
8
+ runnerStore = new Map();
9
+ constructor(store, eventManager) {
10
+ this.store = store;
11
+ this.eventManager = eventManager;
12
+ }
13
+ /**
14
+ * Begins the execution of an task. These are registered tasks and all sanity checks have been performed at this stage to ensure consistency of the object.
15
+ * This function can throw only if any of the event listeners or run function throws
16
+ * @param task the task to be run
17
+ * @param input the input to be passed to the task
18
+ * @param taskDependencies optional dependencies to be passed to the task, if not provided, the dependencies will be the ones already computed from the store.
19
+ */
20
+ async run(task, input, taskDependencies) {
21
+ let runner = this.runnerStore.get(task.id);
22
+ if (!runner) {
23
+ const storeTask = this.store.tasks.get(task.id);
24
+ const deps = taskDependencies || storeTask.computedDependencies;
25
+ runner = this.createRunnerWithMiddleware(task, deps);
26
+ this.runnerStore.set(task.id, runner);
27
+ }
28
+ // begin by dispatching the event of creating it.
29
+ // then ensure the hooks are called
30
+ // then ensure the middleware are called
31
+ await this.eventManager.emit(task.events.beforeRun, { input });
32
+ await this.eventManager.emit(globalEvents_1.globalEvents.tasks.beforeRun, {
33
+ task,
34
+ input,
35
+ });
36
+ let error;
37
+ try {
38
+ // craft the next function starting from the first next function
39
+ const output = await runner(input);
40
+ await this.eventManager.emit(task.events.afterRun, { input, output });
41
+ await this.eventManager.emit(globalEvents_1.globalEvents.tasks.afterRun, {
42
+ task,
43
+ input,
44
+ output,
45
+ });
46
+ return output;
47
+ }
48
+ catch (e) {
49
+ error = e;
50
+ // If you want to rewthrow the error, this should be done inside the onError event.
51
+ await this.eventManager.emit(task.events.onError, { error });
52
+ await this.eventManager.emit(globalEvents_1.globalEvents.tasks.onError, {
53
+ task,
54
+ error,
55
+ });
56
+ throw e;
57
+ }
58
+ }
59
+ /**
60
+ * Creates the function with the chain of middleware.
61
+ * @param task
62
+ * @param input
63
+ * @param taskDependencies
64
+ * @returns
65
+ */
66
+ createRunnerWithMiddleware(task, taskDependencies) {
67
+ // this is the final next()
68
+ let next = async (input) => {
69
+ return task.run.call(null, input, taskDependencies);
70
+ };
71
+ const existingMiddlewares = task.middleware;
72
+ const createdMiddlewares = [
73
+ ...this.store.getGlobalMiddlewares(existingMiddlewares.map((x) => x.id)),
74
+ ...existingMiddlewares,
75
+ ];
76
+ if (createdMiddlewares.length > 0) {
77
+ // we need to run the middleware in reverse order
78
+ // so we can chain the next function
79
+ for (let i = createdMiddlewares.length - 1; i >= 0; i--) {
80
+ const middleware = createdMiddlewares[i];
81
+ const storeMiddleware = this.store.middlewares.get(middleware.id); // we know it exists because at this stage all sanity checks have been done.
82
+ const nextFunction = next;
83
+ next = async (input) => {
84
+ return storeMiddleware.middleware.run({
85
+ taskDefinition: task,
86
+ input,
87
+ next: nextFunction,
88
+ }, storeMiddleware.computedDependencies);
89
+ };
90
+ }
91
+ }
92
+ return next;
93
+ }
94
+ }
95
+ exports.TaskRunner = TaskRunner;
96
+ //# sourceMappingURL=TaskRunner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../../src/models/TaskRunner.ts"],"names":[],"mappings":";;;AAGA,kDAA+C;AAO/C,MAAa,UAAU;IAOA;IACA;IAPF,WAAW,GAAG,IAAI,GAAG,EAGrC,CAAC;IAEJ,YACqB,KAAY,EACZ,YAA0B;QAD1B,UAAK,GAAL,KAAK,CAAO;QACZ,iBAAY,GAAZ,YAAY,CAAc;IAC5C,CAAC;IAEJ;;;;;;OAMG;IACI,KAAK,CAAC,GAAG,CAKd,IAAmC,EACnC,KAAa,EACb,gBAA8C;QAE9C,IAAI,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC3C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAyB,CAAC;YACxE,MAAM,IAAI,GAAG,gBAAgB,IAAI,SAAS,CAAC,oBAAoB,CAAC;YAEhE,MAAM,GAAG,IAAI,CAAC,0BAA0B,CACtC,IAAI,EACJ,IAAI,CACL,CAAC;YAEF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QACxC,CAAC;QAED,iDAAiD;QACjD,mCAAmC;QACnC,wCAAwC;QACxC,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/D,MAAM,IAAI,CA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C,KAAK,CAAC,oBAAoB,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACxE,GAAG,mBAAmB;SACvB,CAAC;QAEF,IAAI,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAClC,iDAAiD;YACjD,oCAAoC;YACpC,KAAK,IAAI,CAAC,GAAG,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;gBACxD,MAAM,UAAU,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAAC;gBACzC,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAChD,UAAU,CAAC,EAAE,CACgB,CAAC,CAAC,4EAA4E;gBAE7G,MAAM,YAAY,GAAG,IAAI,CAAC;gBAC1B,IAAI,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE;oBACrB,OAAO,eAAe,CAAC,UAAU,CAAC,GAAG,CACnC;wBACE,cAAc,EAAE,IAAW;wBAC3B,KAAK;wBACL,IAAI,EAAE,YAAY;qBACnB,EACD,eAAe,CAAC,oBAAoB,CACrC,CAAC;gBACJ,CAAC,CAAC;YACJ,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA/HD,gCA+HC"}
@@ -0,0 +1,5 @@
1
+ export * from "./DependencyProcessor";
2
+ export * from "./EventManager";
3
+ export * from "./Logger";
4
+ export * from "./Store";
5
+ export * from "./TaskRunner";
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./DependencyProcessor"), exports);
18
+ __exportStar(require("./EventManager"), exports);
19
+ __exportStar(require("./Logger"), exports);
20
+ __exportStar(require("./Store"), exports);
21
+ __exportStar(require("./TaskRunner"), exports);
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/models/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAsC;AACtC,iDAA+B;AAC/B,2CAAyB;AACzB,0CAAwB;AACxB,+CAA6B"}
package/dist/run.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { TaskRunner } from "./TaskRunner";
1
+ import { TaskRunner } from "./models/TaskRunner";
2
2
  import { DependencyMapType, ITaskDefinition, IResourceDefinintion, IEventDefinition, IMiddlewareDefinition, DependencyValuesType, IResource } from "./defs";
3
- import { EventManager } from "./EventManager";
4
- import { Store } from "./Store";
3
+ import { EventManager } from "./models/EventManager";
4
+ import { Store } from "./models/Store";
5
5
  export type ResourcesStoreElementType<C = any, V = any, D extends DependencyMapType = {}> = {
6
6
  resource: IResourceDefinintion<C, V, D>;
7
7
  computedDependencies?: DependencyValuesType<D>;
@@ -29,4 +29,4 @@ export type RunnerType = {
29
29
  eventManager: EventManager;
30
30
  taskRunner: TaskRunner;
31
31
  };
32
- export declare function run<C, V>(resource: IResource<C>, config?: C): Promise<V>;
32
+ export declare function run<C, V>(resource: IResource<C, V>, config?: C): Promise<V>;
package/dist/run.js CHANGED
@@ -1,39 +1,46 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.run = run;
4
- const TaskRunner_1 = require("./TaskRunner");
5
- const DependencyProcessor_1 = require("./DependencyProcessor");
6
- const EventManager_1 = require("./EventManager");
4
+ const TaskRunner_1 = require("./models/TaskRunner");
5
+ const DependencyProcessor_1 = require("./models/DependencyProcessor");
6
+ const EventManager_1 = require("./models/EventManager");
7
7
  const globalEvents_1 = require("./globalEvents");
8
- const Store_1 = require("./Store");
8
+ const Store_1 = require("./models/Store");
9
9
  const findCircularDependencies_1 = require("./tools/findCircularDependencies");
10
10
  const errors_1 = require("./errors");
11
11
  const globalResources_1 = require("./globalResources");
12
+ const Logger_1 = require("./models/Logger");
12
13
  async function run(resource, config) {
13
14
  const eventManager = new EventManager_1.EventManager();
14
15
  const store = new Store_1.Store(eventManager);
15
16
  const taskRunner = new TaskRunner_1.TaskRunner(store, eventManager);
16
17
  const processor = new DependencyProcessor_1.DependencyProcessor(store, eventManager, taskRunner);
18
+ const logger = new Logger_1.Logger(eventManager);
17
19
  // In the registration phase we register deeply all the resources, tasks, middleware and events
18
20
  store.initializeStore(resource, config);
21
+ store.storeGenericItem(globalResources_1.globalResources.logger.with(logger));
19
22
  store.storeGenericItem(globalResources_1.globalResources.taskRunner.with(taskRunner));
20
- store.computeRegisterOfResource(resource, config);
21
23
  // We verify that there isn't any circular dependencies before we begin computing the dependencies
22
24
  const dependentNodes = store.getDependentNodes();
23
25
  const circularDependencies = (0, findCircularDependencies_1.findCircularDependencies)(dependentNodes);
24
26
  if (circularDependencies.cycles.length > 0) {
25
27
  throw errors_1.Errors.circularDependencies(circularDependencies.cycles);
26
28
  }
27
- await processor.processHooks();
29
+ await store.processOverrides();
30
+ // a form of hooking, we store the events for all tasks
31
+ await store.storeEventsForAllTasks();
32
+ await processor.attachHooks();
33
+ await processor.computeAllDependencies();
28
34
  // Now we can safely compute dependencies without being afraid of an infinite loop.
29
35
  // The hooking part is done here.
30
36
  await eventManager.emit(globalEvents_1.globalEvents.beforeInit);
31
- await processor.computeAllDependencies();
32
37
  // leftovers that were registered but not depended upon, except root
33
38
  await processor.initializeUninitializedResources();
34
39
  // Now we can initialise the root resource
35
40
  await processor.initializeRoot();
36
41
  await eventManager.emit(globalEvents_1.globalEvents.afterInit);
42
+ // disallow manipulation or attaching more
43
+ store.lock();
37
44
  return store.root.value;
38
45
  }
39
46
  //# sourceMappingURL=run.js.map
package/dist/run.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":";;AA2DA,kBAsCC;AAjGD,6CAA0C;AAU1C,+DAA4D;AAC5D,iDAA8C;AAC9C,iDAA8C;AAC9C,mCAAgC;AAChC,+EAA4E;AAC5E,qCAAkC;AAClC,uDAAoD;AA2C7C,KAAK,UAAU,GAAG,CACvB,QAAsB,EACtB,MAAU;IAEV,MAAM,YAAY,GAAG,IAAI,2BAAY,EAAE,CAAC;IACxC,MAAM,KAAK,GAAG,IAAI,aAAK,CAAC,YAAY,CAAC,CAAC;IACtC,MAAM,UAAU,GAAG,IAAI,uBAAU,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,IAAI,yCAAmB,CAAC,KAAK,EAAE,YAAY,EAAE,UAAU,CAAC,CAAC;IAE3E,+FAA+F;IAC/F,KAAK,CAAC,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IACxC,KAAK,CAAC,gBAAgB,CAAC,iCAAe,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;IACpE,KAAK,CAAC,yBAAyB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAElD,kGAAkG;IAClG,MAAM,cAAc,GAAG,KAAK,CAAC,iBAAiB,EAAE,CAAC;IACjD,MAAM,oBAAoB,GAAG,IAAA,mDAAwB,EAAC,cAAc,CAAC,CAAC;IACtE,IAAI,oBAAoB,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3C,MAA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M,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5D,KAAK,CAAC,gBAAgB,CAAC,iCAAe,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;IAEpE,kGAAkG;IAClG,MAAM,cAAc,GAAG,KAAK,CAAC,iBAAiB,EAAE,CAAC;IACjD,MAAM,oBAAoB,GAAG,IAAA,mDAAwB,EAAC,cAAc,CAAC,CAAC;IACtE,IAAI,oBAAoB,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3C,MAAM,eAAM,CAAC,oBAAoB,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,KAAK,CAAC,gBAAgB,EAAE,CAAC;IAE/B,uDAAuD;IACvD,MAAM,KAAK,CAAC,sBAAsB,EAAE,CAAC;IACrC,MAAM,SAAS,CAAC,WAAW,EAAE,CAAC;IAC9B,MAAM,SAAS,CAAC,sBAAsB,EAAE,CAAC;IAEzC,mFAAmF;IACnF,iCAAiC;IACjC,MAAM,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,UAAU,CAAC,CAAC;IAEjD,oEAAoE;IACpE,MAAM,SAAS,CAAC,gCAAgC,EAAE,CAAC;IAEnD,0CAA0C;IAC1C,MAAM,SAAS,CAAC,cAAc,EAAE,CAAC;IAEjC,MAAM,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,SAAS,CAAC,CAAC;IAEhD,0CAA0C;IAC1C,KAAK,CAAC,IAAI,EAAE,CAAC;IAEb,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;AAC1B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bluelibs/runner",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "BlueLibs Runner",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -17,7 +17,6 @@
17
17
  "test:clean": "jest --clearCache",
18
18
  "testonly": "npm test",
19
19
  "test:ci": "npm run coverage -- --ci --maxWorkers=2 --reporters=default --reporters=jest-junit",
20
- "coverage:upload": "codecov",
21
20
  "prepublishOnly": "npm run build",
22
21
  "typedoc": "typedoc",
23
22
  "benchmark": "jest --testMatch=\"**/__tests__/benchmark/benchmark.test.ts\" --testTimeout 10000"
@@ -1,9 +1,13 @@
1
- import "./EventManager.test";
1
+ import "./models/EventManager.test";
2
+ import "./models/ResourceInitializer.test";
3
+ import "./models/TaskRunner.test";
4
+ import "./models/Store.test";
5
+ import "./models/Logger.test";
6
+
2
7
  import "./tools/findCircularDependencies.test";
3
- import "./ResourceInitializer.test";
8
+
4
9
  import "./run.test";
5
10
  import "./run.hooks.test";
6
- import "./TaskRunner.test";
11
+ import "./run.overrides.test";
7
12
  import "./globalEvents.test";
8
- import "./Store.test";
9
13
  import "./errors.test";
@@ -1,5 +1,6 @@
1
- import { IEvent, IEventDefinition } from "../defs";
2
- import { EventManager } from "../EventManager";
1
+ import { IEvent, IEventDefinition } from "../../defs";
2
+ import { Errors } from "../../errors";
3
+ import { EventManager } from "../../models/EventManager";
3
4
 
4
5
  describe("EventManager", () => {
5
6
  let eventManager: EventManager;
@@ -93,4 +94,21 @@ describe("EventManager", () => {
93
94
  expect(globalListener).toHaveBeenCalledWith(createEvent("test data"));
94
95
  });
95
96
  });
97
+
98
+ it("should lock the EventManager", () => {
99
+ eventManager.lock();
100
+ expect(eventManager.isLocked).toBe(true);
101
+
102
+ expect(() => eventManager.checkLock()).toThrow(
103
+ Errors.locked("EventManager")
104
+ );
105
+
106
+ expect(() => eventManager.addListener(testEvent, jest.fn())).toThrow(
107
+ Errors.locked("EventManager")
108
+ );
109
+
110
+ expect(() => eventManager.addGlobalListener(jest.fn())).toThrow(
111
+ Errors.locked("EventManager")
112
+ );
113
+ });
96
114
  });