@bluelibs/runner 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/define.js +6 -0
- package/dist/define.js.map +1 -1
- package/dist/defs.d.ts +2 -0
- package/dist/defs.js +3 -1
- package/dist/defs.js.map +1 -1
- package/dist/globalResources.js +5 -0
- package/dist/globalResources.js.map +1 -1
- package/dist/models/TaskRunner.d.ts +1 -1
- package/dist/models/TaskRunner.js +4 -5
- package/dist/models/TaskRunner.js.map +1 -1
- package/dist/tools/getCallerFile.d.ts +1 -0
- package/dist/tools/getCallerFile.js +30 -0
- package/dist/tools/getCallerFile.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/index.ts +1 -0
- package/src/__tests__/run.test.ts +12 -3
- package/src/__tests__/tools/getCallerFile.test.ts +51 -0
- package/src/define.ts +7 -0
- package/src/defs.ts +3 -0
- package/src/globalResources.ts +6 -0
- package/src/models/TaskRunner.ts +4 -12
- package/src/tools/getCallerFile.ts +33 -0
package/README.md
CHANGED
|
@@ -280,7 +280,7 @@ const app = resource({
|
|
|
280
280
|
|
|
281
281
|
### wildcard events
|
|
282
282
|
|
|
283
|
-
You can listen to all events by using the wildcard `*`.
|
|
283
|
+
You can listen to all events by using the wildcard `*`. However you need to **manually check** if your dependencies have been computed. For example we dispatch events like 'global.beforeInit' before anything is initialized.
|
|
284
284
|
|
|
285
285
|
```ts
|
|
286
286
|
import { task, resource, run, event, global } from "@bluelibs/runner";
|
package/dist/define.js
CHANGED
|
@@ -11,9 +11,12 @@ exports.isEvent = isEvent;
|
|
|
11
11
|
exports.isMiddleware = isMiddleware;
|
|
12
12
|
const defs_1 = require("./defs");
|
|
13
13
|
const errors_1 = require("./errors");
|
|
14
|
+
const getCallerFile_1 = require("./tools/getCallerFile");
|
|
15
|
+
// Helper function to get the caller file
|
|
14
16
|
function defineTask(taskConfig) {
|
|
15
17
|
return {
|
|
16
18
|
[defs_1.symbols.task]: true,
|
|
19
|
+
[defs_1.symbols.filePath]: (0, getCallerFile_1.getCallerFile)(),
|
|
17
20
|
id: taskConfig.id,
|
|
18
21
|
dependencies: taskConfig.dependencies || {},
|
|
19
22
|
middleware: taskConfig.middleware || [],
|
|
@@ -37,6 +40,7 @@ function defineTask(taskConfig) {
|
|
|
37
40
|
function defineResource(constConfig) {
|
|
38
41
|
return {
|
|
39
42
|
[defs_1.symbols.resource]: true,
|
|
43
|
+
[defs_1.symbols.filePath]: (0, getCallerFile_1.getCallerFile)(),
|
|
40
44
|
id: constConfig.id,
|
|
41
45
|
dependencies: constConfig.dependencies,
|
|
42
46
|
dispose: constConfig.dispose,
|
|
@@ -68,12 +72,14 @@ function defineResource(constConfig) {
|
|
|
68
72
|
}
|
|
69
73
|
function defineEvent(config) {
|
|
70
74
|
return {
|
|
75
|
+
[defs_1.symbols.filePath]: (0, getCallerFile_1.getCallerFile)(),
|
|
71
76
|
[defs_1.symbolEvent]: true,
|
|
72
77
|
...config,
|
|
73
78
|
};
|
|
74
79
|
}
|
|
75
80
|
function defineMiddleware(config) {
|
|
76
81
|
const object = {
|
|
82
|
+
[defs_1.symbols.filePath]: (0, getCallerFile_1.getCallerFile)(),
|
|
77
83
|
[defs_1.symbols.middleware]: true,
|
|
78
84
|
...config,
|
|
79
85
|
dependencies: config.dependencies || {},
|
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":";;AAsBA,gCA8BC;AAED,wCAwCC;AAED,kCAQC;AAED,4CAsBC;AAED,wBAEC;AAED,gCAEC;AAED,oDAIC;AAED,0BAEC;AAED,oCAEC;AAtJD,iCAgBgB;AAChB,qCAAkC;AAClC,yDAAsD;AAEtD,yCAAyC;AAEzC,SAAgB,UAAU,CAMxB,UAA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AU,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
|
@@ -4,6 +4,7 @@ export declare const symbolResourceWithConfig: unique symbol;
|
|
|
4
4
|
export declare const symbolEvent: unique symbol;
|
|
5
5
|
export declare const symbolMiddleware: unique symbol;
|
|
6
6
|
export declare const symbolMiddlewareGlobal: unique symbol;
|
|
7
|
+
export declare const symbolFilePath: unique symbol;
|
|
7
8
|
export declare const symbols: {
|
|
8
9
|
task: symbol;
|
|
9
10
|
resource: symbol;
|
|
@@ -11,6 +12,7 @@ export declare const symbols: {
|
|
|
11
12
|
event: symbol;
|
|
12
13
|
middleware: symbol;
|
|
13
14
|
middlewareGlobal: symbol;
|
|
15
|
+
filePath: symbol;
|
|
14
16
|
};
|
|
15
17
|
export interface IMeta {
|
|
16
18
|
title?: string;
|
package/dist/defs.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.symbols = exports.symbolMiddlewareGlobal = exports.symbolMiddleware = exports.symbolEvent = exports.symbolResourceWithConfig = exports.symbolResource = exports.symbolTask = void 0;
|
|
3
|
+
exports.symbols = exports.symbolFilePath = exports.symbolMiddlewareGlobal = exports.symbolMiddleware = exports.symbolEvent = exports.symbolResourceWithConfig = exports.symbolResource = exports.symbolTask = void 0;
|
|
4
4
|
exports.symbolTask = Symbol("runner.task");
|
|
5
5
|
exports.symbolResource = Symbol("runner.resource");
|
|
6
6
|
exports.symbolResourceWithConfig = Symbol("runner.resourceWithConfig");
|
|
7
7
|
exports.symbolEvent = Symbol("runner.event");
|
|
8
8
|
exports.symbolMiddleware = Symbol("runner.middleware");
|
|
9
9
|
exports.symbolMiddlewareGlobal = Symbol("runner.middlewareGlobal");
|
|
10
|
+
exports.symbolFilePath = Symbol("runner.filePath");
|
|
10
11
|
exports.symbols = {
|
|
11
12
|
task: exports.symbolTask,
|
|
12
13
|
resource: exports.symbolResource,
|
|
@@ -14,5 +15,6 @@ exports.symbols = {
|
|
|
14
15
|
event: exports.symbolEvent,
|
|
15
16
|
middleware: exports.symbolMiddleware,
|
|
16
17
|
middlewareGlobal: exports.symbolMiddlewareGlobal,
|
|
18
|
+
filePath: exports.symbolFilePath,
|
|
17
19
|
};
|
|
18
20
|
//# 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,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;
|
|
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,cAAc,GAAkB,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAE1D,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;IACxC,QAAQ,EAAE,sBAAc;CACzB,CAAC"}
|
package/dist/globalResources.js
CHANGED
|
@@ -5,6 +5,11 @@ const define_1 = require("./define");
|
|
|
5
5
|
const store = (0, define_1.defineResource)({
|
|
6
6
|
id: "global.store",
|
|
7
7
|
init: async (store) => store,
|
|
8
|
+
meta: {
|
|
9
|
+
title: "Store",
|
|
10
|
+
description: "A global store that can be used to store and retrieve tasks, resources, events and middleware",
|
|
11
|
+
tags: ["internal"],
|
|
12
|
+
},
|
|
8
13
|
});
|
|
9
14
|
exports.globalResources = {
|
|
10
15
|
store,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"globalResources.js","sourceRoot":"","sources":["../src/globalResources.ts"],"names":[],"mappings":";;;AAAA,qCAA0C;AAM1C,MAAM,KAAK,GAAG,IAAA,uBAAc,EAAC;IAC3B,EAAE,EAAE,cAAc;IAClB,IAAI,EAAE,KAAK,EAAE,KAAY,EAAE,EAAE,CAAC,KAAK;
|
|
1
|
+
{"version":3,"file":"globalResources.js","sourceRoot":"","sources":["../src/globalResources.ts"],"names":[],"mappings":";;;AAAA,qCAA0C;AAM1C,MAAM,KAAK,GAAG,IAAA,uBAAc,EAAC;IAC3B,EAAE,EAAE,cAAc;IAClB,IAAI,EAAE,KAAK,EAAE,KAAY,EAAE,EAAE,CAAC,KAAK;IACnC,IAAI,EAAE;QACJ,KAAK,EAAE,OAAO;QACd,WAAW,EACT,+FAA+F;QACjG,IAAI,EAAE,CAAC,UAAU,CAAC;KACnB;CACF,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;QACpC,IAAI,EAAE;YACJ,KAAK,EAAE,eAAe;YACtB,WAAW,EACT,iGAAiG;YACnG,IAAI,EAAE,CAAC,UAAU,CAAC;SACnB;KACF,CAAC;IACF,UAAU,EAAE,IAAA,uBAAc,EAAC;QACzB,EAAE,EAAE,mBAAmB;QACvB,IAAI,EAAE,KAAK,EAAE,MAAkB,EAAE,EAAE,CAAC,MAAM;QAC1C,IAAI,EAAE;YACJ,KAAK,EAAE,aAAa;YACpB,WAAW,EACT,+GAA+G;YACjH,IAAI,EAAE,CAAC,UAAU,CAAC;SACnB;KACF,CAAC;IACF,MAAM,EAAE,IAAA,uBAAc,EAAC;QACrB,EAAE,EAAE,eAAe;QACnB,IAAI,EAAE,KAAK,EAAE,MAAc,EAAE,EAAE,CAAC,MAAM;QACtC,IAAI,EAAE;YACJ,KAAK,EAAE,QAAQ;YACf,WAAW,EACT,4HAA4H;SAC/H;KACF,CAAC;CACH,CAAC"}
|
|
@@ -23,5 +23,5 @@ export declare class TaskRunner {
|
|
|
23
23
|
* @param taskDependencies
|
|
24
24
|
* @returns
|
|
25
25
|
*/
|
|
26
|
-
protected createRunnerWithMiddleware<TInput, TOutput extends Promise<any>, TDeps extends DependencyMapType>(task: ITask<TInput, TOutput, TDeps
|
|
26
|
+
protected createRunnerWithMiddleware<TInput, TOutput extends Promise<any>, TDeps extends DependencyMapType>(task: ITask<TInput, TOutput, TDeps>): (input: any) => Promise<TOutput>;
|
|
27
27
|
}
|
|
@@ -22,9 +22,7 @@ class TaskRunner {
|
|
|
22
22
|
async run(task, input, taskDependencies) {
|
|
23
23
|
let runner = this.runnerStore.get(task.id);
|
|
24
24
|
if (!runner) {
|
|
25
|
-
|
|
26
|
-
const deps = taskDependencies || storeTask.computedDependencies;
|
|
27
|
-
runner = this.createRunnerWithMiddleware(task, deps);
|
|
25
|
+
runner = this.createRunnerWithMiddleware(task);
|
|
28
26
|
this.runnerStore.set(task.id, runner);
|
|
29
27
|
}
|
|
30
28
|
const isGlobalEventListener = task.on === "*";
|
|
@@ -81,14 +79,15 @@ class TaskRunner {
|
|
|
81
79
|
* @param taskDependencies
|
|
82
80
|
* @returns
|
|
83
81
|
*/
|
|
84
|
-
createRunnerWithMiddleware(task
|
|
82
|
+
createRunnerWithMiddleware(task) {
|
|
83
|
+
const storeTask = this.store.tasks.get(task.id);
|
|
85
84
|
// this is the final next()
|
|
86
85
|
let next = async (input) => {
|
|
87
86
|
this.logger.debug({
|
|
88
87
|
message: `Running task ${task.id}`,
|
|
89
88
|
input,
|
|
90
89
|
}, task.id);
|
|
91
|
-
return task.run.call(null, input,
|
|
90
|
+
return task.run.call(null, input, storeTask?.computedDependencies);
|
|
92
91
|
};
|
|
93
92
|
const existingMiddlewares = task.middleware;
|
|
94
93
|
const createdMiddlewares = [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../../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CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACxE,GAAG,mBAAmB;SACvB,CAAC;QAEF,IAAI,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAClC,iDAAiD;YACjD,oCAAoC;YACpC,KAAK,IAAI,CAAC,GAAG,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;gBACxD,MAAM,UAAU,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAAC;gBACzC,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAChD,UAAU,CAAC,EAAE,CACgB,CAAC,CAAC,4EAA4E;gBAE7G,MAAM,YAAY,GAAG,IAAI,CAAC;gBAC1B,IAAI,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE;oBACrB,OAAO,eAAe,CAAC,UAAU,CAAC,GAAG,CACnC;wBACE,cAAc,EAAE,IAAW;wBAC3B,KAAK;wBACL,IAAI,EAAE,YAAY;qBACnB,EACD,eAAe,CAAC,oBAAoB,CACrC,CAAC;gBACJ,CAAC,CAAC;YACJ,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA3KD,gCA2KC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getCallerFile(): string | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCallerFile = getCallerFile;
|
|
4
|
+
function getCallerFile() {
|
|
5
|
+
const originalFunc = Error.prepareStackTrace;
|
|
6
|
+
try {
|
|
7
|
+
const err = new Error();
|
|
8
|
+
let callerfile;
|
|
9
|
+
let currentfile;
|
|
10
|
+
// Safeguard prepareStackTrace
|
|
11
|
+
Error.prepareStackTrace = (err, stack) => stack;
|
|
12
|
+
const stack = err.stack;
|
|
13
|
+
// Don't know how to test this.
|
|
14
|
+
// if (stack.length < 3) {
|
|
15
|
+
// // We need at least 3 frames: current function, its caller, and one above
|
|
16
|
+
// return undefined;
|
|
17
|
+
// }
|
|
18
|
+
// Remove the first frame (getCallerFile itself)
|
|
19
|
+
stack.shift();
|
|
20
|
+
// Remove the second frame (the direct caller of getCallerFile)
|
|
21
|
+
currentfile = stack.shift()?.getFileName();
|
|
22
|
+
// The third frame (the caller above the immediate one)
|
|
23
|
+
callerfile = stack.shift()?.getFileName();
|
|
24
|
+
return callerfile; // Return the file name of the caller above
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
Error.prepareStackTrace = originalFunc;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=getCallerFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCallerFile.js","sourceRoot":"","sources":["../../src/tools/getCallerFile.ts"],"names":[],"mappings":";;AAAA,sCAgCC;AAhCD,SAAgB,aAAa;IAC3B,MAAM,YAAY,GAAG,KAAK,CAAC,iBAAiB,CAAC;IAE7C,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,KAAK,EAAE,CAAC;QACxB,IAAI,UAAU,CAAC;QACf,IAAI,WAAW,CAAC;QAEhB,8BAA8B;QAC9B,KAAK,CAAC,iBAAiB,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC;QAEhD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAqC,CAAC;QAExD,+BAA+B;QAC/B,0BAA0B;QAC1B,8EAA8E;QAC9E,sBAAsB;QACtB,IAAI;QAEJ,gDAAgD;QAChD,KAAK,CAAC,KAAK,EAAE,CAAC;QAEd,+DAA+D;QAC/D,WAAW,GAAG,KAAK,CAAC,KAAK,EAAE,EAAE,WAAW,EAAE,CAAC;QAE3C,uDAAuD;QACvD,UAAU,GAAG,KAAK,CAAC,KAAK,EAAE,EAAE,WAAW,EAAE,CAAC;QAE1C,OAAO,UAAU,CAAC,CAAC,2CAA2C;IAChE,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,iBAAiB,GAAG,YAAY,CAAC;IACzC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
package/src/__tests__/index.ts
CHANGED
|
@@ -344,19 +344,27 @@ describe("run", () => {
|
|
|
344
344
|
const testEvent = defineEvent<{ message: string }>({ id: "test.event" });
|
|
345
345
|
const eventHandler = jest.fn();
|
|
346
346
|
let isReady = false;
|
|
347
|
+
let matched = false;
|
|
347
348
|
|
|
349
|
+
const dummyResource = defineResource({
|
|
350
|
+
id: "dummy",
|
|
351
|
+
init: async () => "dummy",
|
|
352
|
+
});
|
|
348
353
|
const task = defineTask({
|
|
349
354
|
id: "app",
|
|
350
355
|
on: "*",
|
|
351
|
-
|
|
352
|
-
|
|
356
|
+
dependencies: { dummyResource },
|
|
357
|
+
async run(event, { dummyResource }) {
|
|
358
|
+
if (dummyResource === "dummy") {
|
|
359
|
+
matched = true;
|
|
360
|
+
}
|
|
353
361
|
isReady && eventHandler();
|
|
354
362
|
},
|
|
355
363
|
});
|
|
356
364
|
|
|
357
365
|
const app = defineResource({
|
|
358
366
|
id: "app.resource",
|
|
359
|
-
register: [testEvent, task],
|
|
367
|
+
register: [testEvent, task, dummyResource],
|
|
360
368
|
dependencies: { task, testEvent },
|
|
361
369
|
async init(_, deps) {
|
|
362
370
|
isReady = true;
|
|
@@ -366,6 +374,7 @@ describe("run", () => {
|
|
|
366
374
|
|
|
367
375
|
await run(app);
|
|
368
376
|
expect(eventHandler).toHaveBeenCalled();
|
|
377
|
+
expect(matched).toBe(true);
|
|
369
378
|
});
|
|
370
379
|
});
|
|
371
380
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineTask,
|
|
3
|
+
defineResource,
|
|
4
|
+
defineMiddleware,
|
|
5
|
+
defineEvent,
|
|
6
|
+
} from "../../define";
|
|
7
|
+
import { symbols } from "../../defs";
|
|
8
|
+
import { getCallerFile } from "../../tools/getCallerFile";
|
|
9
|
+
|
|
10
|
+
describe("getCallerFile", () => {
|
|
11
|
+
it("should return the file name of the caller", () => {
|
|
12
|
+
function testFunction() {
|
|
13
|
+
return getCallerFile();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const callerFile = testFunction();
|
|
17
|
+
|
|
18
|
+
expect(callerFile).toBeDefined();
|
|
19
|
+
expect(callerFile).toContain("getCallerFile.test"); // we don't use .ts because for coverage it gets compiled to js
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("Should work with tasks, resources, middleware and events", () => {
|
|
23
|
+
const task = defineTask({
|
|
24
|
+
id: "task",
|
|
25
|
+
run: async () => {},
|
|
26
|
+
});
|
|
27
|
+
const resource = defineResource({
|
|
28
|
+
id: "resource",
|
|
29
|
+
init: async () => {},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const middleware = defineMiddleware({
|
|
33
|
+
id: "middleware",
|
|
34
|
+
run: async () => {},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const event = defineEvent({
|
|
38
|
+
id: "event",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(task[symbols.filePath]).toBeDefined();
|
|
42
|
+
expect(resource[symbols.filePath]).toBeDefined();
|
|
43
|
+
expect(middleware[symbols.filePath]).toBeDefined();
|
|
44
|
+
expect(event[symbols.filePath]).toBeDefined();
|
|
45
|
+
|
|
46
|
+
expect(task[symbols.filePath]).toContain("getCallerFile.test");
|
|
47
|
+
expect(resource[symbols.filePath]).toContain("getCallerFile.test");
|
|
48
|
+
expect(middleware[symbols.filePath]).toContain("getCallerFile.test");
|
|
49
|
+
expect(event[symbols.filePath]).toContain("getCallerFile.test");
|
|
50
|
+
});
|
|
51
|
+
});
|
package/src/define.ts
CHANGED
|
@@ -16,6 +16,9 @@ import {
|
|
|
16
16
|
symbolEvent,
|
|
17
17
|
} from "./defs";
|
|
18
18
|
import { Errors } from "./errors";
|
|
19
|
+
import { getCallerFile } from "./tools/getCallerFile";
|
|
20
|
+
|
|
21
|
+
// Helper function to get the caller file
|
|
19
22
|
|
|
20
23
|
export function defineTask<
|
|
21
24
|
Input = undefined,
|
|
@@ -27,6 +30,7 @@ export function defineTask<
|
|
|
27
30
|
): ITask<Input, Output, Deps, TOn> {
|
|
28
31
|
return {
|
|
29
32
|
[symbols.task]: true,
|
|
33
|
+
[symbols.filePath]: getCallerFile(),
|
|
30
34
|
id: taskConfig.id,
|
|
31
35
|
dependencies: taskConfig.dependencies || ({} as Deps),
|
|
32
36
|
middleware: taskConfig.middleware || [],
|
|
@@ -58,6 +62,7 @@ export function defineResource<
|
|
|
58
62
|
): IResource<TConfig, TValue, TDeps> {
|
|
59
63
|
return {
|
|
60
64
|
[symbols.resource]: true,
|
|
65
|
+
[symbols.filePath]: getCallerFile(),
|
|
61
66
|
id: constConfig.id,
|
|
62
67
|
dependencies: constConfig.dependencies,
|
|
63
68
|
dispose: constConfig.dispose,
|
|
@@ -93,6 +98,7 @@ export function defineEvent<TPayload = any>(
|
|
|
93
98
|
config: IEventDefinitionConfig<TPayload>
|
|
94
99
|
): IEventDefinition<TPayload> {
|
|
95
100
|
return {
|
|
101
|
+
[symbols.filePath]: getCallerFile(),
|
|
96
102
|
[symbolEvent]: true,
|
|
97
103
|
...config,
|
|
98
104
|
};
|
|
@@ -102,6 +108,7 @@ export function defineMiddleware<TDeps extends DependencyMapType = {}>(
|
|
|
102
108
|
config: IMiddlewareDefinition<TDeps>
|
|
103
109
|
): IMiddleware<TDeps> {
|
|
104
110
|
const object = {
|
|
111
|
+
[symbols.filePath]: getCallerFile(),
|
|
105
112
|
[symbols.middleware]: true,
|
|
106
113
|
...config,
|
|
107
114
|
dependencies: config.dependencies || ({} as TDeps),
|
package/src/defs.ts
CHANGED
|
@@ -9,6 +9,8 @@ export const symbolMiddlewareGlobal: unique symbol = Symbol(
|
|
|
9
9
|
"runner.middlewareGlobal"
|
|
10
10
|
);
|
|
11
11
|
|
|
12
|
+
export const symbolFilePath: unique symbol = Symbol("runner.filePath");
|
|
13
|
+
|
|
12
14
|
export const symbols = {
|
|
13
15
|
task: symbolTask,
|
|
14
16
|
resource: symbolResource,
|
|
@@ -16,6 +18,7 @@ export const symbols = {
|
|
|
16
18
|
event: symbolEvent,
|
|
17
19
|
middleware: symbolMiddleware,
|
|
18
20
|
middlewareGlobal: symbolMiddlewareGlobal,
|
|
21
|
+
filePath: symbolFilePath,
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
export interface IMeta {
|
package/src/globalResources.ts
CHANGED
|
@@ -7,6 +7,12 @@ import { TaskRunner } from "./models/TaskRunner";
|
|
|
7
7
|
const store = defineResource({
|
|
8
8
|
id: "global.store",
|
|
9
9
|
init: async (store: Store) => store,
|
|
10
|
+
meta: {
|
|
11
|
+
title: "Store",
|
|
12
|
+
description:
|
|
13
|
+
"A global store that can be used to store and retrieve tasks, resources, events and middleware",
|
|
14
|
+
tags: ["internal"],
|
|
15
|
+
},
|
|
10
16
|
});
|
|
11
17
|
|
|
12
18
|
export const globalResources = {
|
package/src/models/TaskRunner.ts
CHANGED
|
@@ -39,13 +39,7 @@ export class TaskRunner {
|
|
|
39
39
|
): Promise<TOutput | undefined> {
|
|
40
40
|
let runner = this.runnerStore.get(task.id);
|
|
41
41
|
if (!runner) {
|
|
42
|
-
|
|
43
|
-
const deps = taskDependencies || storeTask.computedDependencies;
|
|
44
|
-
|
|
45
|
-
runner = this.createRunnerWithMiddleware<TInput, TOutput, TDeps>(
|
|
46
|
-
task,
|
|
47
|
-
deps
|
|
48
|
-
);
|
|
42
|
+
runner = this.createRunnerWithMiddleware<TInput, TOutput, TDeps>(task);
|
|
49
43
|
|
|
50
44
|
this.runnerStore.set(task.id, runner);
|
|
51
45
|
}
|
|
@@ -140,10 +134,8 @@ export class TaskRunner {
|
|
|
140
134
|
TInput,
|
|
141
135
|
TOutput extends Promise<any>,
|
|
142
136
|
TDeps extends DependencyMapType
|
|
143
|
-
>(
|
|
144
|
-
|
|
145
|
-
taskDependencies: DependencyValuesType<{}>
|
|
146
|
-
) {
|
|
137
|
+
>(task: ITask<TInput, TOutput, TDeps>) {
|
|
138
|
+
const storeTask = this.store.tasks.get(task.id);
|
|
147
139
|
// this is the final next()
|
|
148
140
|
let next = async (input) => {
|
|
149
141
|
this.logger.debug(
|
|
@@ -154,7 +146,7 @@ export class TaskRunner {
|
|
|
154
146
|
task.id
|
|
155
147
|
);
|
|
156
148
|
|
|
157
|
-
return task.run.call(null, input,
|
|
149
|
+
return task.run.call(null, input, storeTask?.computedDependencies as any);
|
|
158
150
|
};
|
|
159
151
|
|
|
160
152
|
const existingMiddlewares = task.middleware;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function getCallerFile(): string | undefined {
|
|
2
|
+
const originalFunc = Error.prepareStackTrace;
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
const err = new Error();
|
|
6
|
+
let callerfile;
|
|
7
|
+
let currentfile;
|
|
8
|
+
|
|
9
|
+
// Safeguard prepareStackTrace
|
|
10
|
+
Error.prepareStackTrace = (err, stack) => stack;
|
|
11
|
+
|
|
12
|
+
const stack = err.stack as unknown as NodeJS.CallSite[];
|
|
13
|
+
|
|
14
|
+
// Don't know how to test this.
|
|
15
|
+
// if (stack.length < 3) {
|
|
16
|
+
// // We need at least 3 frames: current function, its caller, and one above
|
|
17
|
+
// return undefined;
|
|
18
|
+
// }
|
|
19
|
+
|
|
20
|
+
// Remove the first frame (getCallerFile itself)
|
|
21
|
+
stack.shift();
|
|
22
|
+
|
|
23
|
+
// Remove the second frame (the direct caller of getCallerFile)
|
|
24
|
+
currentfile = stack.shift()?.getFileName();
|
|
25
|
+
|
|
26
|
+
// The third frame (the caller above the immediate one)
|
|
27
|
+
callerfile = stack.shift()?.getFileName();
|
|
28
|
+
|
|
29
|
+
return callerfile; // Return the file name of the caller above
|
|
30
|
+
} finally {
|
|
31
|
+
Error.prepareStackTrace = originalFunc;
|
|
32
|
+
}
|
|
33
|
+
}
|