@bluelibs/runner 1.5.1 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/define.d.ts +3 -3
- package/dist/define.js +2 -1
- package/dist/define.js.map +1 -1
- package/dist/defs.d.ts +19 -4
- package/dist/defs.js +13 -7
- package/dist/defs.js.map +1 -1
- package/dist/run.d.ts +2 -2
- package/package.json +1 -1
- package/src/__tests__/errors.test.ts +3 -1
- package/src/__tests__/models/EventManager.test.ts +30 -9
- package/src/__tests__/run.test.ts +10 -2
- package/src/__tests__/typesafety.test.ts +4 -1
- package/src/define.ts +7 -4
- package/src/defs.ts +31 -10
- package/src/run.ts +2 -2
package/dist/define.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ITask, ITaskDefinition, IResource, IResourceWithConfig,
|
|
1
|
+
import { ITask, ITaskDefinition, IResource, IResourceWithConfig, IResourceDefinition, IEventDefinition, IMiddlewareDefinition, DependencyMapType, IMiddleware, IEventDefinitionConfig } from "./defs";
|
|
2
2
|
export declare function defineTask<Input = undefined, Output extends Promise<any> = any, Deps extends DependencyMapType = any, Test = any>(taskConfig: ITaskDefinition<Input, Output, Deps, Test>): ITask<Input, Output, Deps, Test>;
|
|
3
|
-
export declare function defineResource<TConfig = void, TValue = any, TDeps extends DependencyMapType = {}, THooks = any>(constConfig:
|
|
4
|
-
export declare function defineEvent<TPayload = any>(config:
|
|
3
|
+
export declare function defineResource<TConfig = void, TValue = any, TDeps extends DependencyMapType = {}, THooks = any>(constConfig: IResourceDefinition<TConfig, TValue, TDeps, THooks>): IResource<TConfig, TValue, TDeps>;
|
|
4
|
+
export declare function defineEvent<TPayload = any>(config: IEventDefinitionConfig<TPayload>): IEventDefinition<TPayload>;
|
|
5
5
|
export declare function defineMiddleware<TDeps extends DependencyMapType = {}>(config: IMiddlewareDefinition<TDeps>): IMiddleware<TDeps>;
|
|
6
6
|
export declare function isTask(definition: any): definition is ITask;
|
|
7
7
|
export declare function isResource(definition: any): definition is IResource;
|
package/dist/define.js
CHANGED
|
@@ -47,6 +47,7 @@ function defineResource(constConfig) {
|
|
|
47
47
|
with: function (config) {
|
|
48
48
|
return {
|
|
49
49
|
[defs_1.symbols.resourceWithConfig]: true,
|
|
50
|
+
id: this.id,
|
|
50
51
|
resource: this,
|
|
51
52
|
config,
|
|
52
53
|
};
|
|
@@ -68,7 +69,7 @@ function defineResource(constConfig) {
|
|
|
68
69
|
}
|
|
69
70
|
function defineEvent(config) {
|
|
70
71
|
return {
|
|
71
|
-
[defs_1.
|
|
72
|
+
[defs_1.symbolEvent]: true,
|
|
72
73
|
...config,
|
|
73
74
|
};
|
|
74
75
|
}
|
package/dist/define.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"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"}
|
package/dist/defs.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export declare const symbolTask: unique symbol;
|
|
2
|
+
export declare const symbolResource: unique symbol;
|
|
3
|
+
export declare const symbolResourceWithConfig: unique symbol;
|
|
4
|
+
export declare const symbolEvent: unique symbol;
|
|
5
|
+
export declare const symbolMiddleware: unique symbol;
|
|
6
|
+
export declare const symbolMiddlewareGlobal: unique symbol;
|
|
1
7
|
export declare const symbols: {
|
|
2
8
|
task: symbol;
|
|
3
9
|
resource: symbol;
|
|
@@ -19,7 +25,7 @@ export interface IEventMeta extends IMeta {
|
|
|
19
25
|
}
|
|
20
26
|
export interface IMiddlewareMeta extends IMeta {
|
|
21
27
|
}
|
|
22
|
-
export type DependencyMapType = Record<string, ITask | IResource
|
|
28
|
+
export type DependencyMapType = Record<string, ITask<any, any, any> | IResource<any, any, any> | IEventDefinition<any>>;
|
|
23
29
|
type ExtractTaskInput<T> = T extends ITask<infer I, any, infer D> ? I : never;
|
|
24
30
|
type ExtractTaskOutput<T> = T extends ITask<any, infer O, infer D> ? O : never;
|
|
25
31
|
type ExtractResourceValue<T> = T extends IResource<any, infer V, infer D> ? V : never;
|
|
@@ -31,7 +37,7 @@ export type DependencyValueType<T> = T extends ITask<any, any, any> ? TaskDepend
|
|
|
31
37
|
export type DependencyValuesType<T extends DependencyMapType> = {
|
|
32
38
|
[K in keyof T]: DependencyValueType<T[K]>;
|
|
33
39
|
};
|
|
34
|
-
export type RegisterableItems = IResourceWithConfig<any> | IResource<any> | ITaskDefinition | IMiddlewareDefinition | IEventDefinition;
|
|
40
|
+
export type RegisterableItems = IResourceWithConfig<any> | IResource<void, any, any> | ITaskDefinition | IMiddlewareDefinition | IEventDefinition;
|
|
35
41
|
export interface ITaskDefinition<TInput = any, TOutput extends Promise<any> = any, TDependencies extends DependencyMapType = {}, TEventDefinitionInput = null> {
|
|
36
42
|
id: string;
|
|
37
43
|
dependencies?: TDependencies | (() => TDependencies);
|
|
@@ -85,7 +91,7 @@ export interface ITask<TInput = any, TOutput extends Promise<any> = any, TDepend
|
|
|
85
91
|
onError: IEventDefinition<OnErrorEventPayload>;
|
|
86
92
|
};
|
|
87
93
|
}
|
|
88
|
-
export interface
|
|
94
|
+
export interface IResourceDefinition<TConfig = any, TValue = unknown, TDependencies extends DependencyMapType = {}, THooks = any, TRegisterableItems = any> {
|
|
89
95
|
id: string;
|
|
90
96
|
dependencies?: TDependencies | ((config: TConfig) => TDependencies);
|
|
91
97
|
hooks?: IHookDefinition<TDependencies, THooks>[] | ((config: TConfig) => IHookDefinition<TDependencies, THooks>[]);
|
|
@@ -104,7 +110,7 @@ export interface IResourceDefinintion<TConfig = any, TValue = unknown, TDependen
|
|
|
104
110
|
overrides?: Array<IResource | ITask | IMiddleware | IResourceWithConfig>;
|
|
105
111
|
middleware?: IMiddlewareDefinition[];
|
|
106
112
|
}
|
|
107
|
-
export interface IResource<TConfig = void, TValue = any, TDependencies extends DependencyMapType = any> extends
|
|
113
|
+
export interface IResource<TConfig = void, TValue = any, TDependencies extends DependencyMapType = any> extends IResourceDefinition<TConfig, TValue, TDependencies> {
|
|
108
114
|
with(config: TConfig): IResourceWithConfig<TConfig, TValue, TDependencies>;
|
|
109
115
|
register: Array<RegisterableItems> | ((config: TConfig) => Array<RegisterableItems>);
|
|
110
116
|
/**
|
|
@@ -120,6 +126,7 @@ export interface IResource<TConfig = void, TValue = any, TDependencies extends D
|
|
|
120
126
|
middleware: IMiddlewareDefinition[];
|
|
121
127
|
}
|
|
122
128
|
export interface IResourceWithConfig<TConfig = any, TValue = any, TDependencies extends DependencyMapType = any> {
|
|
129
|
+
id: string;
|
|
123
130
|
resource: IResource<TConfig, TValue, TDependencies>;
|
|
124
131
|
config: TConfig;
|
|
125
132
|
}
|
|
@@ -139,8 +146,16 @@ export interface IEvent<TPayload = any> {
|
|
|
139
146
|
source: string;
|
|
140
147
|
}
|
|
141
148
|
export type EventHandlerType<T = any> = (event: IEvent<T>) => any | Promise<any>;
|
|
149
|
+
export interface IEventDefinitionConfig<TPayload = void> {
|
|
150
|
+
id: string;
|
|
151
|
+
meta?: IEventMeta;
|
|
152
|
+
}
|
|
142
153
|
export interface IEventDefinition<TPayload = void> {
|
|
143
154
|
id: string;
|
|
155
|
+
/**
|
|
156
|
+
* We use this event to discriminate between resources with just 'id' and 'events' as they collide. This is a workaround, should be redone using classes and instanceof.
|
|
157
|
+
*/
|
|
158
|
+
[symbolEvent]: true;
|
|
144
159
|
meta?: IEventMeta;
|
|
145
160
|
}
|
|
146
161
|
export interface IMiddlewareDefinition<TDependencies extends DependencyMapType = any> {
|
package/dist/defs.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.symbols = void 0;
|
|
3
|
+
exports.symbols = exports.symbolMiddlewareGlobal = exports.symbolMiddleware = exports.symbolEvent = exports.symbolResourceWithConfig = exports.symbolResource = exports.symbolTask = void 0;
|
|
4
|
+
exports.symbolTask = Symbol("runner.task");
|
|
5
|
+
exports.symbolResource = Symbol("runner.resource");
|
|
6
|
+
exports.symbolResourceWithConfig = Symbol("runner.resourceWithConfig");
|
|
7
|
+
exports.symbolEvent = Symbol("runner.event");
|
|
8
|
+
exports.symbolMiddleware = Symbol("runner.middleware");
|
|
9
|
+
exports.symbolMiddlewareGlobal = Symbol("runner.middlewareGlobal");
|
|
4
10
|
exports.symbols = {
|
|
5
|
-
task:
|
|
6
|
-
resource:
|
|
7
|
-
resourceWithConfig:
|
|
8
|
-
event:
|
|
9
|
-
middleware:
|
|
10
|
-
middlewareGlobal:
|
|
11
|
+
task: exports.symbolTask,
|
|
12
|
+
resource: exports.symbolResource,
|
|
13
|
+
resourceWithConfig: exports.symbolResourceWithConfig,
|
|
14
|
+
event: exports.symbolEvent,
|
|
15
|
+
middleware: exports.symbolMiddleware,
|
|
16
|
+
middlewareGlobal: exports.symbolMiddlewareGlobal,
|
|
11
17
|
};
|
|
12
18
|
//# sourceMappingURL=defs.js.map
|
package/dist/defs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defs.js","sourceRoot":"","sources":["../src/defs.ts"],"names":[],"mappings":";;;AAAa,QAAA,
|
|
1
|
+
{"version":3,"file":"defs.js","sourceRoot":"","sources":["../src/defs.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAkB,MAAM,CAAC,aAAa,CAAC,CAAC;AAClD,QAAA,cAAc,GAAkB,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAC1D,QAAA,wBAAwB,GAAkB,MAAM,CAC3D,2BAA2B,CAC5B,CAAC;AACW,QAAA,WAAW,GAAkB,MAAM,CAAC,cAAc,CAAC,CAAC;AACpD,QAAA,gBAAgB,GAAkB,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAC9D,QAAA,sBAAsB,GAAkB,MAAM,CACzD,yBAAyB,CAC1B,CAAC;AAEW,QAAA,OAAO,GAAG;IACrB,IAAI,EAAE,kBAAU;IAChB,QAAQ,EAAE,sBAAc;IACxB,kBAAkB,EAAE,gCAAwB;IAC5C,KAAK,EAAE,mBAAW;IAClB,UAAU,EAAE,wBAAgB;IAC5B,gBAAgB,EAAE,8BAAsB;CACzC,CAAC"}
|
package/dist/run.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DependencyMapType, ITaskDefinition,
|
|
1
|
+
import { DependencyMapType, ITaskDefinition, IResourceDefinition, IEventDefinition, IMiddlewareDefinition, DependencyValuesType, IResource } from "./defs";
|
|
2
2
|
export type ResourcesStoreElementType<C = any, V = any, D extends DependencyMapType = {}> = {
|
|
3
|
-
resource:
|
|
3
|
+
resource: IResourceDefinition<C, V, D>;
|
|
4
4
|
computedDependencies?: DependencyValuesType<D>;
|
|
5
5
|
config: C;
|
|
6
6
|
value: V;
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IEvent, IEventDefinition } from "../../defs";
|
|
1
|
+
import { IEvent, IEventDefinition, symbolEvent } from "../../defs";
|
|
2
2
|
import { Errors } from "../../errors";
|
|
3
3
|
import { EventManager } from "../../models/EventManager";
|
|
4
4
|
|
|
@@ -8,7 +8,7 @@ describe("EventManager", () => {
|
|
|
8
8
|
|
|
9
9
|
beforeEach(() => {
|
|
10
10
|
eventManager = new EventManager();
|
|
11
|
-
eventDefinition = { id: "testEvent" };
|
|
11
|
+
eventDefinition = { id: "testEvent", [symbolEvent]: true };
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
it("should add and emit event listener", async () => {
|
|
@@ -160,8 +160,14 @@ describe("EventManager", () => {
|
|
|
160
160
|
});
|
|
161
161
|
|
|
162
162
|
it("should handle multiple events", async () => {
|
|
163
|
-
const eventDef1: IEventDefinition<string> = {
|
|
164
|
-
|
|
163
|
+
const eventDef1: IEventDefinition<string> = {
|
|
164
|
+
id: "event1",
|
|
165
|
+
[symbolEvent]: true,
|
|
166
|
+
};
|
|
167
|
+
const eventDef2: IEventDefinition<string> = {
|
|
168
|
+
id: "event2",
|
|
169
|
+
[symbolEvent]: true,
|
|
170
|
+
};
|
|
165
171
|
|
|
166
172
|
const handler1 = jest.fn();
|
|
167
173
|
const handler2 = jest.fn();
|
|
@@ -203,8 +209,14 @@ describe("EventManager", () => {
|
|
|
203
209
|
});
|
|
204
210
|
|
|
205
211
|
it("should handle listeners added to multiple events", async () => {
|
|
206
|
-
const eventDef1: IEventDefinition<string> = {
|
|
207
|
-
|
|
212
|
+
const eventDef1: IEventDefinition<string> = {
|
|
213
|
+
id: "event1",
|
|
214
|
+
[symbolEvent]: true,
|
|
215
|
+
};
|
|
216
|
+
const eventDef2: IEventDefinition<string> = {
|
|
217
|
+
id: "event2",
|
|
218
|
+
[symbolEvent]: true,
|
|
219
|
+
};
|
|
208
220
|
|
|
209
221
|
const handler = jest.fn();
|
|
210
222
|
|
|
@@ -231,8 +243,14 @@ describe("EventManager", () => {
|
|
|
231
243
|
});
|
|
232
244
|
|
|
233
245
|
it("should not affect other events when emitting one", async () => {
|
|
234
|
-
const eventDef1: IEventDefinition<string> = {
|
|
235
|
-
|
|
246
|
+
const eventDef1: IEventDefinition<string> = {
|
|
247
|
+
id: "event1",
|
|
248
|
+
[symbolEvent]: true,
|
|
249
|
+
};
|
|
250
|
+
const eventDef2: IEventDefinition<string> = {
|
|
251
|
+
id: "event2",
|
|
252
|
+
[symbolEvent]: true,
|
|
253
|
+
};
|
|
236
254
|
|
|
237
255
|
const handler1 = jest.fn();
|
|
238
256
|
const handler2 = jest.fn();
|
|
@@ -368,7 +386,10 @@ describe("EventManager", () => {
|
|
|
368
386
|
it("should handle listeners with no data", async () => {
|
|
369
387
|
const handler = jest.fn();
|
|
370
388
|
|
|
371
|
-
const voidEventDefinition: IEventDefinition<void> = {
|
|
389
|
+
const voidEventDefinition: IEventDefinition<void> = {
|
|
390
|
+
id: "voidEvent",
|
|
391
|
+
[symbolEvent]: true,
|
|
392
|
+
};
|
|
372
393
|
|
|
373
394
|
eventManager.addListener(voidEventDefinition, handler);
|
|
374
395
|
|
|
@@ -440,7 +440,12 @@ describe("run", () => {
|
|
|
440
440
|
|
|
441
441
|
const typeTest = defineResource({
|
|
442
442
|
id: "typeTest",
|
|
443
|
-
register: [
|
|
443
|
+
register: [
|
|
444
|
+
t2,
|
|
445
|
+
testResource.with({
|
|
446
|
+
prefix: "Hello,",
|
|
447
|
+
}),
|
|
448
|
+
],
|
|
444
449
|
});
|
|
445
450
|
|
|
446
451
|
const app = defineResource({
|
|
@@ -460,7 +465,10 @@ describe("run", () => {
|
|
|
460
465
|
const testResource = defineResource({
|
|
461
466
|
id: "test.resource",
|
|
462
467
|
init: async () => {
|
|
463
|
-
|
|
468
|
+
// we do this so it doesn't become a never.
|
|
469
|
+
if (true === true) {
|
|
470
|
+
throw new Error("Init failed");
|
|
471
|
+
}
|
|
464
472
|
},
|
|
465
473
|
});
|
|
466
474
|
|
|
@@ -77,12 +77,15 @@ describe("typesafety", () => {
|
|
|
77
77
|
// @ts-expect-error
|
|
78
78
|
deps.task2;
|
|
79
79
|
},
|
|
80
|
-
register:
|
|
80
|
+
register: [
|
|
81
81
|
dummyResourceNoConfig,
|
|
82
82
|
// @ts-expect-error
|
|
83
83
|
dummyResourceNoConfig.with("hello"),
|
|
84
84
|
// @ts-expect-error
|
|
85
85
|
dummyResourceNoConfig.with({ anyObject: true }),
|
|
86
|
+
|
|
87
|
+
// @ts-expect-error
|
|
88
|
+
dummyResource, // should throw
|
|
86
89
|
dummyResource.with({ ok: true }),
|
|
87
90
|
// @ts-expect-error
|
|
88
91
|
dummyResource.with({ ok: 123 }),
|
package/src/define.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
ITaskDefinition,
|
|
4
4
|
IResource,
|
|
5
5
|
IResourceWithConfig,
|
|
6
|
-
|
|
6
|
+
IResourceDefinition,
|
|
7
7
|
IEventDefinition,
|
|
8
8
|
IMiddlewareDefinition,
|
|
9
9
|
symbols,
|
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
IMiddleware,
|
|
13
13
|
IHookDefinition,
|
|
14
14
|
IEvent,
|
|
15
|
+
IEventDefinitionConfig,
|
|
16
|
+
symbolEvent,
|
|
15
17
|
} from "./defs";
|
|
16
18
|
import { Errors } from "./errors";
|
|
17
19
|
|
|
@@ -52,7 +54,7 @@ export function defineResource<
|
|
|
52
54
|
TDeps extends DependencyMapType = {},
|
|
53
55
|
THooks = any
|
|
54
56
|
>(
|
|
55
|
-
constConfig:
|
|
57
|
+
constConfig: IResourceDefinition<TConfig, TValue, TDeps, THooks>
|
|
56
58
|
): IResource<TConfig, TValue, TDeps> {
|
|
57
59
|
return {
|
|
58
60
|
[symbols.resource]: true,
|
|
@@ -66,6 +68,7 @@ export function defineResource<
|
|
|
66
68
|
with: function (config: TConfig) {
|
|
67
69
|
return {
|
|
68
70
|
[symbols.resourceWithConfig]: true,
|
|
71
|
+
id: this.id,
|
|
69
72
|
resource: this,
|
|
70
73
|
config,
|
|
71
74
|
};
|
|
@@ -88,10 +91,10 @@ export function defineResource<
|
|
|
88
91
|
}
|
|
89
92
|
|
|
90
93
|
export function defineEvent<TPayload = any>(
|
|
91
|
-
config:
|
|
94
|
+
config: IEventDefinitionConfig<TPayload>
|
|
92
95
|
): IEventDefinition<TPayload> {
|
|
93
96
|
return {
|
|
94
|
-
[
|
|
97
|
+
[symbolEvent]: true,
|
|
95
98
|
...config,
|
|
96
99
|
};
|
|
97
100
|
}
|
package/src/defs.ts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
export const symbolTask: unique symbol = Symbol("runner.task");
|
|
2
|
+
export const symbolResource: unique symbol = Symbol("runner.resource");
|
|
3
|
+
export const symbolResourceWithConfig: unique symbol = Symbol(
|
|
4
|
+
"runner.resourceWithConfig"
|
|
5
|
+
);
|
|
6
|
+
export const symbolEvent: unique symbol = Symbol("runner.event");
|
|
7
|
+
export const symbolMiddleware: unique symbol = Symbol("runner.middleware");
|
|
8
|
+
export const symbolMiddlewareGlobal: unique symbol = Symbol(
|
|
9
|
+
"runner.middlewareGlobal"
|
|
10
|
+
);
|
|
11
|
+
|
|
1
12
|
export const symbols = {
|
|
2
|
-
task:
|
|
3
|
-
resource:
|
|
4
|
-
resourceWithConfig:
|
|
5
|
-
event:
|
|
6
|
-
middleware:
|
|
7
|
-
middlewareGlobal:
|
|
13
|
+
task: symbolTask,
|
|
14
|
+
resource: symbolResource,
|
|
15
|
+
resourceWithConfig: symbolResourceWithConfig,
|
|
16
|
+
event: symbolEvent,
|
|
17
|
+
middleware: symbolMiddleware,
|
|
18
|
+
middlewareGlobal: symbolMiddlewareGlobal,
|
|
8
19
|
};
|
|
9
20
|
|
|
10
21
|
export interface IMeta {
|
|
@@ -21,7 +32,7 @@ export interface IMiddlewareMeta extends IMeta {}
|
|
|
21
32
|
// DependencyMap types
|
|
22
33
|
export type DependencyMapType = Record<
|
|
23
34
|
string,
|
|
24
|
-
ITask | IResource
|
|
35
|
+
ITask<any, any, any> | IResource<any, any, any> | IEventDefinition<any>
|
|
25
36
|
>;
|
|
26
37
|
|
|
27
38
|
// Helper Types for Extracting Generics
|
|
@@ -53,7 +64,7 @@ export type DependencyValuesType<T extends DependencyMapType> = {
|
|
|
53
64
|
// RegisterableItems Type with Conditional Inclusion
|
|
54
65
|
export type RegisterableItems =
|
|
55
66
|
| IResourceWithConfig<any>
|
|
56
|
-
| IResource<any>
|
|
67
|
+
| IResource<void, any, any>
|
|
57
68
|
| ITaskDefinition
|
|
58
69
|
| IMiddlewareDefinition
|
|
59
70
|
| IEventDefinition;
|
|
@@ -136,7 +147,7 @@ export interface ITask<
|
|
|
136
147
|
};
|
|
137
148
|
}
|
|
138
149
|
// Resource interfaces
|
|
139
|
-
export interface
|
|
150
|
+
export interface IResourceDefinition<
|
|
140
151
|
TConfig = any,
|
|
141
152
|
TValue = unknown,
|
|
142
153
|
TDependencies extends DependencyMapType = {},
|
|
@@ -177,7 +188,7 @@ export interface IResource<
|
|
|
177
188
|
TConfig = void,
|
|
178
189
|
TValue = any,
|
|
179
190
|
TDependencies extends DependencyMapType = any
|
|
180
|
-
> extends
|
|
191
|
+
> extends IResourceDefinition<TConfig, TValue, TDependencies> {
|
|
181
192
|
with(config: TConfig): IResourceWithConfig<TConfig, TValue, TDependencies>;
|
|
182
193
|
register:
|
|
183
194
|
| Array<RegisterableItems>
|
|
@@ -203,6 +214,7 @@ export interface IResourceWithConfig<
|
|
|
203
214
|
TValue = any,
|
|
204
215
|
TDependencies extends DependencyMapType = any
|
|
205
216
|
> {
|
|
217
|
+
id: string;
|
|
206
218
|
resource: IResource<TConfig, TValue, TDependencies>;
|
|
207
219
|
config: TConfig;
|
|
208
220
|
}
|
|
@@ -228,8 +240,17 @@ export type EventHandlerType<T = any> = (
|
|
|
228
240
|
) => any | Promise<any>;
|
|
229
241
|
|
|
230
242
|
// Other necessary interfaces
|
|
243
|
+
export interface IEventDefinitionConfig<TPayload = void> {
|
|
244
|
+
id: string;
|
|
245
|
+
meta?: IEventMeta;
|
|
246
|
+
}
|
|
247
|
+
|
|
231
248
|
export interface IEventDefinition<TPayload = void> {
|
|
232
249
|
id: string;
|
|
250
|
+
/**
|
|
251
|
+
* We use this event to discriminate between resources with just 'id' and 'events' as they collide. This is a workaround, should be redone using classes and instanceof.
|
|
252
|
+
*/
|
|
253
|
+
[symbolEvent]: true;
|
|
233
254
|
meta?: IEventMeta;
|
|
234
255
|
}
|
|
235
256
|
|
package/src/run.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { TaskRunner } from "./models/TaskRunner";
|
|
|
2
2
|
import {
|
|
3
3
|
DependencyMapType,
|
|
4
4
|
ITaskDefinition,
|
|
5
|
-
|
|
5
|
+
IResourceDefinition,
|
|
6
6
|
IEventDefinition,
|
|
7
7
|
IMiddlewareDefinition,
|
|
8
8
|
DependencyValuesType,
|
|
@@ -22,7 +22,7 @@ export type ResourcesStoreElementType<
|
|
|
22
22
|
V = any,
|
|
23
23
|
D extends DependencyMapType = {}
|
|
24
24
|
> = {
|
|
25
|
-
resource:
|
|
25
|
+
resource: IResourceDefinition<C, V, D>;
|
|
26
26
|
computedDependencies?: DependencyValuesType<D>;
|
|
27
27
|
config: C;
|
|
28
28
|
value: V;
|