@bluelibs/runner 1.0.0 → 1.1.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/README.md +42 -1
- package/dist/EventManager.d.ts +4 -0
- package/dist/EventManager.js +15 -0
- package/dist/EventManager.js.map +1 -1
- package/dist/Store.d.ts +7 -2
- package/dist/Store.js +32 -1
- package/dist/Store.js.map +1 -1
- package/dist/define.js +1 -7
- package/dist/define.js.map +1 -1
- package/dist/defs.d.ts +9 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +2 -0
- package/dist/errors.js.map +1 -1
- package/dist/globalResources.d.ts +3 -3
- package/dist/globalResources.js +5 -4
- package/dist/globalResources.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/run.d.ts +1 -1
- package/dist/run.js +2 -1
- package/dist/run.js.map +1 -1
- package/package.json +1 -2
- package/src/EventManager.ts +20 -0
- package/src/Store.ts +44 -2
- package/src/__tests__/EventManager.test.ts +18 -0
- package/src/__tests__/Store.test.ts +81 -96
- package/src/__tests__/run.test.ts +28 -1
- package/src/define.ts +1 -8
- package/src/defs.ts +13 -0
- package/src/errors.ts +6 -0
- package/src/globalResources.ts +10 -8
- package/src/run.ts +4 -2
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ const app = resource({
|
|
|
77
77
|
});
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
### When to use
|
|
80
|
+
### When to use each?
|
|
81
81
|
|
|
82
82
|
It is unrealistic to create a task for everything you're doing in your system, not only it will be tedious for the developer, but it will affect performance unnecessarily. The idea is to think of a task of something that you want trackable as an action, for example:
|
|
83
83
|
|
|
@@ -87,6 +87,47 @@ It is unrealistic to create a task for everything you're doing in your system, n
|
|
|
87
87
|
|
|
88
88
|
Resources are more like services, they are singletons, they are meant to be used as a shared functionality across your application. They can be constants, services, functions, etc.
|
|
89
89
|
|
|
90
|
+
### Resource dispose()
|
|
91
|
+
|
|
92
|
+
Resources can have a `dispose()` method that can be used to clean up resources. This is useful for cleaning up resources like closing database connections, etc. You typically want to use this when you have opened pending connections or you need to do some cleanup or a graceful shutdown.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { task, run, resource } from "@bluelibs/runner";
|
|
96
|
+
|
|
97
|
+
const dbResource = resource({
|
|
98
|
+
async init(config, deps) {
|
|
99
|
+
const db = await connectToDatabase();
|
|
100
|
+
return db;
|
|
101
|
+
},
|
|
102
|
+
async dispose(db, config, deps) {
|
|
103
|
+
return db.close();
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
If you want to call dispose, you have to do it through the global store.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { task, run, resource, global } from "@bluelibs/runner";
|
|
112
|
+
|
|
113
|
+
const app = resource({
|
|
114
|
+
id: "app",
|
|
115
|
+
register: [dbResource],
|
|
116
|
+
dependencies: {
|
|
117
|
+
store: global.resources.store,
|
|
118
|
+
},
|
|
119
|
+
async init(_, deps) {
|
|
120
|
+
return {
|
|
121
|
+
dispose: async () => deps.store.dispose(),
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const value = await run(app);
|
|
127
|
+
// To begin the disposal process.
|
|
128
|
+
await value.dispose();
|
|
129
|
+
```
|
|
130
|
+
|
|
90
131
|
## Encapsulation
|
|
91
132
|
|
|
92
133
|
We want to make sure that our tasks are not dependent on the outside world. This is why we have the `dependencies` object.
|
package/dist/EventManager.d.ts
CHANGED
|
@@ -4,8 +4,12 @@ export interface IEventHandlerOptions<T = any> {
|
|
|
4
4
|
filter?: (event: IEvent<T>) => boolean;
|
|
5
5
|
}
|
|
6
6
|
export declare class EventManager {
|
|
7
|
+
#private;
|
|
7
8
|
private listeners;
|
|
8
9
|
private globalListeners;
|
|
10
|
+
get isLocked(): boolean;
|
|
11
|
+
lock(): void;
|
|
12
|
+
checkLock(): void;
|
|
9
13
|
emit<TInput>(eventDefinition: IEventDefinition<TInput>, ...args: TInput extends void ? [] : [TInput]): Promise<void>;
|
|
10
14
|
addListener<T>(event: IEventDefinition | Array<IEventDefinition>, handler: EventHandlerType<T>, options?: IEventHandlerOptions<T>): void;
|
|
11
15
|
addGlobalListener(handler: EventHandlerType, options?: IEventHandlerOptions): void;
|
package/dist/EventManager.js
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventManager = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
4
5
|
const HandlerOptionsDefaults = { order: 0 };
|
|
5
6
|
class EventManager {
|
|
6
7
|
listeners = new Map();
|
|
7
8
|
globalListeners = [];
|
|
9
|
+
#isLocked = false;
|
|
10
|
+
get isLocked() {
|
|
11
|
+
return this.#isLocked;
|
|
12
|
+
}
|
|
13
|
+
lock() {
|
|
14
|
+
this.#isLocked = true;
|
|
15
|
+
}
|
|
16
|
+
checkLock() {
|
|
17
|
+
if (this.#isLocked) {
|
|
18
|
+
throw errors_1.Errors.locked("EventManager");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
8
21
|
async emit(eventDefinition, ...args) {
|
|
9
22
|
const data = args[0];
|
|
10
23
|
const eventListeners = this.listeners.get(eventDefinition.id) || [];
|
|
@@ -24,6 +37,7 @@ class EventManager {
|
|
|
24
37
|
}
|
|
25
38
|
}
|
|
26
39
|
addListener(event, handler, options = HandlerOptionsDefaults) {
|
|
40
|
+
this.checkLock();
|
|
27
41
|
if (Array.isArray(event)) {
|
|
28
42
|
event.forEach((id) => this.addListener(id, handler, options));
|
|
29
43
|
}
|
|
@@ -40,6 +54,7 @@ class EventManager {
|
|
|
40
54
|
}
|
|
41
55
|
}
|
|
42
56
|
addGlobalListener(handler, options = HandlerOptionsDefaults) {
|
|
57
|
+
this.checkLock();
|
|
43
58
|
const newListener = {
|
|
44
59
|
handler,
|
|
45
60
|
order: options.order || 0,
|
package/dist/EventManager.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EventManager.js","sourceRoot":"","sources":["../src/EventManager.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"EventManager.js","sourceRoot":"","sources":["../src/EventManager.ts"],"names":[],"mappings":";;;AACA,qCAAkC;AAElC,MAAM,sBAAsB,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;AAa5C,MAAa,YAAY;IACf,SAAS,GAAoC,IAAI,GAAG,EAAE,CAAC;IACvD,eAAe,GAAuB,EAAE,CAAC;IACjD,SAAS,GAAG,KAAK,CAAC;IAElB,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI;QACF,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IACxB,CAAC;IAED,SAAS;QACP,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACnB,MAAM,eAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED,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,EAA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aAAa,CAAC,SAA6B;QACjD,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC;CACF;AAvFD,oCAuFC"}
|
package/dist/Store.d.ts
CHANGED
|
@@ -21,9 +21,10 @@ export type EventStoreElementType = {
|
|
|
21
21
|
event: IEventDefinition;
|
|
22
22
|
};
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
24
|
+
* Store class which is used to store all the resources, tasks, middleware and events.
|
|
25
25
|
*/
|
|
26
26
|
export declare class Store {
|
|
27
|
+
#private;
|
|
27
28
|
protected readonly eventManager: EventManager;
|
|
28
29
|
root: ResourceStoreElementType;
|
|
29
30
|
tasks: Map<string, TaskStoreElementType>;
|
|
@@ -31,6 +32,9 @@ export declare class Store {
|
|
|
31
32
|
events: Map<string, EventStoreElementType>;
|
|
32
33
|
middlewares: Map<string, MiddlewareStoreElementType>;
|
|
33
34
|
constructor(eventManager: EventManager);
|
|
35
|
+
get isLocked(): boolean;
|
|
36
|
+
lock(): void;
|
|
37
|
+
checkLock(): void;
|
|
34
38
|
/**
|
|
35
39
|
* Store the root before beginning registration
|
|
36
40
|
* @param root
|
|
@@ -42,12 +46,13 @@ export declare class Store {
|
|
|
42
46
|
* @param element
|
|
43
47
|
* @param config
|
|
44
48
|
*/
|
|
45
|
-
computeRegisterOfResource
|
|
49
|
+
private computeRegisterOfResource;
|
|
46
50
|
/**
|
|
47
51
|
* middlewares are already stored in their final form and the check for them would be redundant
|
|
48
52
|
* @param id
|
|
49
53
|
*/
|
|
50
54
|
protected checkIfIDExists(id: string): void | never;
|
|
55
|
+
dispose(): Promise<void>;
|
|
51
56
|
getGlobalMiddlewares(excludingIds: string[]): IMiddleware[];
|
|
52
57
|
/**
|
|
53
58
|
* If you want to register something to the store you can use this function.
|
package/dist/Store.js
CHANGED
|
@@ -7,7 +7,7 @@ const globalEvents_1 = require("./globalEvents");
|
|
|
7
7
|
const errors_1 = require("./errors");
|
|
8
8
|
const globalResources_1 = require("./globalResources");
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Store class which is used to store all the resources, tasks, middleware and events.
|
|
11
11
|
*/
|
|
12
12
|
class Store {
|
|
13
13
|
eventManager;
|
|
@@ -16,15 +16,37 @@ class Store {
|
|
|
16
16
|
resources = new Map();
|
|
17
17
|
events = new Map();
|
|
18
18
|
middlewares = new Map();
|
|
19
|
+
#isLocked = false;
|
|
20
|
+
#isInitialized = false;
|
|
19
21
|
constructor(eventManager) {
|
|
20
22
|
this.eventManager = eventManager;
|
|
21
23
|
}
|
|
24
|
+
get isLocked() {
|
|
25
|
+
return this.#isLocked;
|
|
26
|
+
}
|
|
27
|
+
lock() {
|
|
28
|
+
this.#isLocked = true;
|
|
29
|
+
this.eventManager.lock();
|
|
30
|
+
// freeze the maps so they can't be modified
|
|
31
|
+
Object.freeze(this.tasks);
|
|
32
|
+
Object.freeze(this.resources);
|
|
33
|
+
Object.freeze(this.events);
|
|
34
|
+
Object.freeze(this.middlewares);
|
|
35
|
+
}
|
|
36
|
+
checkLock() {
|
|
37
|
+
if (this.#isLocked) {
|
|
38
|
+
throw new Error("Cannot modify the Store when it is locked.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
22
41
|
/**
|
|
23
42
|
* Store the root before beginning registration
|
|
24
43
|
* @param root
|
|
25
44
|
* @param config
|
|
26
45
|
*/
|
|
27
46
|
initializeStore(root, config) {
|
|
47
|
+
if (this.#isInitialized) {
|
|
48
|
+
throw errors_1.Errors.storeAlreadyInitialized();
|
|
49
|
+
}
|
|
28
50
|
this.storeGenericItem(globalResources_1.globalResources.eventManager.with(this.eventManager));
|
|
29
51
|
this.storeGenericItem(globalResources_1.globalResources.store.with(this));
|
|
30
52
|
root.dependencies =
|
|
@@ -42,7 +64,9 @@ class Store {
|
|
|
42
64
|
globalEvents_1.globalEventsArray.forEach((event) => {
|
|
43
65
|
this.storeEvent(event);
|
|
44
66
|
});
|
|
67
|
+
this.computeRegisterOfResource(root, config);
|
|
45
68
|
this.resources.set(root.id, this.root);
|
|
69
|
+
this.#isInitialized = true;
|
|
46
70
|
}
|
|
47
71
|
/**
|
|
48
72
|
* Beginning with the root, we perform registering to the container all the resources, tasks, middleware and events.
|
|
@@ -72,6 +96,13 @@ class Store {
|
|
|
72
96
|
throw errors_1.Errors.duplicateRegistration("Event", id);
|
|
73
97
|
}
|
|
74
98
|
}
|
|
99
|
+
async dispose() {
|
|
100
|
+
for (const resource of this.resources.values()) {
|
|
101
|
+
if (resource.resource.dispose) {
|
|
102
|
+
await resource.resource.dispose(resource.value, resource.config, resource.computedDependencies);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
75
106
|
getGlobalMiddlewares(excludingIds) {
|
|
76
107
|
return Array.from(this.middlewares.values())
|
|
77
108
|
.filter((x) => x.middleware[defs_1.symbols.middlewareGlobal])
|
package/dist/Store.js.map
CHANGED
|
@@ -1 +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;
|
|
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;IASe;IAR/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;IACxE,SAAS,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errors");
|
|
14
14
|
function defineTask(config) {
|
|
15
|
-
const autorun = {};
|
|
16
|
-
// if (config.autorun) {
|
|
17
|
-
// if (config.autorun.on) {
|
|
18
|
-
// autorun.on = Array.isArray(config.autorun.on);
|
|
19
|
-
// autorun.schedule = config.autorun.schedule;
|
|
20
|
-
// }
|
|
21
|
-
// }
|
|
22
15
|
return {
|
|
23
16
|
[defs_1.symbols.task]: true,
|
|
24
17
|
id: config.id,
|
|
@@ -46,6 +39,7 @@ function defineResource(constConfig) {
|
|
|
46
39
|
id: constConfig.id,
|
|
47
40
|
dependencies: constConfig.dependencies,
|
|
48
41
|
hooks: constConfig.hooks || [],
|
|
42
|
+
dispose: constConfig.dispose,
|
|
49
43
|
register: constConfig.register || [],
|
|
50
44
|
init: constConfig.init,
|
|
51
45
|
with: function (config) {
|
package/dist/define.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":";;AAeA,
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":";;AAeA,gCA4BC;AAED,wCAmCC;AAED,kCAOC;AAED,4CAqBC;AAED,wBAEC;AAED,gCAEC;AAED,oDAIC;AAED,0BAEC;AAED,oCAEC;AAtID,iCAYgB;AAChB,qCAAkC;AAElC,SAAgB,UAAU,CAMxB,MAAkD;IAElD,OAAO;QACL,CAAC,cAAO,CAAC,IAAI,CAAC,EAAE,IAAI;QACpB,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,YAAY,EAAE,MAAM,CAAC,YAAY,IAAK,EAAW;QACjD,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,EAAE;QACnC,GAAG,EAAE,MAAM,CAAC,GAAG;QACf,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,MAAM,EAAE;YACN,SAAS,EAAE,WAAW,CAAC;gBACrB,EAAE,EAAE,GAAG,MAAM,CAAC,EAAE,YAAY;aAC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G;QACb,CAAC,cAAO,CAAC,UAAU,CAAC,EAAE,IAAI;QAC1B,GAAG,MAAM;QACT,YAAY,EAAE,MAAM,CAAC,YAAY,IAAK,EAAY;KACnD,CAAC;IAEF,OAAO;QACL,GAAG,MAAM;QACT,MAAM;YACJ,OAAO;gBACL,GAAG,MAAM;gBACT,CAAC,cAAO,CAAC,gBAAgB,CAAC,EAAE,IAAI;gBAChC,MAAM;oBACJ,MAAM,eAAM,CAAC,uBAAuB,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAClD,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAgB,MAAM,CAAC,UAAe;IACpC,OAAO,UAAU,IAAI,UAAU,CAAC,cAAO,CAAC,IAAI,CAAC,CAAC;AAChD,CAAC;AAED,SAAgB,UAAU,CAAC,UAAe;IACxC,OAAO,UAAU,IAAI,UAAU,CAAC,cAAO,CAAC,QAAQ,CAAC,CAAC;AACpD,CAAC;AAED,SAAgB,oBAAoB,CAClC,UAAe;IAEf,OAAO,UAAU,IAAI,UAAU,CAAC,cAAO,CAAC,kBAAkB,CAAC,CAAC;AAC9D,CAAC;AAED,SAAgB,OAAO,CAAC,UAAe;IACrC,OAAO,UAAU,IAAI,UAAU,CAAC,cAAO,CAAC,KAAK,CAAC,CAAC;AACjD,CAAC;AAED,SAAgB,YAAY,CAAC,UAAe;IAC1C,OAAO,UAAU,IAAI,UAAU,CAAC,cAAO,CAAC,UAAU,CAAC,CAAC;AACtD,CAAC"}
|
package/dist/defs.d.ts
CHANGED
|
@@ -74,6 +74,15 @@ export interface IResourceDefinintion<TConfig = void, TValue = unknown, TDepende
|
|
|
74
74
|
hooks?: IHookDefinition<TDependencies>[] | ((config: TConfig) => IHookDefinition<TDependencies>[]);
|
|
75
75
|
register?: Array<RegisterableItems> | ((config: TConfig) => Array<RegisterableItems>);
|
|
76
76
|
init?: (config: TConfig, dependencies: DependencyValuesType<TDependencies>) => Promise<TValue>;
|
|
77
|
+
/**
|
|
78
|
+
* Clean-up function for the resource. This is called when the resource is no longer needed.
|
|
79
|
+
*
|
|
80
|
+
* @param value The value of the resource
|
|
81
|
+
* @param config The configuration it received
|
|
82
|
+
* @param dependencies The dependencies it needed
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
dispose?: (value: TValue, config: TConfig, dependencies: DependencyValuesType<TDependencies>) => Promise<TValue>;
|
|
77
86
|
meta?: IResourceMeta;
|
|
78
87
|
}
|
|
79
88
|
export interface IResource<TConfig = void, TValue = any, TDependencies extends DependencyMapType = any> extends IResourceDefinintion<TConfig, TValue, TDependencies> {
|
package/dist/errors.d.ts
CHANGED
package/dist/errors.js
CHANGED
|
@@ -8,5 +8,7 @@ exports.Errors = {
|
|
|
8
8
|
circularDependencies: (cycles) => new Error(`Circular dependencies detected: ${cycles.join(", ")}`),
|
|
9
9
|
eventNotFound: (id) => new Error(`Event "${id}" not found. Did you forget to register it?`),
|
|
10
10
|
middlewareAlreadyGlobal: (id) => new Error("Cannot call global on a global middleware: " + id),
|
|
11
|
+
locked: (what) => new Error(`Cannot modify the ${what} when it is locked.`),
|
|
12
|
+
storeAlreadyInitialized: () => new Error("Store already initialized. Cannot reinitialize."),
|
|
11
13
|
};
|
|
12
14
|
//# sourceMappingURL=errors.js.map
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAEa,QAAA,MAAM,GAAG;IACpB,qBAAqB,EAAE,CAAC,IAAY,EAAE,EAAU,EAAE,EAAE,CAClD,IAAI,KAAK,CAAC,GAAG,IAAI,KAAK,EAAE,sBAAsB,CAAC;IAEjD,kBAAkB,EAAE,CAAC,GAAW,EAAE,EAAE,CAClC,IAAI,KAAK,CACP,cAAc,GAAG,+DAA+D,CACjF;IAEH,eAAe,EAAE,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,sBAA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C;CAC/D,CAAC"}
|
|
@@ -2,7 +2,7 @@ import { EventManager } from "./EventManager";
|
|
|
2
2
|
import { Store } from "./Store";
|
|
3
3
|
import { TaskRunner } from "./TaskRunner";
|
|
4
4
|
export declare const globalResources: {
|
|
5
|
-
store: import("./defs").IResource<Store,
|
|
6
|
-
eventManager: import("./defs").IResource<EventManager,
|
|
7
|
-
taskRunner: import("./defs").IResource<TaskRunner,
|
|
5
|
+
store: import("./defs").IResource<Store, Store, {}>;
|
|
6
|
+
eventManager: import("./defs").IResource<EventManager, EventManager, {}>;
|
|
7
|
+
taskRunner: import("./defs").IResource<TaskRunner, TaskRunner, {}>;
|
|
8
8
|
};
|
package/dist/globalResources.js
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.globalResources = void 0;
|
|
4
4
|
const define_1 = require("./define");
|
|
5
|
+
const store = (0, define_1.defineResource)({
|
|
6
|
+
id: "global.store",
|
|
7
|
+
init: async (store) => store,
|
|
8
|
+
});
|
|
5
9
|
exports.globalResources = {
|
|
6
|
-
store
|
|
7
|
-
id: "global.store",
|
|
8
|
-
init: async (store) => store,
|
|
9
|
-
}),
|
|
10
|
+
store,
|
|
10
11
|
eventManager: (0, define_1.defineResource)({
|
|
11
12
|
id: "global.eventManager",
|
|
12
13
|
init: async (em) => em,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"globalResources.js","sourceRoot":"","sources":["../src/globalResources.ts"],"names":[],"mappings":";;;AAAA,qCAA0C;
|
|
1
|
+
{"version":3,"file":"globalResources.js","sourceRoot":"","sources":["../src/globalResources.ts"],"names":[],"mappings":";;;AAAA,qCAA0C;AAK1C,MAAM,KAAK,GAAG,IAAA,uBAAc,EAAC;IAC3B,EAAE,EAAE,cAAc;IAClB,IAAI,EAAE,KAAK,EAAE,KAAY,EAAE,EAAE,CAAC,KAAK;CACpC,CAAC,CAAC;AAEU,QAAA,eAAe,GAAG;IAC7B,KAAK;IACL,YAAY,EAAE,IAAA,uBAAc,EAAC;QAC3B,EAAE,EAAE,qBAAqB;QACzB,IAAI,EAAE,KAAK,EAAE,EAAgB,EAAE,EAAE,CAAC,EAAE;KACrC,CAAC;IACF,UAAU,EAAE,IAAA,uBAAc,EAAC;QACzB,EAAE,EAAE,mBAAmB;QACvB,IAAI,EAAE,KAAK,EAAE,MAAkB,EAAE,EAAE,CAAC,MAAM;KAC3C,CAAC;CACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -36,9 +36,9 @@ declare const globals: {
|
|
|
36
36
|
};
|
|
37
37
|
};
|
|
38
38
|
resources: {
|
|
39
|
-
store: import("./defs").IResource<import("./Store").Store,
|
|
40
|
-
eventManager: import("./defs").IResource<import("./EventManager").EventManager,
|
|
41
|
-
taskRunner: import("./defs").IResource<import("./TaskRunner").TaskRunner,
|
|
39
|
+
store: import("./defs").IResource<import("./Store").Store, import("./Store").Store, {}>;
|
|
40
|
+
eventManager: import("./defs").IResource<import("./EventManager").EventManager, import("./EventManager").EventManager, {}>;
|
|
41
|
+
taskRunner: import("./defs").IResource<import("./TaskRunner").TaskRunner, import("./TaskRunner").TaskRunner, {}>;
|
|
42
42
|
};
|
|
43
43
|
};
|
|
44
44
|
export { globals };
|
package/dist/run.d.ts
CHANGED
package/dist/run.js
CHANGED
|
@@ -17,7 +17,6 @@ async function run(resource, config) {
|
|
|
17
17
|
// In the registration phase we register deeply all the resources, tasks, middleware and events
|
|
18
18
|
store.initializeStore(resource, config);
|
|
19
19
|
store.storeGenericItem(globalResources_1.globalResources.taskRunner.with(taskRunner));
|
|
20
|
-
store.computeRegisterOfResource(resource, config);
|
|
21
20
|
// We verify that there isn't any circular dependencies before we begin computing the dependencies
|
|
22
21
|
const dependentNodes = store.getDependentNodes();
|
|
23
22
|
const circularDependencies = (0, findCircularDependencies_1.findCircularDependencies)(dependentNodes);
|
|
@@ -34,6 +33,8 @@ async function run(resource, config) {
|
|
|
34
33
|
// Now we can initialise the root resource
|
|
35
34
|
await processor.initializeRoot();
|
|
36
35
|
await eventManager.emit(globalEvents_1.globalEvents.afterInit);
|
|
36
|
+
// disallow manipulation or attaching more
|
|
37
|
+
store.lock();
|
|
37
38
|
return store.root.value;
|
|
38
39
|
}
|
|
39
40
|
//# 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,
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":";;AA2DA,kBAwCC;AAnGD,6CAA0C;AAU1C,+DAA4D;AAC5D,iDAA8C;AAC9C,iDAA8C;AAC9C,mCAAgC;AAChC,+EAA4E;AAC5E,qCAAkC;AAClC,uDAAoD;AA2C7C,KAAK,UAAU,GAAG,CACvB,QAAyB,EACzB,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;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,SAAS,CAAC,YAAY,EAAE,CAAC;IAE/B,mFAAmF;IACnF,iCAAiC;IACjC,MAAM,YAAY,CAAC,IAAI,CAAC,2BAAY,CAAC,UAAU,CAAC,CAAC;IAEjD,MAAM,SAAS,CAAC,sBAAsB,EAAE,CAAC;IAEzC,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.
|
|
3
|
+
"version": "1.1.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"
|
package/src/EventManager.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EventHandlerType, IEvent, IEventDefinition } from "./defs";
|
|
2
|
+
import { Errors } from "./errors";
|
|
2
3
|
|
|
3
4
|
const HandlerOptionsDefaults = { order: 0 };
|
|
4
5
|
|
|
@@ -16,6 +17,21 @@ export interface IEventHandlerOptions<T = any> {
|
|
|
16
17
|
export class EventManager {
|
|
17
18
|
private listeners: Map<string, IListenerStorage[]> = new Map();
|
|
18
19
|
private globalListeners: IListenerStorage[] = [];
|
|
20
|
+
#isLocked = false;
|
|
21
|
+
|
|
22
|
+
get isLocked() {
|
|
23
|
+
return this.#isLocked;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
lock() {
|
|
27
|
+
this.#isLocked = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
checkLock() {
|
|
31
|
+
if (this.#isLocked) {
|
|
32
|
+
throw Errors.locked("EventManager");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
19
35
|
|
|
20
36
|
async emit<TInput>(
|
|
21
37
|
eventDefinition: IEventDefinition<TInput>,
|
|
@@ -46,6 +62,8 @@ export class EventManager {
|
|
|
46
62
|
handler: EventHandlerType<T>,
|
|
47
63
|
options: IEventHandlerOptions<T> = HandlerOptionsDefaults
|
|
48
64
|
): void {
|
|
65
|
+
this.checkLock();
|
|
66
|
+
|
|
49
67
|
if (Array.isArray(event)) {
|
|
50
68
|
event.forEach((id) => this.addListener(id, handler, options));
|
|
51
69
|
} else {
|
|
@@ -66,6 +84,8 @@ export class EventManager {
|
|
|
66
84
|
handler: EventHandlerType,
|
|
67
85
|
options: IEventHandlerOptions = HandlerOptionsDefaults
|
|
68
86
|
): void {
|
|
87
|
+
this.checkLock();
|
|
88
|
+
|
|
69
89
|
const newListener: IListenerStorage = {
|
|
70
90
|
handler,
|
|
71
91
|
order: options.order || 0,
|
package/src/Store.ts
CHANGED
|
@@ -51,7 +51,7 @@ export type EventStoreElementType = {
|
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
54
|
+
* Store class which is used to store all the resources, tasks, middleware and events.
|
|
55
55
|
*/
|
|
56
56
|
export class Store {
|
|
57
57
|
root!: ResourceStoreElementType;
|
|
@@ -59,15 +59,41 @@ export class Store {
|
|
|
59
59
|
public resources: Map<string, ResourceStoreElementType> = new Map();
|
|
60
60
|
public events: Map<string, EventStoreElementType> = new Map();
|
|
61
61
|
public middlewares: Map<string, MiddlewareStoreElementType> = new Map();
|
|
62
|
+
#isLocked = false;
|
|
63
|
+
#isInitialized = false;
|
|
62
64
|
|
|
63
65
|
constructor(protected readonly eventManager: EventManager) {}
|
|
64
66
|
|
|
67
|
+
get isLocked() {
|
|
68
|
+
return this.#isLocked;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
lock() {
|
|
72
|
+
this.#isLocked = true;
|
|
73
|
+
this.eventManager.lock();
|
|
74
|
+
// freeze the maps so they can't be modified
|
|
75
|
+
Object.freeze(this.tasks);
|
|
76
|
+
Object.freeze(this.resources);
|
|
77
|
+
Object.freeze(this.events);
|
|
78
|
+
Object.freeze(this.middlewares);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
checkLock() {
|
|
82
|
+
if (this.#isLocked) {
|
|
83
|
+
throw new Error("Cannot modify the Store when it is locked.");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
65
87
|
/**
|
|
66
88
|
* Store the root before beginning registration
|
|
67
89
|
* @param root
|
|
68
90
|
* @param config
|
|
69
91
|
*/
|
|
70
92
|
initializeStore(root: IResource<any>, config: any) {
|
|
93
|
+
if (this.#isInitialized) {
|
|
94
|
+
throw Errors.storeAlreadyInitialized();
|
|
95
|
+
}
|
|
96
|
+
|
|
71
97
|
this.storeGenericItem(globalResources.eventManager.with(this.eventManager));
|
|
72
98
|
this.storeGenericItem(globalResources.store.with(this));
|
|
73
99
|
|
|
@@ -89,7 +115,11 @@ export class Store {
|
|
|
89
115
|
this.storeEvent(event);
|
|
90
116
|
});
|
|
91
117
|
|
|
118
|
+
this.computeRegisterOfResource(root, config);
|
|
119
|
+
|
|
92
120
|
this.resources.set(root.id, this.root);
|
|
121
|
+
|
|
122
|
+
this.#isInitialized = true;
|
|
93
123
|
}
|
|
94
124
|
|
|
95
125
|
/**
|
|
@@ -97,7 +127,7 @@ export class Store {
|
|
|
97
127
|
* @param element
|
|
98
128
|
* @param config
|
|
99
129
|
*/
|
|
100
|
-
computeRegisterOfResource<C>(element: IResource<C>, config?: C) {
|
|
130
|
+
private computeRegisterOfResource<C>(element: IResource<C>, config?: C) {
|
|
101
131
|
const items =
|
|
102
132
|
typeof element.register === "function"
|
|
103
133
|
? element.register(config as C)
|
|
@@ -124,6 +154,18 @@ export class Store {
|
|
|
124
154
|
}
|
|
125
155
|
}
|
|
126
156
|
|
|
157
|
+
public async dispose() {
|
|
158
|
+
for (const resource of this.resources.values()) {
|
|
159
|
+
if (resource.resource.dispose) {
|
|
160
|
+
await resource.resource.dispose(
|
|
161
|
+
resource.value,
|
|
162
|
+
resource.config,
|
|
163
|
+
resource.computedDependencies as DependencyMapType
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
127
169
|
public getGlobalMiddlewares(excludingIds: string[]): IMiddleware[] {
|
|
128
170
|
return Array.from(this.middlewares.values())
|
|
129
171
|
.filter((x) => x.middleware[symbols.middlewareGlobal])
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IEvent, IEventDefinition } from "../defs";
|
|
2
|
+
import { Errors } from "../errors";
|
|
2
3
|
import { EventManager } from "../EventManager";
|
|
3
4
|
|
|
4
5
|
describe("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
|
});
|
|
@@ -1,143 +1,128 @@
|
|
|
1
|
-
import { EventManager } from "../EventManager";
|
|
2
1
|
import { Store } from "../Store";
|
|
2
|
+
import { EventManager } from "../EventManager";
|
|
3
3
|
import {
|
|
4
|
-
defineTask,
|
|
5
4
|
defineResource,
|
|
6
|
-
|
|
5
|
+
defineTask,
|
|
7
6
|
defineMiddleware,
|
|
7
|
+
defineEvent,
|
|
8
8
|
} from "../define";
|
|
9
|
+
import { globalResources } from "../globalResources";
|
|
9
10
|
|
|
10
11
|
describe("Store", () => {
|
|
11
|
-
let store: Store;
|
|
12
12
|
let eventManager: EventManager;
|
|
13
|
+
let store: Store;
|
|
13
14
|
|
|
14
15
|
beforeEach(() => {
|
|
15
16
|
eventManager = new EventManager();
|
|
16
17
|
store = new Store(eventManager);
|
|
17
18
|
});
|
|
18
19
|
|
|
19
|
-
it("should
|
|
20
|
-
const
|
|
21
|
-
id: "
|
|
22
|
-
|
|
20
|
+
it("should initialize the store with a root resource", () => {
|
|
21
|
+
const rootResource = defineResource({
|
|
22
|
+
id: "root",
|
|
23
|
+
init: async () => "Root Value",
|
|
23
24
|
});
|
|
24
25
|
|
|
25
|
-
store.
|
|
26
|
-
defineResource({
|
|
27
|
-
id: "root",
|
|
28
|
-
register: [task],
|
|
29
|
-
})
|
|
30
|
-
);
|
|
26
|
+
store.initializeStore(rootResource, {});
|
|
31
27
|
|
|
32
|
-
expect(store.
|
|
33
|
-
expect(store.
|
|
28
|
+
expect(store.root.resource).toBe(rootResource);
|
|
29
|
+
expect(store.resources.has("root")).toBe(true);
|
|
34
30
|
});
|
|
35
31
|
|
|
36
|
-
it("should
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
});
|
|
32
|
+
it("should lock the store and prevent modifications", () => {
|
|
33
|
+
store.lock();
|
|
34
|
+
expect(store.isLocked).toBe(true);
|
|
40
35
|
|
|
41
|
-
store.
|
|
42
|
-
|
|
43
|
-
id: "root",
|
|
44
|
-
register: [resource],
|
|
45
|
-
})
|
|
36
|
+
expect(() => store.checkLock()).toThrow(
|
|
37
|
+
"Cannot modify the Store when it is locked."
|
|
46
38
|
);
|
|
47
|
-
|
|
48
|
-
expect(store.resources.get("testResource")).toBeDefined();
|
|
49
|
-
expect(store.resources.get("testResource")?.resource).toBe(resource);
|
|
50
39
|
});
|
|
51
40
|
|
|
52
|
-
it("should
|
|
53
|
-
const
|
|
54
|
-
id: "
|
|
41
|
+
it("should store a task and retrieve it", () => {
|
|
42
|
+
const testTask = defineTask({
|
|
43
|
+
id: "test.task",
|
|
44
|
+
run: async () => "Task executed",
|
|
55
45
|
});
|
|
56
46
|
|
|
57
|
-
store.
|
|
58
|
-
defineResource({
|
|
59
|
-
id: "root",
|
|
60
|
-
register: [event],
|
|
61
|
-
})
|
|
62
|
-
);
|
|
47
|
+
store.storeGenericItem(testTask);
|
|
63
48
|
|
|
64
|
-
expect(store.
|
|
65
|
-
expect(store.events.get("testEvent")?.event).toBe(event);
|
|
49
|
+
expect(store.tasks.has("test.task")).toBe(true);
|
|
66
50
|
});
|
|
67
51
|
|
|
68
|
-
it("should
|
|
69
|
-
const
|
|
70
|
-
id: "
|
|
71
|
-
|
|
52
|
+
it("should store a resource and retrieve it", () => {
|
|
53
|
+
const testResource = defineResource({
|
|
54
|
+
id: "test.resource",
|
|
55
|
+
init: async () => "Resource Value",
|
|
72
56
|
});
|
|
73
57
|
|
|
74
|
-
store.
|
|
75
|
-
defineResource({
|
|
76
|
-
id: "root",
|
|
77
|
-
register: [middleware],
|
|
78
|
-
})
|
|
79
|
-
);
|
|
58
|
+
store.storeGenericItem(testResource);
|
|
80
59
|
|
|
81
|
-
expect(store.
|
|
82
|
-
expect(store.middlewares.get("testMiddleware")?.middleware).toBe(
|
|
83
|
-
middleware
|
|
84
|
-
);
|
|
60
|
+
expect(store.resources.has("test.resource")).toBe(true);
|
|
85
61
|
});
|
|
86
62
|
|
|
87
|
-
it("should
|
|
88
|
-
const
|
|
89
|
-
id: "
|
|
90
|
-
run: async () => {
|
|
63
|
+
it("should store a middleware and retrieve it", () => {
|
|
64
|
+
const testMiddleware = defineMiddleware({
|
|
65
|
+
id: "test.middleware",
|
|
66
|
+
run: async ({ next }) => {
|
|
67
|
+
return `Middleware: ${await next()}`;
|
|
68
|
+
},
|
|
91
69
|
});
|
|
92
70
|
|
|
93
|
-
store.
|
|
94
|
-
defineResource({
|
|
95
|
-
id: "root",
|
|
96
|
-
register: [task],
|
|
97
|
-
})
|
|
98
|
-
);
|
|
71
|
+
store.storeGenericItem(testMiddleware);
|
|
99
72
|
|
|
100
|
-
expect(()
|
|
101
|
-
store.computeRegisterOfResource(
|
|
102
|
-
defineResource({
|
|
103
|
-
id: "anotherRoot",
|
|
104
|
-
register: [task],
|
|
105
|
-
})
|
|
106
|
-
);
|
|
107
|
-
}).toThrow('Task "duplicateItem" already registered');
|
|
73
|
+
expect(store.middlewares.has("test.middleware")).toBe(true);
|
|
108
74
|
});
|
|
109
75
|
|
|
110
|
-
it("should
|
|
111
|
-
const
|
|
112
|
-
id: "task1",
|
|
113
|
-
dependencies: { dep1: {} as any },
|
|
114
|
-
run: async () => {},
|
|
115
|
-
});
|
|
76
|
+
it("should store an event and retrieve it", () => {
|
|
77
|
+
const testEvent = defineEvent({ id: "test.event" });
|
|
116
78
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
79
|
+
store.storeGenericItem(testEvent);
|
|
80
|
+
|
|
81
|
+
expect(store.events.has("test.event")).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should dispose of resources correctly", async () => {
|
|
85
|
+
const disposeFn = jest.fn(async (...args: any[]) => {});
|
|
86
|
+
const testResource = defineResource({
|
|
87
|
+
id: "test.resource",
|
|
88
|
+
dispose: disposeFn,
|
|
89
|
+
init: async () => "Resource Value",
|
|
121
90
|
});
|
|
122
91
|
|
|
123
|
-
store.
|
|
124
|
-
defineResource({
|
|
125
|
-
id: "root",
|
|
126
|
-
register: [task1, task2],
|
|
127
|
-
})
|
|
128
|
-
);
|
|
92
|
+
store.storeGenericItem(testResource);
|
|
129
93
|
|
|
130
|
-
|
|
94
|
+
// Simulate resource initialization
|
|
95
|
+
store.resources.get("test.resource")!.value = "Resource Value";
|
|
96
|
+
store.resources.get("test.resource")!.isInitialized = true;
|
|
131
97
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
expect(
|
|
135
|
-
|
|
136
|
-
|
|
98
|
+
await store.dispose();
|
|
99
|
+
|
|
100
|
+
expect(disposeFn).toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should throw an error for duplicate registration", () => {
|
|
104
|
+
const testTask = defineTask({
|
|
105
|
+
id: "duplicate.task",
|
|
106
|
+
run: async () => "Task executed",
|
|
137
107
|
});
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
108
|
+
|
|
109
|
+
store.storeGenericItem(testTask);
|
|
110
|
+
|
|
111
|
+
expect(() => store.storeGenericItem(testTask)).toThrow(
|
|
112
|
+
/already registered/i
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should throw an error if you're trying to initialize the store twice", () => {
|
|
117
|
+
const rootResource = defineResource({
|
|
118
|
+
id: "root",
|
|
119
|
+
init: async () => "Root Value",
|
|
141
120
|
});
|
|
121
|
+
|
|
122
|
+
store.initializeStore(rootResource, {});
|
|
123
|
+
|
|
124
|
+
expect(() => store.initializeStore(rootResource, {})).toThrow(
|
|
125
|
+
/Store already initialized/i
|
|
126
|
+
);
|
|
142
127
|
});
|
|
143
128
|
});
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
defineMiddleware,
|
|
6
6
|
} from "../define";
|
|
7
7
|
import { run } from "../run";
|
|
8
|
+
import { globalResources } from "../globalResources";
|
|
8
9
|
|
|
9
10
|
describe("run", () => {
|
|
10
11
|
// Tasks
|
|
@@ -603,7 +604,6 @@ describe("run", () => {
|
|
|
603
604
|
dependencies: () => ({ middle }),
|
|
604
605
|
register: () => [middle, testTask],
|
|
605
606
|
async init(_, { middle }) {
|
|
606
|
-
console.log(middle.toString());
|
|
607
607
|
expect(await middle()).toBe("middle");
|
|
608
608
|
},
|
|
609
609
|
});
|
|
@@ -611,4 +611,31 @@ describe("run", () => {
|
|
|
611
611
|
await run(app);
|
|
612
612
|
expect(mockFn).toHaveBeenCalled();
|
|
613
613
|
});
|
|
614
|
+
|
|
615
|
+
describe("disposal", () => {
|
|
616
|
+
it("should be able to dispose of a resource", async () => {
|
|
617
|
+
const disposeFn = jest.fn();
|
|
618
|
+
const testResource = defineResource({
|
|
619
|
+
id: "test.resource",
|
|
620
|
+
dispose: disposeFn,
|
|
621
|
+
init: async () => "Resource Value",
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
const app = defineResource({
|
|
625
|
+
id: "app",
|
|
626
|
+
register: [testResource],
|
|
627
|
+
dependencies: { testResource, store: globalResources.store },
|
|
628
|
+
async init(_, { testResource, store }) {
|
|
629
|
+
expect(testResource).toBe("Resource Value");
|
|
630
|
+
|
|
631
|
+
return {
|
|
632
|
+
dispose: () => store.dispose(),
|
|
633
|
+
};
|
|
634
|
+
},
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
const result = await run(app);
|
|
638
|
+
await result.dispose();
|
|
639
|
+
});
|
|
640
|
+
});
|
|
614
641
|
});
|
package/src/define.ts
CHANGED
|
@@ -21,14 +21,6 @@ export function defineTask<
|
|
|
21
21
|
>(
|
|
22
22
|
config: ITaskDefinition<Input, Output, Deps, Test>
|
|
23
23
|
): ITask<Input, Output, Deps, Test> {
|
|
24
|
-
const autorun: any = {};
|
|
25
|
-
// if (config.autorun) {
|
|
26
|
-
// if (config.autorun.on) {
|
|
27
|
-
// autorun.on = Array.isArray(config.autorun.on);
|
|
28
|
-
// autorun.schedule = config.autorun.schedule;
|
|
29
|
-
// }
|
|
30
|
-
// }
|
|
31
|
-
|
|
32
24
|
return {
|
|
33
25
|
[symbols.task]: true,
|
|
34
26
|
id: config.id,
|
|
@@ -63,6 +55,7 @@ export function defineResource<
|
|
|
63
55
|
id: constConfig.id,
|
|
64
56
|
dependencies: constConfig.dependencies,
|
|
65
57
|
hooks: constConfig.hooks || [],
|
|
58
|
+
dispose: constConfig.dispose,
|
|
66
59
|
register: constConfig.register || [],
|
|
67
60
|
init: constConfig.init,
|
|
68
61
|
with: function (config: TConfig) {
|
package/src/defs.ts
CHANGED
|
@@ -132,6 +132,19 @@ export interface IResourceDefinintion<
|
|
|
132
132
|
config: TConfig,
|
|
133
133
|
dependencies: DependencyValuesType<TDependencies>
|
|
134
134
|
) => Promise<TValue>;
|
|
135
|
+
/**
|
|
136
|
+
* Clean-up function for the resource. This is called when the resource is no longer needed.
|
|
137
|
+
*
|
|
138
|
+
* @param value The value of the resource
|
|
139
|
+
* @param config The configuration it received
|
|
140
|
+
* @param dependencies The dependencies it needed
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
dispose?: (
|
|
144
|
+
value: TValue,
|
|
145
|
+
config: TConfig,
|
|
146
|
+
dependencies: DependencyValuesType<TDependencies>
|
|
147
|
+
) => Promise<TValue>;
|
|
135
148
|
meta?: IResourceMeta;
|
|
136
149
|
}
|
|
137
150
|
|
package/src/errors.ts
CHANGED
|
@@ -19,4 +19,10 @@ export const Errors = {
|
|
|
19
19
|
|
|
20
20
|
middlewareAlreadyGlobal: (id: string) =>
|
|
21
21
|
new Error("Cannot call global on a global middleware: " + id),
|
|
22
|
+
|
|
23
|
+
locked: (what: string) =>
|
|
24
|
+
new Error(`Cannot modify the ${what} when it is locked.`),
|
|
25
|
+
|
|
26
|
+
storeAlreadyInitialized: () =>
|
|
27
|
+
new Error("Store already initialized. Cannot reinitialize."),
|
|
22
28
|
};
|
package/src/globalResources.ts
CHANGED
|
@@ -3,17 +3,19 @@ import { EventManager } from "./EventManager";
|
|
|
3
3
|
import { Store } from "./Store";
|
|
4
4
|
import { TaskRunner } from "./TaskRunner";
|
|
5
5
|
|
|
6
|
+
const store = defineResource({
|
|
7
|
+
id: "global.store",
|
|
8
|
+
init: async (store: Store) => store,
|
|
9
|
+
});
|
|
10
|
+
|
|
6
11
|
export const globalResources = {
|
|
7
|
-
store
|
|
8
|
-
|
|
9
|
-
init: async (store) => store,
|
|
10
|
-
}),
|
|
11
|
-
eventManager: defineResource<EventManager>({
|
|
12
|
+
store,
|
|
13
|
+
eventManager: defineResource({
|
|
12
14
|
id: "global.eventManager",
|
|
13
|
-
init: async (em) => em,
|
|
15
|
+
init: async (em: EventManager) => em,
|
|
14
16
|
}),
|
|
15
|
-
taskRunner: defineResource
|
|
17
|
+
taskRunner: defineResource({
|
|
16
18
|
id: "global.taskRunner",
|
|
17
|
-
init: async (runner) => runner,
|
|
19
|
+
init: async (runner: TaskRunner) => runner,
|
|
18
20
|
}),
|
|
19
21
|
};
|
package/src/run.ts
CHANGED
|
@@ -58,7 +58,7 @@ export type RunnerType = {
|
|
|
58
58
|
};
|
|
59
59
|
|
|
60
60
|
export async function run<C, V>(
|
|
61
|
-
resource: IResource<C>,
|
|
61
|
+
resource: IResource<C, V>,
|
|
62
62
|
config?: C
|
|
63
63
|
): Promise<V> {
|
|
64
64
|
const eventManager = new EventManager();
|
|
@@ -69,7 +69,6 @@ export async function run<C, V>(
|
|
|
69
69
|
// In the registration phase we register deeply all the resources, tasks, middleware and events
|
|
70
70
|
store.initializeStore(resource, config);
|
|
71
71
|
store.storeGenericItem(globalResources.taskRunner.with(taskRunner));
|
|
72
|
-
store.computeRegisterOfResource(resource, config);
|
|
73
72
|
|
|
74
73
|
// We verify that there isn't any circular dependencies before we begin computing the dependencies
|
|
75
74
|
const dependentNodes = store.getDependentNodes();
|
|
@@ -94,5 +93,8 @@ export async function run<C, V>(
|
|
|
94
93
|
|
|
95
94
|
await eventManager.emit(globalEvents.afterInit);
|
|
96
95
|
|
|
96
|
+
// disallow manipulation or attaching more
|
|
97
|
+
store.lock();
|
|
98
|
+
|
|
97
99
|
return store.root.value;
|
|
98
100
|
}
|