@bluelibs/runner 1.0.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.
- package/LICENSE.md +7 -0
- package/README.md +797 -0
- package/dist/DependencyProcessor.d.ts +49 -0
- package/dist/DependencyProcessor.js +178 -0
- package/dist/DependencyProcessor.js.map +1 -0
- package/dist/EventManager.d.ts +13 -0
- package/dist/EventManager.js +58 -0
- package/dist/EventManager.js.map +1 -0
- package/dist/ResourceInitializer.d.ts +13 -0
- package/dist/ResourceInitializer.js +54 -0
- package/dist/ResourceInitializer.js.map +1 -0
- package/dist/Store.d.ts +62 -0
- package/dist/Store.js +186 -0
- package/dist/Store.js.map +1 -0
- package/dist/TaskRunner.d.ts +22 -0
- package/dist/TaskRunner.js +93 -0
- package/dist/TaskRunner.js.map +1 -0
- package/dist/define.d.ts +10 -0
- package/dist/define.js +111 -0
- package/dist/define.js.map +1 -0
- package/dist/defs.d.ts +127 -0
- package/dist/defs.js +12 -0
- package/dist/defs.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/globalEvents.d.ts +36 -0
- package/dist/globalEvents.js +45 -0
- package/dist/globalEvents.js.map +1 -0
- package/dist/globalResources.d.ts +8 -0
- package/dist/globalResources.js +19 -0
- package/dist/globalResources.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/run.d.ts +32 -0
- package/dist/run.js +39 -0
- package/dist/run.js.map +1 -0
- package/dist/tools/findCircularDependencies.d.ts +16 -0
- package/dist/tools/findCircularDependencies.js +53 -0
- package/dist/tools/findCircularDependencies.js.map +1 -0
- package/package.json +50 -0
- package/src/DependencyProcessor.ts +243 -0
- package/src/EventManager.ts +84 -0
- package/src/ResourceInitializer.ts +69 -0
- package/src/Store.ts +250 -0
- package/src/TaskRunner.ts +135 -0
- package/src/__tests__/EventManager.test.ts +96 -0
- package/src/__tests__/ResourceInitializer.test.ts +109 -0
- package/src/__tests__/Store.test.ts +143 -0
- package/src/__tests__/TaskRunner.test.ts +135 -0
- package/src/__tests__/benchmark/benchmark.test.ts +146 -0
- package/src/__tests__/errors.test.ts +268 -0
- package/src/__tests__/globalEvents.test.ts +99 -0
- package/src/__tests__/index.ts +9 -0
- package/src/__tests__/run.hooks.test.ts +110 -0
- package/src/__tests__/run.test.ts +614 -0
- package/src/__tests__/tools/findCircularDependencies.test.ts +217 -0
- package/src/define.ts +142 -0
- package/src/defs.ts +221 -0
- package/src/errors.ts +22 -0
- package/src/globalEvents.ts +64 -0
- package/src/globalResources.ts +19 -0
- package/src/index.ts +28 -0
- package/src/run.ts +98 -0
- package/src/tools/findCircularDependencies.ts +69 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DependencyMapType, DependencyValuesType, ITask, IResource, IEventDefinition } from "./defs";
|
|
2
|
+
import { ResourceStoreElementType, Store } from "./Store";
|
|
3
|
+
import { EventManager } from "./EventManager";
|
|
4
|
+
import { ResourceInitializer } from "./ResourceInitializer";
|
|
5
|
+
import { TaskRunner } from "./TaskRunner";
|
|
6
|
+
/**
|
|
7
|
+
* This class is responsible of setting up dependencies with their respective computedValues.
|
|
8
|
+
* Note that all elements must have been previously registered otherwise errors will be thrown
|
|
9
|
+
* when trying to depend on something not in the store.
|
|
10
|
+
*/
|
|
11
|
+
export declare class DependencyProcessor {
|
|
12
|
+
protected readonly store: Store;
|
|
13
|
+
protected readonly eventManager: EventManager;
|
|
14
|
+
protected readonly taskRunner: TaskRunner;
|
|
15
|
+
protected readonly resourceInitializer: ResourceInitializer;
|
|
16
|
+
constructor(store: Store, eventManager: EventManager, taskRunner: TaskRunner);
|
|
17
|
+
/**
|
|
18
|
+
* This function is going to go through all the resources, tasks and middleware to compute their required dependencies.
|
|
19
|
+
*/
|
|
20
|
+
computeAllDependencies(): Promise<void>;
|
|
21
|
+
initializeUninitializedResources(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Processes dependencies and hooks
|
|
24
|
+
* @param resource
|
|
25
|
+
*/
|
|
26
|
+
protected processResourceDependencies(resource: ResourceStoreElementType<any, any, {}>): Promise<void>;
|
|
27
|
+
initializeRoot(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Processes all hooks, should run before emission of any event.
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
processHooks(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Processes the hooks for resources
|
|
35
|
+
* @param hooks
|
|
36
|
+
* @param deps
|
|
37
|
+
*/
|
|
38
|
+
processHooksForResource(resourceStoreElement: ResourceStoreElementType<any, any, {}>): void;
|
|
39
|
+
extractDependencies<T extends DependencyMapType>(map: T): Promise<DependencyValuesType<T>>;
|
|
40
|
+
extractDependency(object: any): Promise<any>;
|
|
41
|
+
/**
|
|
42
|
+
* Converts the event into a running functions with real inputs
|
|
43
|
+
* @param object
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
extractEventDependency(object: IEventDefinition<Record<string, any>>): (input: any) => Promise<void>;
|
|
47
|
+
extractTaskDependency(object: ITask<any, any, {}>): Promise<(input: any) => Promise<any>>;
|
|
48
|
+
extractResourceDependency(object: IResource<any, any, any>): Promise<any>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DependencyProcessor = void 0;
|
|
4
|
+
const utils = require("./define");
|
|
5
|
+
const ResourceInitializer_1 = require("./ResourceInitializer");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
/**
|
|
8
|
+
* This class is responsible of setting up dependencies with their respective computedValues.
|
|
9
|
+
* Note that all elements must have been previously registered otherwise errors will be thrown
|
|
10
|
+
* when trying to depend on something not in the store.
|
|
11
|
+
*/
|
|
12
|
+
class DependencyProcessor {
|
|
13
|
+
store;
|
|
14
|
+
eventManager;
|
|
15
|
+
taskRunner;
|
|
16
|
+
resourceInitializer;
|
|
17
|
+
constructor(store, eventManager, taskRunner) {
|
|
18
|
+
this.store = store;
|
|
19
|
+
this.eventManager = eventManager;
|
|
20
|
+
this.taskRunner = taskRunner;
|
|
21
|
+
this.resourceInitializer = new ResourceInitializer_1.ResourceInitializer(store, eventManager);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* This function is going to go through all the resources, tasks and middleware to compute their required dependencies.
|
|
25
|
+
*/
|
|
26
|
+
async computeAllDependencies() {
|
|
27
|
+
for (const middleware of this.store.middlewares.values()) {
|
|
28
|
+
const deps = middleware.middleware.dependencies;
|
|
29
|
+
middleware.computedDependencies = await this.extractDependencies(deps);
|
|
30
|
+
}
|
|
31
|
+
for (const task of this.store.tasks.values()) {
|
|
32
|
+
const deps = task.task.dependencies;
|
|
33
|
+
task.computedDependencies = await this.extractDependencies(deps);
|
|
34
|
+
let eventDefinition = task.task.on;
|
|
35
|
+
if (eventDefinition) {
|
|
36
|
+
if (this.store.events.get(eventDefinition.id) === undefined) {
|
|
37
|
+
throw errors_1.Errors.eventNotFound(eventDefinition.id);
|
|
38
|
+
}
|
|
39
|
+
this.eventManager.addListener(eventDefinition, async (receivedEvent) => {
|
|
40
|
+
return this.taskRunner.run(task.task, receivedEvent, task.computedDependencies);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const resource of this.store.resources.values()) {
|
|
45
|
+
await this.processResourceDependencies(resource);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Most likely these are resources that no-one has dependencies towards
|
|
49
|
+
// We need to ensure they work too!
|
|
50
|
+
async initializeUninitializedResources() {
|
|
51
|
+
for (const resource of this.store.resources.values()) {
|
|
52
|
+
if (resource.isInitialized === false &&
|
|
53
|
+
// The root is the last one to be initialized and is done in a separate process.
|
|
54
|
+
resource.resource.id !== this.store.root.resource.id) {
|
|
55
|
+
await this.processResourceDependencies(resource);
|
|
56
|
+
resource.value = await this.resourceInitializer.initializeResource(resource.resource, resource.config, resource.computedDependencies);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Processes dependencies and hooks
|
|
62
|
+
* @param resource
|
|
63
|
+
*/
|
|
64
|
+
async processResourceDependencies(resource) {
|
|
65
|
+
const deps = resource.resource.dependencies;
|
|
66
|
+
resource.computedDependencies = await this.extractDependencies(deps);
|
|
67
|
+
}
|
|
68
|
+
async initializeRoot() {
|
|
69
|
+
const storeResource = this.store.root;
|
|
70
|
+
storeResource.value = await this.resourceInitializer.initializeResource(storeResource.resource, storeResource.config,
|
|
71
|
+
// They are already computed
|
|
72
|
+
storeResource.computedDependencies);
|
|
73
|
+
storeResource.isInitialized = true;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Processes all hooks, should run before emission of any event.
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
processHooks() {
|
|
80
|
+
// iterate through resources and send them to processHooks
|
|
81
|
+
for (const resource of this.store.resources.values()) {
|
|
82
|
+
if (resource.resource.hooks) {
|
|
83
|
+
this.processHooksForResource(resource);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Processes the hooks for resources
|
|
89
|
+
* @param hooks
|
|
90
|
+
* @param deps
|
|
91
|
+
*/
|
|
92
|
+
processHooksForResource(resourceStoreElement) {
|
|
93
|
+
let hooks = resourceStoreElement.resource.hooks;
|
|
94
|
+
if (typeof hooks === "function") {
|
|
95
|
+
hooks = hooks(resourceStoreElement.config);
|
|
96
|
+
}
|
|
97
|
+
if (hooks.length === 0) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
for (const hook of hooks) {
|
|
101
|
+
const event = hook.event;
|
|
102
|
+
if (this.store.events.has(event.id) === false) {
|
|
103
|
+
throw errors_1.Errors.eventNotFound(event.id);
|
|
104
|
+
}
|
|
105
|
+
this.eventManager.addListener(event, async (receivedEvent) => {
|
|
106
|
+
return hook.run(receivedEvent, resourceStoreElement.computedDependencies);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async extractDependencies(map) {
|
|
111
|
+
const object = {};
|
|
112
|
+
for (const key in map) {
|
|
113
|
+
object[key] = await this.extractDependency(map[key]);
|
|
114
|
+
}
|
|
115
|
+
return object;
|
|
116
|
+
}
|
|
117
|
+
async extractDependency(object) {
|
|
118
|
+
if (utils.isResource(object)) {
|
|
119
|
+
return this.extractResourceDependency(object);
|
|
120
|
+
}
|
|
121
|
+
else if (utils.isTask(object)) {
|
|
122
|
+
return this.extractTaskDependency(object);
|
|
123
|
+
}
|
|
124
|
+
else if (utils.isEvent(object)) {
|
|
125
|
+
return this.extractEventDependency(object);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
throw errors_1.Errors.unknownItemType(object);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Converts the event into a running functions with real inputs
|
|
133
|
+
* @param object
|
|
134
|
+
* @returns
|
|
135
|
+
*/
|
|
136
|
+
extractEventDependency(object) {
|
|
137
|
+
return async (input) => {
|
|
138
|
+
return this.eventManager.emit(object, input);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
async extractTaskDependency(object) {
|
|
142
|
+
const storeTask = this.store.tasks.get(object.id);
|
|
143
|
+
if (storeTask === undefined) {
|
|
144
|
+
throw errors_1.Errors.dependencyNotFound(`Task ${object.id}`);
|
|
145
|
+
}
|
|
146
|
+
if (!storeTask.isInitialized) {
|
|
147
|
+
storeTask.isInitialized = true;
|
|
148
|
+
// it's sanitised
|
|
149
|
+
const dependencies = object.dependencies;
|
|
150
|
+
storeTask.computedDependencies = await this.extractDependencies(dependencies);
|
|
151
|
+
}
|
|
152
|
+
return (input) => {
|
|
153
|
+
return this.taskRunner.run(storeTask.task, input, storeTask.computedDependencies);
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async extractResourceDependency(object) {
|
|
157
|
+
// check if it exists in the store with the value
|
|
158
|
+
const storeResource = this.store.resources.get(object.id);
|
|
159
|
+
if (storeResource === undefined) {
|
|
160
|
+
throw errors_1.Errors.dependencyNotFound(`Resource ${object.id}`);
|
|
161
|
+
}
|
|
162
|
+
const { resource, config } = storeResource;
|
|
163
|
+
if (storeResource.isInitialized) {
|
|
164
|
+
return storeResource.value;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
// we need to initialize the resource
|
|
168
|
+
storeResource.isInitialized = true;
|
|
169
|
+
// check if it has an initialisation function that provides the value
|
|
170
|
+
if (resource.init) {
|
|
171
|
+
storeResource.value = await this.resourceInitializer.initializeResource(resource, config, await this.extractDependencies(resource.dependencies || {}));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return storeResource.value;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.DependencyProcessor = DependencyProcessor;
|
|
178
|
+
//# sourceMappingURL=DependencyProcessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DependencyProcessor.js","sourceRoot":"","sources":["../src/DependencyProcessor.ts"],"names":[],"mappings":";;;AASA,kCAAkC;AAElC,+DAA4D;AAE5D,qCAAkC;AAElC;;;;GAIG;AACH,MAAa,mBAAmB;IAIT;IACA;IACA;IALF,mBAAmB,CAAsB;IAE5D,YACqB,KAAY,EACZ,YAA0B,EAC1B,UAAsB;QAFtB,UAAK,GAAL,KAAK,CAAO;QACZ,iBAAY,GAAZ,YAAY,CAAc;QAC1B,eAAU,GAAV,UAAU,CAAY;QAEzC,IAAI,CAAC,mBAAmB,GAAG,IAAI,yCAAmB,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;IAC1E,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,sBAAsB;QAC1B,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,EAAE,EAAE,C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,CAC/B,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,yBAAyB,CAAC,MAAgC;QAC9D,iDAAiD;QACjD,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC1D,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,MAAM,eAAM,CAAC,kBAAkB,CAAC,YAAY,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAC3D,CAAC;QAED,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,aAAa,CAAC;QAC3C,IAAI,aAAa,CAAC,aAAa,EAAE,CAAC;YAChC,OAAO,aAAa,CAAC,KAAK,CAAC;QAC7B,CAAC;aAAM,CAAC;YACN,qCAAqC;YACrC,aAAa,CAAC,aAAa,GAAG,IAAI,CAAC;YAEnC,qEAAqE;YACrE,IAAI,QAAQ,CAAC,IAAI,EAAE,CAAC;gBAClB,aAAa,CAAC,KAAK,GAAG,MAAM,IAAI,CAAC,mBAAmB,CAAC,kBAAkB,CACrE,QAAQ,EACR,MAAM,EACN,MAAM,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,YAAY,IAAI,EAAE,CAAC,CAC5D,CAAC;YACJ,CAAC;QACH,CAAC;QAED,OAAO,aAAa,CAAC,KAAK,CAAC;IAC7B,CAAC;CACF;AA9ND,kDA8NC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EventHandlerType, IEvent, IEventDefinition } from "./defs";
|
|
2
|
+
export interface IEventHandlerOptions<T = any> {
|
|
3
|
+
order?: number;
|
|
4
|
+
filter?: (event: IEvent<T>) => boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare class EventManager {
|
|
7
|
+
private listeners;
|
|
8
|
+
private globalListeners;
|
|
9
|
+
emit<TInput>(eventDefinition: IEventDefinition<TInput>, ...args: TInput extends void ? [] : [TInput]): Promise<void>;
|
|
10
|
+
addListener<T>(event: IEventDefinition | Array<IEventDefinition>, handler: EventHandlerType<T>, options?: IEventHandlerOptions<T>): void;
|
|
11
|
+
addGlobalListener(handler: EventHandlerType, options?: IEventHandlerOptions): void;
|
|
12
|
+
private sortListeners;
|
|
13
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventManager = void 0;
|
|
4
|
+
const HandlerOptionsDefaults = { order: 0 };
|
|
5
|
+
class EventManager {
|
|
6
|
+
listeners = new Map();
|
|
7
|
+
globalListeners = [];
|
|
8
|
+
async emit(eventDefinition, ...args) {
|
|
9
|
+
const data = args[0];
|
|
10
|
+
const eventListeners = this.listeners.get(eventDefinition.id) || [];
|
|
11
|
+
const allListeners = this.sortListeners([
|
|
12
|
+
...eventListeners,
|
|
13
|
+
...this.globalListeners,
|
|
14
|
+
]);
|
|
15
|
+
const event = {
|
|
16
|
+
id: eventDefinition.id,
|
|
17
|
+
data,
|
|
18
|
+
};
|
|
19
|
+
for (const listener of allListeners) {
|
|
20
|
+
const ok = !listener.filter || listener.filter(event);
|
|
21
|
+
if (ok) {
|
|
22
|
+
await listener.handler(event);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
addListener(event, handler, options = HandlerOptionsDefaults) {
|
|
27
|
+
if (Array.isArray(event)) {
|
|
28
|
+
event.forEach((id) => this.addListener(id, handler, options));
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const eventId = event.id;
|
|
32
|
+
const listeners = this.listeners.get(eventId) || [];
|
|
33
|
+
const newListener = {
|
|
34
|
+
handler,
|
|
35
|
+
order: options.order || 0,
|
|
36
|
+
filter: options.filter,
|
|
37
|
+
};
|
|
38
|
+
const newListeners = this.sortListeners([...listeners, newListener]);
|
|
39
|
+
this.listeners.set(eventId, newListeners);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
addGlobalListener(handler, options = HandlerOptionsDefaults) {
|
|
43
|
+
const newListener = {
|
|
44
|
+
handler,
|
|
45
|
+
order: options.order || 0,
|
|
46
|
+
filter: options.filter,
|
|
47
|
+
};
|
|
48
|
+
this.globalListeners = this.sortListeners([
|
|
49
|
+
...this.globalListeners,
|
|
50
|
+
newListener,
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
sortListeners(listeners) {
|
|
54
|
+
return [...listeners].sort((a, b) => a.order - b.order);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.EventManager = EventManager;
|
|
58
|
+
//# sourceMappingURL=EventManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventManager.js","sourceRoot":"","sources":["../src/EventManager.ts"],"names":[],"mappings":";;;AAEA,MAAM,sBAAsB,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;AAa5C,MAAa,YAAY;IACf,SAAS,GAAoC,IAAI,GAAG,EAAE,CAAC;IACvD,eAAe,GAAuB,EAAE,CAAC;IAEjD,KAAK,CAAC,IAAI,CACR,eAAyC,EACzC,GAAG,IAAyC;QAE5C,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACrB,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;QACpE,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC;YACtC,GAAG,cAAc;YACjB,GAAG,IAAI,CAAC,eAAe;SACxB,CAAC,CAAC;QAEH,MAAM,KAAK,GAAW;YACpB,EAAE,EAAE,eAAe,CAAC,EAAE;YACtB,IAAI;SACL,CAAC;QAEF,KAAK,MAAM,QAAQ,IAAI,YAAY,EAAE,CAAC;YACpC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACtD,IAAI,EAAE,EAAE,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C,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC;CACF;AApED,oCAoEC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DependencyMapType, DependencyValuesType, IResource } from "./defs";
|
|
2
|
+
import { EventManager } from "./EventManager";
|
|
3
|
+
import { Store } from "./Store";
|
|
4
|
+
export declare class ResourceInitializer {
|
|
5
|
+
protected readonly store: Store;
|
|
6
|
+
protected readonly eventManager: EventManager;
|
|
7
|
+
constructor(store: Store, eventManager: EventManager);
|
|
8
|
+
/**
|
|
9
|
+
* 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.
|
|
10
|
+
* This function can throw only if any of the event listeners or run function throws
|
|
11
|
+
*/
|
|
12
|
+
initializeResource<TConfig = null, TValue = any, TDeps extends DependencyMapType = {}>(resource: IResource<TConfig, TValue, TDeps>, config: TConfig, dependencies: DependencyValuesType<TDeps>): Promise<TValue | undefined>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResourceInitializer = void 0;
|
|
4
|
+
const globalEvents_1 = require("./globalEvents");
|
|
5
|
+
class ResourceInitializer {
|
|
6
|
+
store;
|
|
7
|
+
eventManager;
|
|
8
|
+
constructor(store, eventManager) {
|
|
9
|
+
this.store = store;
|
|
10
|
+
this.eventManager = eventManager;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 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.
|
|
14
|
+
* This function can throw only if any of the event listeners or run function throws
|
|
15
|
+
*/
|
|
16
|
+
async initializeResource(resource, config, dependencies) {
|
|
17
|
+
// begin by dispatching the event of creating it.
|
|
18
|
+
// then ensure the hooks are called
|
|
19
|
+
// then ensure the middleware are called
|
|
20
|
+
await this.eventManager.emit(globalEvents_1.globalEvents.resources.beforeInit, {
|
|
21
|
+
config,
|
|
22
|
+
resource,
|
|
23
|
+
});
|
|
24
|
+
await this.eventManager.emit(resource.events.beforeInit, { config });
|
|
25
|
+
let error, value;
|
|
26
|
+
try {
|
|
27
|
+
if (resource.init) {
|
|
28
|
+
value = await resource.init(config, dependencies);
|
|
29
|
+
}
|
|
30
|
+
await this.eventManager.emit(resource.events.afterInit, {
|
|
31
|
+
config,
|
|
32
|
+
value,
|
|
33
|
+
});
|
|
34
|
+
await this.eventManager.emit(globalEvents_1.globalEvents.resources.afterInit, {
|
|
35
|
+
config,
|
|
36
|
+
resource,
|
|
37
|
+
value,
|
|
38
|
+
});
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
error = e;
|
|
43
|
+
// If you want to rewthrow the error, this should be done inside the onError event.
|
|
44
|
+
await this.eventManager.emit(resource.events.onError, { error });
|
|
45
|
+
await this.eventManager.emit(globalEvents_1.globalEvents.resources.onError, {
|
|
46
|
+
error,
|
|
47
|
+
resource,
|
|
48
|
+
});
|
|
49
|
+
throw e;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ResourceInitializer = ResourceInitializer;
|
|
54
|
+
//# sourceMappingURL=ResourceInitializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResourceInitializer.js","sourceRoot":"","sources":["../src/ResourceInitializer.ts"],"names":[],"mappings":";;;AAOA,iDAA8C;AAG9C,MAAa,mBAAmB;IAET;IACA;IAFrB,YACqB,KAAY,EACZ,YAA0B;QAD1B,UAAK,GAAL,KAAK,CAAO;QACZ,iBAAY,GAAZ,YAAY,CAAc;IAC5C,CAAC;IAEJ;;;OAGG;IACI,KAAK,CAAC,kBAAkB,CAK7B,QAA2C,EAC3C,MAAe,EACf,YAAyC;QAEzC,iDAAiD;QACjD,mCAAmC;QACnC,wCAAwC;QACxC,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,SAAS,CAAC,UAAU,EAAE;YAC9D,MAAM;YACN,QAAQ;SACT,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,UAAU,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAErE,IAAI,KAAK,EAAE,KAAK,CAAC;QACjB,IAAI,CAAC;YACH,IAAI,QAAQ,CAAC,IAAI,EAAE,CAAC;gBAClB,KAAK,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;YACpD,CAAC;YAED,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE;gBACtD,MAAM;gBACN,KAAK;aACN,CAAC,CAAC;YACH,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,SAAS,CAAC,SAAS,EAAE;gBAC7D,MAAM;gBACN,QAAQ;gBACR,KAAK;aACN,CAAC,CAAC;YAEH,OAAO,KAAK,CAAC;QACf,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,KAAK,GAAG,CAAC,CAAC;YAEV,mFAAmF;YACnF,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;YACjE,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,SAAS,CAAC,OAAO,EAAE;gBAC3D,KAAK;gBACL,QAAQ;aACT,CAAC,CAAC;YAEH,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC;CACF;AA1DD,kDA0DC"}
|
package/dist/Store.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { DependencyMapType, DependencyValuesType, IEventDefinition, IResource, ITask, RegisterableItems, IMiddleware } from "./defs";
|
|
2
|
+
import { IDependentNode } from "./tools/findCircularDependencies";
|
|
3
|
+
import { EventManager } from "./EventManager";
|
|
4
|
+
export type ResourceStoreElementType<C = any, V = any, D extends DependencyMapType = {}> = {
|
|
5
|
+
resource: IResource<C, V, D>;
|
|
6
|
+
computedDependencies?: DependencyValuesType<D>;
|
|
7
|
+
config: C;
|
|
8
|
+
value: V;
|
|
9
|
+
isInitialized?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type TaskStoreElementType<Input = any, Output extends Promise<any> = any, D extends DependencyMapType = any> = {
|
|
12
|
+
task: ITask<Input, Output, D>;
|
|
13
|
+
computedDependencies: DependencyValuesType<D>;
|
|
14
|
+
isInitialized: boolean;
|
|
15
|
+
};
|
|
16
|
+
export type MiddlewareStoreElementType<TDeps extends DependencyMapType = any> = {
|
|
17
|
+
middleware: IMiddleware<TDeps>;
|
|
18
|
+
computedDependencies: DependencyValuesType<TDeps>;
|
|
19
|
+
};
|
|
20
|
+
export type EventStoreElementType = {
|
|
21
|
+
event: IEventDefinition;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* @internal This should be used for testing purposes only.
|
|
25
|
+
*/
|
|
26
|
+
export declare class Store {
|
|
27
|
+
protected readonly eventManager: EventManager;
|
|
28
|
+
root: ResourceStoreElementType;
|
|
29
|
+
tasks: Map<string, TaskStoreElementType>;
|
|
30
|
+
resources: Map<string, ResourceStoreElementType>;
|
|
31
|
+
events: Map<string, EventStoreElementType>;
|
|
32
|
+
middlewares: Map<string, MiddlewareStoreElementType>;
|
|
33
|
+
constructor(eventManager: EventManager);
|
|
34
|
+
/**
|
|
35
|
+
* Store the root before beginning registration
|
|
36
|
+
* @param root
|
|
37
|
+
* @param config
|
|
38
|
+
*/
|
|
39
|
+
initializeStore(root: IResource<any>, config: any): void;
|
|
40
|
+
/**
|
|
41
|
+
* Beginning with the root, we perform registering to the container all the resources, tasks, middleware and events.
|
|
42
|
+
* @param element
|
|
43
|
+
* @param config
|
|
44
|
+
*/
|
|
45
|
+
computeRegisterOfResource<C>(element: IResource<C>, config?: C): void;
|
|
46
|
+
/**
|
|
47
|
+
* middlewares are already stored in their final form and the check for them would be redundant
|
|
48
|
+
* @param id
|
|
49
|
+
*/
|
|
50
|
+
protected checkIfIDExists(id: string): void | never;
|
|
51
|
+
getGlobalMiddlewares(excludingIds: string[]): IMiddleware[];
|
|
52
|
+
/**
|
|
53
|
+
* If you want to register something to the store you can use this function.
|
|
54
|
+
* @param item
|
|
55
|
+
*/
|
|
56
|
+
storeGenericItem<C>(item: RegisterableItems): void;
|
|
57
|
+
storeEvent<C>(item: IEventDefinition<void>): void;
|
|
58
|
+
private storeResourceWithConfig;
|
|
59
|
+
private storeResource;
|
|
60
|
+
private storeTask;
|
|
61
|
+
getDependentNodes(): IDependentNode[];
|
|
62
|
+
}
|
package/dist/Store.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
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
|
+
* @internal This should be used for testing purposes only.
|
|
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
|
+
constructor(eventManager) {
|
|
20
|
+
this.eventManager = eventManager;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Store the root before beginning registration
|
|
24
|
+
* @param root
|
|
25
|
+
* @param config
|
|
26
|
+
*/
|
|
27
|
+
initializeStore(root, config) {
|
|
28
|
+
this.storeGenericItem(globalResources_1.globalResources.eventManager.with(this.eventManager));
|
|
29
|
+
this.storeGenericItem(globalResources_1.globalResources.store.with(this));
|
|
30
|
+
root.dependencies =
|
|
31
|
+
typeof root.dependencies === "function"
|
|
32
|
+
? root.dependencies(config)
|
|
33
|
+
: root.dependencies;
|
|
34
|
+
this.root = {
|
|
35
|
+
resource: root,
|
|
36
|
+
computedDependencies: {},
|
|
37
|
+
config,
|
|
38
|
+
value: undefined,
|
|
39
|
+
isInitialized: false,
|
|
40
|
+
};
|
|
41
|
+
// register global events
|
|
42
|
+
globalEvents_1.globalEventsArray.forEach((event) => {
|
|
43
|
+
this.storeEvent(event);
|
|
44
|
+
});
|
|
45
|
+
this.resources.set(root.id, this.root);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Beginning with the root, we perform registering to the container all the resources, tasks, middleware and events.
|
|
49
|
+
* @param element
|
|
50
|
+
* @param config
|
|
51
|
+
*/
|
|
52
|
+
computeRegisterOfResource(element, config) {
|
|
53
|
+
const items = typeof element.register === "function"
|
|
54
|
+
? element.register(config)
|
|
55
|
+
: element.register;
|
|
56
|
+
for (const item of items) {
|
|
57
|
+
this.storeGenericItem(item);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* middlewares are already stored in their final form and the check for them would be redundant
|
|
62
|
+
* @param id
|
|
63
|
+
*/
|
|
64
|
+
checkIfIDExists(id) {
|
|
65
|
+
if (this.tasks.has(id)) {
|
|
66
|
+
throw errors_1.Errors.duplicateRegistration("Task", id);
|
|
67
|
+
}
|
|
68
|
+
if (this.resources.has(id)) {
|
|
69
|
+
throw errors_1.Errors.duplicateRegistration("Resource", id);
|
|
70
|
+
}
|
|
71
|
+
if (this.events.has(id)) {
|
|
72
|
+
throw errors_1.Errors.duplicateRegistration("Event", id);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
getGlobalMiddlewares(excludingIds) {
|
|
76
|
+
return Array.from(this.middlewares.values())
|
|
77
|
+
.filter((x) => x.middleware[defs_1.symbols.middlewareGlobal])
|
|
78
|
+
.filter((x) => !excludingIds.includes(x.middleware.id))
|
|
79
|
+
.map((x) => x.middleware);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* If you want to register something to the store you can use this function.
|
|
83
|
+
* @param item
|
|
84
|
+
*/
|
|
85
|
+
storeGenericItem(item) {
|
|
86
|
+
if (utils.isTask(item)) {
|
|
87
|
+
this.storeTask(item);
|
|
88
|
+
}
|
|
89
|
+
else if (utils.isResource(item)) {
|
|
90
|
+
// Registration a simple resource, which is interpreted as a resource with no configuration.
|
|
91
|
+
this.storeResource(item);
|
|
92
|
+
}
|
|
93
|
+
else if (utils.isEvent(item)) {
|
|
94
|
+
this.storeEvent(item);
|
|
95
|
+
}
|
|
96
|
+
else if (utils.isMiddleware(item)) {
|
|
97
|
+
if (this.middlewares.has(item.id)) {
|
|
98
|
+
throw errors_1.Errors.duplicateRegistration("Middleware", item.id);
|
|
99
|
+
}
|
|
100
|
+
item.dependencies =
|
|
101
|
+
typeof item.dependencies === "function"
|
|
102
|
+
? item.dependencies()
|
|
103
|
+
: item.dependencies;
|
|
104
|
+
this.middlewares.set(item.id, {
|
|
105
|
+
middleware: item,
|
|
106
|
+
computedDependencies: {},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else if (utils.isResourceWithConfig(item)) {
|
|
110
|
+
this.storeResourceWithConfig(item);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
throw errors_1.Errors.unknownItemType(item);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
storeEvent(item) {
|
|
117
|
+
this.checkIfIDExists(item.id);
|
|
118
|
+
this.events.set(item.id, { event: item });
|
|
119
|
+
}
|
|
120
|
+
storeResourceWithConfig(item) {
|
|
121
|
+
this.checkIfIDExists(item.resource.id);
|
|
122
|
+
this.resources.set(item.resource.id, {
|
|
123
|
+
resource: item.resource,
|
|
124
|
+
config: item.config,
|
|
125
|
+
value: undefined,
|
|
126
|
+
isInitialized: false,
|
|
127
|
+
});
|
|
128
|
+
this.computeRegisterOfResource(item.resource, item.config);
|
|
129
|
+
}
|
|
130
|
+
storeResource(item) {
|
|
131
|
+
this.checkIfIDExists(item.id);
|
|
132
|
+
this.storeEvent(item.events.beforeInit);
|
|
133
|
+
this.storeEvent(item.events.afterInit);
|
|
134
|
+
this.storeEvent(item.events.onError);
|
|
135
|
+
item.dependencies =
|
|
136
|
+
typeof item.dependencies === "function"
|
|
137
|
+
? item.dependencies()
|
|
138
|
+
: item.dependencies;
|
|
139
|
+
this.resources.set(item.id, {
|
|
140
|
+
resource: item,
|
|
141
|
+
config: {},
|
|
142
|
+
value: undefined,
|
|
143
|
+
isInitialized: false,
|
|
144
|
+
});
|
|
145
|
+
this.computeRegisterOfResource(item, {});
|
|
146
|
+
}
|
|
147
|
+
storeTask(item) {
|
|
148
|
+
this.checkIfIDExists(item.id);
|
|
149
|
+
item.dependencies =
|
|
150
|
+
typeof item.dependencies === "function"
|
|
151
|
+
? item.dependencies()
|
|
152
|
+
: item.dependencies;
|
|
153
|
+
this.storeEvent(item.events.beforeRun);
|
|
154
|
+
this.storeEvent(item.events.afterRun);
|
|
155
|
+
this.storeEvent(item.events.onError);
|
|
156
|
+
this.tasks.set(item.id, {
|
|
157
|
+
task: item,
|
|
158
|
+
computedDependencies: {},
|
|
159
|
+
isInitialized: false,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
getDependentNodes() {
|
|
163
|
+
const depenedants = [];
|
|
164
|
+
for (const task of this.tasks.values()) {
|
|
165
|
+
depenedants.push({
|
|
166
|
+
id: task.task.id,
|
|
167
|
+
dependencies: task.task.dependencies,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
for (const middleware of this.middlewares.values()) {
|
|
171
|
+
depenedants.push({
|
|
172
|
+
id: middleware.middleware.id,
|
|
173
|
+
dependencies: middleware.middleware.dependencies,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
for (const resource of this.resources.values()) {
|
|
177
|
+
depenedants.push({
|
|
178
|
+
id: resource.resource.id,
|
|
179
|
+
dependencies: resource.resource.dependencies || {},
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return depenedants;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.Store = Store;
|
|
186
|
+
//# sourceMappingURL=Store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Store.js","sourceRoot":"","sources":["../src/Store.ts"],"names":[],"mappings":";;;AAAA,iCAWgB;AAChB,kCAAkC;AAElC,iDAAmD;AACnD,qCAAkC;AAClC,uDAAoD;AAoCpD;;GAEG;AACH,MAAa,KAAK;IAOe;IAN/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;IAExE,YAA+B,YAA0B;QAA1B,iBAAY,GAAZ,YAAY,CAAc;IAAG,CAAC;IAE7D;;;;OAIG;IACH,eAAe,CAAC,IAAoB,EAAE,MAAW;QAC/C,IAAI,CAAC,gBAAgB,CAAC,iCAAe,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;QAC5E,IAAI,CAAC,gBAAgB,CAAC,iCAAe,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAExD,IAAI,CAAC,YAAY;YACf,OAAO,IAAI,CAAC,YAAY,KAAK,UAAU;gBACrC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;gBAC3B,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC;QAExB,IAAI,CAAC,IAAI,GAAG;YACV,QAAQ,EAAE,IAAI;YACd,oBAAoB,EAAE,EAAE;YACxB,MAAM;YACN,KAAK,EAAE,SAAS;YAChB,aA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