@modern-js/plugin 0.0.0-bundle-deps-202110124831 → 0.0.0-runtime-2021112193858
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/CHANGELOG.md +14 -2
- package/dist/js/modern/index.js +2 -3
- package/dist/js/modern/manager/async.js +3 -3
- package/dist/js/modern/manager/runner.js +1 -1
- package/dist/js/modern/manager/sync.js +3 -8
- package/dist/js/modern/waterfall/async.js +28 -34
- package/dist/js/modern/waterfall/sync.js +29 -36
- package/dist/js/modern/workflow/async.js +31 -30
- package/dist/js/modern/workflow/parallel.js +20 -29
- package/dist/js/modern/workflow/sync.js +20 -27
- package/dist/js/node/index.js +12 -25
- package/dist/js/node/manager/async.js +6 -6
- package/dist/js/node/manager/runner.js +2 -2
- package/dist/js/node/manager/sync.js +8 -14
- package/dist/js/node/waterfall/async.js +27 -34
- package/dist/js/node/waterfall/sync.js +29 -36
- package/dist/js/node/workflow/async.js +30 -30
- package/dist/js/node/workflow/parallel.js +19 -29
- package/dist/js/node/workflow/sync.js +19 -27
- package/dist/js/treeshaking/index.js +2 -3
- package/dist/js/treeshaking/manager/async.js +3 -3
- package/dist/js/treeshaking/manager/runner.js +1 -1
- package/dist/js/treeshaking/manager/sync.js +3 -8
- package/dist/js/treeshaking/waterfall/async.js +63 -108
- package/dist/js/treeshaking/waterfall/sync.js +36 -39
- package/dist/js/treeshaking/workflow/async.js +89 -88
- package/dist/js/treeshaking/workflow/parallel.js +42 -66
- package/dist/js/treeshaking/workflow/sync.js +50 -30
- package/dist/types/index.d.ts +2 -3
- package/dist/types/manager/runner.d.ts +1 -1
- package/dist/types/manager/sync.d.ts +2 -3
- package/dist/types/waterfall/async.d.ts +9 -5
- package/dist/types/waterfall/sync.d.ts +4 -4
- package/dist/types/workflow/async.d.ts +3 -2
- package/dist/types/workflow/parallel.d.ts +1 -1
- package/dist/types/workflow/sync.d.ts +1 -1
- package/node.d.ts +1 -1
- package/node.js +1 -1
- package/package.json +6 -5
- package/src/index.ts +2 -2
- package/src/manager/async.ts +1 -1
- package/src/manager/runner.ts +1 -1
- package/src/manager/sync.ts +12 -16
- package/src/waterfall/async.ts +34 -49
- package/src/waterfall/sync.ts +26 -45
- package/src/workflow/async.ts +29 -33
- package/src/workflow/parallel.ts +17 -35
- package/src/workflow/sync.ts +13 -35
- package/tests/async.test.ts +7 -3
- package/tests/pipeline.test.ts +4 -14
- package/tests/sync.test.ts +6 -2
- package/tests/waterfall.test.ts +1 -1
- package/dist/js/modern/asyncHooksImpl.js +0 -63
- package/dist/js/modern/asyncHooksInterface.js +0 -16
- package/dist/js/modern/context.js +0 -130
- package/dist/js/modern/counter.js +0 -40
- package/dist/js/modern/hook.js +0 -47
- package/dist/js/modern/pipeline/async.js +0 -97
- package/dist/js/modern/pipeline/index.js +0 -2
- package/dist/js/modern/pipeline/sync.js +0 -97
- package/dist/js/node/asyncHooksImpl.js +0 -82
- package/dist/js/node/asyncHooksInterface.js +0 -30
- package/dist/js/node/context.js +0 -164
- package/dist/js/node/counter.js +0 -52
- package/dist/js/node/hook.js +0 -57
- package/dist/js/node/pipeline/async.js +0 -110
- package/dist/js/node/pipeline/index.js +0 -31
- package/dist/js/node/pipeline/sync.js +0 -110
- package/dist/js/treeshaking/asyncHooksImpl.js +0 -65
- package/dist/js/treeshaking/asyncHooksInterface.js +0 -16
- package/dist/js/treeshaking/context.js +0 -137
- package/dist/js/treeshaking/counter.js +0 -74
- package/dist/js/treeshaking/hook.js +0 -51
- package/dist/js/treeshaking/pipeline/async.js +0 -165
- package/dist/js/treeshaking/pipeline/index.js +0 -2
- package/dist/js/treeshaking/pipeline/sync.js +0 -118
- package/dist/types/asyncHooksImpl.d.ts +0 -10
- package/dist/types/asyncHooksInterface.d.ts +0 -21
- package/dist/types/context.d.ts +0 -47
- package/dist/types/counter.d.ts +0 -22
- package/dist/types/hook.d.ts +0 -13
- package/dist/types/pipeline/async.d.ts +0 -35
- package/dist/types/pipeline/index.d.ts +0 -2
- package/dist/types/pipeline/sync.d.ts +0 -37
- package/src/asyncHooksImpl.ts +0 -64
- package/src/asyncHooksInterface.ts +0 -34
- package/src/context.ts +0 -184
- package/src/counter.ts +0 -78
- package/src/hook.ts +0 -46
- package/src/pipeline/async.ts +0 -155
- package/src/pipeline/index.ts +0 -2
- package/src/pipeline/sync.ts +0 -152
- package/tests/context.test.ts +0 -114
- package/tests/counter.test.ts +0 -31
- package/tests/hook.test.ts +0 -113
package/src/waterfall/sync.ts
CHANGED
@@ -1,26 +1,22 @@
|
|
1
|
-
import { createCounter } from '../counter';
|
2
1
|
import {
|
3
|
-
Hooks,
|
4
|
-
runHooks,
|
5
|
-
fromContainer,
|
6
2
|
Container,
|
7
|
-
createContainer,
|
8
3
|
useContainer,
|
9
|
-
|
4
|
+
createPipeline,
|
5
|
+
Middleware,
|
6
|
+
} from 'farrow-pipeline';
|
10
7
|
|
11
8
|
const WATERFALL_SYMBOL = Symbol('WATERFALL_SYMBOL');
|
12
9
|
|
13
|
-
|
14
|
-
export type
|
15
|
-
export type BrookInput<I = unknown> = Brook<I> | { brook: Brook<I> };
|
10
|
+
export type Brook<I = unknown> = (I: I) => I;
|
11
|
+
export type BrookInput<I = unknown> = Brook<I> | { middleware: Brook<I> };
|
16
12
|
export type Brooks<I = unknown> = Brook<I>[];
|
17
13
|
export type BrookInputs<I = unknown> = BrookInput<I>[];
|
18
14
|
|
19
15
|
export const getBrook = <I>(input: BrookInput<I>) => {
|
20
16
|
if (typeof input === 'function') {
|
21
17
|
return input;
|
22
|
-
} else if (input && typeof input.
|
23
|
-
return input.
|
18
|
+
} else if (input && typeof input.middleware === 'function') {
|
19
|
+
return input.middleware;
|
24
20
|
}
|
25
21
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string,@typescript-eslint/restrict-template-expressions
|
26
22
|
throw new Error(`${input} is not a Brook or { brook: Brook }`);
|
@@ -35,7 +31,7 @@ export type RunWaterfallOptions<I = unknown> = {
|
|
35
31
|
export type Waterfall<I = void> = {
|
36
32
|
run: (input: I, options?: RunWaterfallOptions<I>) => I;
|
37
33
|
use: (...I: BrookInputs<I>) => Waterfall<I>;
|
38
|
-
|
34
|
+
middleware: Brook<I>;
|
39
35
|
[WATERFALL_SYMBOL]: true;
|
40
36
|
};
|
41
37
|
|
@@ -77,53 +73,38 @@ export type Waterfalls2Runners<PS extends WaterfallRecord | void> = {
|
|
77
73
|
|
78
74
|
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
79
75
|
export const createWaterfall = <I = void>(): Waterfall<I> => {
|
80
|
-
const
|
81
|
-
|
82
|
-
const createCurrentRunner = (hooks: Hooks) =>
|
83
|
-
createCounter<I, I>((index, input, next) => {
|
84
|
-
if (index >= middlewares.length) {
|
85
|
-
return input;
|
86
|
-
}
|
87
|
-
|
88
|
-
return runHooks(() => next(middlewares[index](input) || input), hooks);
|
89
|
-
});
|
90
|
-
|
91
|
-
const currentContainer = createContainer();
|
92
|
-
const currentHooks = fromContainer(currentContainer);
|
93
|
-
const currentRunner = createCurrentRunner(currentHooks);
|
76
|
+
const pipeline = createPipeline<I, I>();
|
94
77
|
|
95
|
-
const use: Waterfall<I>['use'] = (...
|
96
|
-
|
78
|
+
const use: Waterfall<I>['use'] = (...brooks) => {
|
79
|
+
pipeline.use(...brooks.map(getBrook).map(mapBrookToMiddleware));
|
97
80
|
return waterfall;
|
98
81
|
};
|
99
82
|
|
100
|
-
const run: Waterfall<I>['run'] = (input, options) =>
|
101
|
-
|
102
|
-
|
103
|
-
container === currentContainer ? currentHooks : fromContainer(container);
|
104
|
-
const runner =
|
105
|
-
container === currentContainer
|
106
|
-
? currentRunner
|
107
|
-
: createCurrentRunner(hooks);
|
83
|
+
const run: Waterfall<I>['run'] = (input, options) =>
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
85
|
+
pipeline.run(input, { ...options, onLast: input => input });
|
108
86
|
|
109
|
-
|
110
|
-
};
|
111
|
-
|
112
|
-
const brook: Waterfall<I>['brook'] = input => {
|
87
|
+
const middleware: Waterfall<I>['middleware'] = input => {
|
113
88
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
114
89
|
const container = useContainer();
|
115
|
-
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
91
|
+
return pipeline.run(input, { container, onLast: input => input });
|
116
92
|
};
|
117
93
|
|
118
|
-
const waterfall = {
|
94
|
+
const waterfall: Waterfall<I> = {
|
95
|
+
...pipeline,
|
119
96
|
use,
|
120
97
|
run,
|
121
|
-
|
122
|
-
[WATERFALL_SYMBOL]: true
|
98
|
+
middleware,
|
99
|
+
[WATERFALL_SYMBOL]: true,
|
123
100
|
};
|
124
|
-
|
125
101
|
return waterfall;
|
126
102
|
};
|
127
103
|
|
128
104
|
export const isWaterfall = (input: any): input is Waterfall<any> =>
|
129
105
|
Boolean(input?.[WATERFALL_SYMBOL]);
|
106
|
+
|
107
|
+
const mapBrookToMiddleware =
|
108
|
+
<I>(brook: Brook<I>): Middleware<I, I> =>
|
109
|
+
(input, next) =>
|
110
|
+
next(brook(input));
|
package/src/workflow/async.ts
CHANGED
@@ -1,14 +1,13 @@
|
|
1
|
-
import {
|
2
|
-
import { Hooks, runHooks, fromContainer, createContainer } from '../context';
|
1
|
+
import { MaybeAsync, createAsyncPipeline, Middleware } from 'farrow-pipeline';
|
3
2
|
import type { RunWorkflowOptions } from './sync';
|
4
3
|
|
5
4
|
const ASYNC_WORKFLOW_SYMBOL = Symbol('ASYNC_WORKFLOW_SYMBOL');
|
6
5
|
|
7
|
-
export type AsyncWorker<I, O> = (I: I) =>
|
6
|
+
export type AsyncWorker<I, O> = (I: I) => MaybeAsync<O>;
|
8
7
|
export type AsyncWorkers<I, O> = AsyncWorker<I, O>[];
|
9
8
|
|
10
9
|
export type AsyncWorkflow<I, O> = {
|
11
|
-
run: (input: I, options?: RunWorkflowOptions) =>
|
10
|
+
run: (input: I, options?: RunWorkflowOptions) => MaybeAsync<O[]>;
|
12
11
|
use: (...I: AsyncWorkers<I, O>) => AsyncWorkflow<I, O>;
|
13
12
|
[ASYNC_WORKFLOW_SYMBOL]: true;
|
14
13
|
};
|
@@ -54,43 +53,25 @@ export const createAsyncWorkflow = <I = void, O = unknown>(): AsyncWorkflow<
|
|
54
53
|
I,
|
55
54
|
O
|
56
55
|
> => {
|
57
|
-
const
|
58
|
-
|
59
|
-
const createCurrentRunner = (hooks: Hooks) =>
|
60
|
-
createAsyncCounter<I, O[]>(async (index, input, next) => {
|
61
|
-
if (index >= middlewares.length) {
|
62
|
-
return [];
|
63
|
-
}
|
64
|
-
|
65
|
-
const middleware = middlewares[index];
|
66
|
-
return runHooks(async () => {
|
67
|
-
const result = await middleware(input);
|
68
|
-
const rest = await next(input);
|
69
|
-
return [result, ...rest];
|
70
|
-
}, hooks);
|
71
|
-
});
|
72
|
-
const currentContainer = createContainer();
|
73
|
-
const currentHooks = fromContainer(currentContainer);
|
74
|
-
const currentRunner = createCurrentRunner(currentHooks);
|
56
|
+
const pipeline = createAsyncPipeline<I, O[]>();
|
75
57
|
|
76
58
|
const use: AsyncWorkflow<I, O>['use'] = (...input) => {
|
77
|
-
|
59
|
+
pipeline.use(...input.map(mapAsyncWorkerToAsyncMiddleware));
|
78
60
|
return workflow;
|
79
61
|
};
|
80
62
|
|
81
63
|
const run: AsyncWorkflow<I, O>['run'] = async (input, options) => {
|
82
|
-
const
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
return runner.start(input);
|
64
|
+
const result = pipeline.run(input, { ...options, onLast: () => [] });
|
65
|
+
if (isPromise(result)) {
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow,promise/prefer-await-to-then
|
67
|
+
return result.then(result => result.filter(Boolean));
|
68
|
+
} else {
|
69
|
+
return result.filter(Boolean);
|
70
|
+
}
|
91
71
|
};
|
92
72
|
|
93
|
-
const workflow = {
|
73
|
+
const workflow: AsyncWorkflow<I, O> = {
|
74
|
+
...pipeline,
|
94
75
|
use,
|
95
76
|
run,
|
96
77
|
[ASYNC_WORKFLOW_SYMBOL]: true as const,
|
@@ -98,3 +79,18 @@ export const createAsyncWorkflow = <I = void, O = unknown>(): AsyncWorkflow<
|
|
98
79
|
|
99
80
|
return workflow;
|
100
81
|
};
|
82
|
+
|
83
|
+
const mapAsyncWorkerToAsyncMiddleware =
|
84
|
+
<I, O>(worker: AsyncWorker<I, O>): Middleware<I, MaybeAsync<O[]>> =>
|
85
|
+
async (input, next) =>
|
86
|
+
[await worker(input), ...(await next(input))];
|
87
|
+
|
88
|
+
function isPromise(obj: any): obj is Promise<any> {
|
89
|
+
/* eslint-disable promise/prefer-await-to-then */
|
90
|
+
return (
|
91
|
+
Boolean(obj) &&
|
92
|
+
(typeof obj === 'object' || typeof obj === 'function') &&
|
93
|
+
typeof obj.then === 'function'
|
94
|
+
);
|
95
|
+
/* eslint-enable promise/prefer-await-to-then */
|
96
|
+
}
|
package/src/workflow/parallel.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import { AsyncWorker, AsyncWorkers } from './async';
|
1
|
+
import { MaybeAsync, createPipeline, Middleware } from 'farrow-pipeline';
|
2
|
+
import type { AsyncWorker, AsyncWorkers } from './async';
|
4
3
|
import type { RunWorkflowOptions } from './sync';
|
5
4
|
|
6
5
|
const PARALLEL_WORKFLOW_SYMBOL = Symbol('PARALLEL_WORKFLOW_SYMBOL');
|
@@ -69,47 +68,30 @@ export const createParallelWorkflow = <
|
|
69
68
|
I = void,
|
70
69
|
O = unknown,
|
71
70
|
>(): ParallelWorkflow<I, O> => {
|
72
|
-
const
|
73
|
-
|
74
|
-
const createCurrentRunner = (hooks: Hooks) =>
|
75
|
-
createAsyncCounter<I, O[]>((index, input, next) => {
|
76
|
-
if (index >= middlewares.length) {
|
77
|
-
return Promise.resolve<any[]>([]);
|
78
|
-
}
|
79
|
-
|
80
|
-
const middleware = middlewares[index];
|
81
|
-
return runHooks(
|
82
|
-
async () => Promise.all([middleware(input), ...(await next(input))]),
|
83
|
-
hooks,
|
84
|
-
);
|
85
|
-
});
|
86
|
-
|
87
|
-
const currentContainer = createContainer();
|
88
|
-
const currentHooks = fromContainer(currentContainer);
|
89
|
-
const currentRunner = createCurrentRunner(currentHooks);
|
71
|
+
const pipeline = createPipeline<I, MaybeAsync<O>[]>();
|
90
72
|
|
91
73
|
const use: ParallelWorkflow<I, O>['use'] = (...input) => {
|
92
|
-
|
74
|
+
pipeline.use(...input.map(mapParallelWorkerToAsyncMiddleware));
|
93
75
|
return workflow;
|
94
76
|
};
|
95
77
|
|
96
|
-
const run: ParallelWorkflow<I, O>['run'] = async (input, options) =>
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
container === currentContainer
|
102
|
-
? currentRunner
|
103
|
-
: createCurrentRunner(hooks);
|
104
|
-
|
105
|
-
return runner.start(input);
|
106
|
-
};
|
78
|
+
const run: ParallelWorkflow<I, O>['run'] = async (input, options) =>
|
79
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
80
|
+
Promise.all(pipeline.run(input, { ...options, onLast: () => [] })).then(
|
81
|
+
result => result.filter(Boolean),
|
82
|
+
);
|
107
83
|
|
108
|
-
const workflow = {
|
109
|
-
|
84
|
+
const workflow: ParallelWorkflow<I, O> = {
|
85
|
+
...pipeline,
|
110
86
|
run,
|
87
|
+
use,
|
111
88
|
[PARALLEL_WORKFLOW_SYMBOL]: true as const,
|
112
89
|
};
|
113
90
|
|
114
91
|
return workflow;
|
115
92
|
};
|
93
|
+
|
94
|
+
const mapParallelWorkerToAsyncMiddleware =
|
95
|
+
<I, O>(worker: AsyncWorker<I, O>): Middleware<I, MaybeAsync<O>[]> =>
|
96
|
+
(input, next) =>
|
97
|
+
[worker(input), ...next(input)];
|
package/src/workflow/sync.ts
CHANGED
@@ -1,11 +1,4 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
Hooks,
|
4
|
-
runHooks,
|
5
|
-
fromContainer,
|
6
|
-
Container,
|
7
|
-
createContainer,
|
8
|
-
} from '../context';
|
1
|
+
import { Container, createPipeline, Middleware } from 'farrow-pipeline';
|
9
2
|
|
10
3
|
const WORKFLOW_SYMBOL = Symbol('WORKFLOW_SYMBOL');
|
11
4
|
|
@@ -58,40 +51,20 @@ export type Workflows2Runners<PS extends WorkflowRecord | void> = {
|
|
58
51
|
|
59
52
|
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
60
53
|
export const createWorkflow = <I = void, O = unknown>(): Workflow<I, O> => {
|
61
|
-
const
|
62
|
-
|
63
|
-
const createCurrentRunner = (hooks: Hooks) =>
|
64
|
-
createCounter<I, O[]>((index, input, next) => {
|
65
|
-
if (index >= middlewares.length) {
|
66
|
-
return [];
|
67
|
-
}
|
68
|
-
|
69
|
-
const middleware = middlewares[index];
|
70
|
-
return runHooks(() => [middleware(input), ...next(input)], hooks);
|
71
|
-
});
|
72
|
-
|
73
|
-
const currentContainer = createContainer();
|
74
|
-
const currentHooks = fromContainer(currentContainer);
|
75
|
-
const currentRunner = createCurrentRunner(currentHooks);
|
54
|
+
const pipeline = createPipeline<I, O[]>();
|
76
55
|
|
77
56
|
const use: Workflow<I, O>['use'] = (...input) => {
|
78
|
-
|
57
|
+
pipeline.use(...input.map(mapWorkerToMiddleware));
|
79
58
|
return workflow;
|
80
59
|
};
|
81
60
|
|
82
|
-
const run: Workflow<I, O>['run'] = (input, options) => {
|
83
|
-
const
|
84
|
-
|
85
|
-
container === currentContainer ? currentHooks : fromContainer(container);
|
86
|
-
const runner =
|
87
|
-
container === currentContainer
|
88
|
-
? currentRunner
|
89
|
-
: createCurrentRunner(hooks);
|
90
|
-
|
91
|
-
return runner.start(input);
|
61
|
+
const run: Workflow<I, O>['run'] = async (input, options) => {
|
62
|
+
const result = pipeline.run(input, { ...options, onLast: () => [] });
|
63
|
+
return result.filter(Boolean);
|
92
64
|
};
|
93
65
|
|
94
|
-
const workflow = {
|
66
|
+
const workflow: Workflow<I, O> = {
|
67
|
+
...pipeline,
|
95
68
|
use,
|
96
69
|
run,
|
97
70
|
[WORKFLOW_SYMBOL]: true as const,
|
@@ -102,3 +75,8 @@ export const createWorkflow = <I = void, O = unknown>(): Workflow<I, O> => {
|
|
102
75
|
|
103
76
|
export const isWorkflow = (input: any): input is Workflow<unknown, unknown> =>
|
104
77
|
Boolean(input?.[WORKFLOW_SYMBOL]);
|
78
|
+
|
79
|
+
const mapWorkerToMiddleware =
|
80
|
+
<I, O>(worker: Worker<I, O>): Middleware<I, O[]> =>
|
81
|
+
(input, next) =>
|
82
|
+
[worker(input), ...next(input)];
|
package/tests/async.test.ts
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
2
2
|
/* eslint-disable max-lines */
|
3
|
+
import { enable, disable } from 'farrow-pipeline/asyncHooks.node';
|
4
|
+
import {
|
5
|
+
createPipeline,
|
6
|
+
createAsyncPipeline,
|
7
|
+
createContext,
|
8
|
+
createContainer,
|
9
|
+
} from 'farrow-pipeline';
|
3
10
|
import { main } from './fixtures/async/core';
|
4
11
|
import foo from './fixtures/async/base/foo';
|
5
12
|
import bar, { getBar } from './fixtures/async/base/bar';
|
6
13
|
import dFoo from './fixtures/async/dynamic/foo';
|
7
14
|
import dBar, { getNumber } from './fixtures/async/dynamic/bar';
|
8
15
|
import { sleep } from './helpers';
|
9
|
-
import { createContext, createContainer } from '@/context';
|
10
16
|
import { createManager, createAsyncManager, useRunner } from '@/manager';
|
11
|
-
import { enable, disable } from '@/asyncHooksImpl';
|
12
17
|
import { createWaterfall, createAsyncWaterfall } from '@/waterfall';
|
13
|
-
import { createPipeline, createAsyncPipeline } from '@/pipeline';
|
14
18
|
import {
|
15
19
|
createWorkflow,
|
16
20
|
createAsyncWorkflow,
|
package/tests/pipeline.test.ts
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
2
2
|
/* eslint-disable max-lines */
|
3
|
+
import * as asyncHooksImpl from 'farrow-pipeline/asyncHooks.node';
|
3
4
|
import { sleep } from './helpers';
|
4
5
|
import {
|
5
6
|
createContext,
|
6
7
|
createContainer,
|
7
8
|
createPipeline,
|
8
9
|
createAsyncPipeline,
|
9
|
-
useAsyncPipeline,
|
10
10
|
usePipeline,
|
11
11
|
useContainer,
|
12
12
|
isPipeline,
|
13
|
-
isAsyncPipeline,
|
14
13
|
} from '@/index';
|
15
|
-
import * as asyncHooksImpl from '@/asyncHooksImpl';
|
16
14
|
|
17
15
|
describe('createPipeline', () => {
|
18
16
|
it('basic usage', async () => {
|
@@ -531,7 +529,7 @@ describe('createPipeline', () => {
|
|
531
529
|
pipeline0.use(async input => `${input} from pipeline0`);
|
532
530
|
|
533
531
|
pipeline1.use(async input => {
|
534
|
-
const runPipeline1 =
|
532
|
+
const runPipeline1 = usePipeline(pipeline0);
|
535
533
|
|
536
534
|
const text = await runPipeline1(' pipeline1');
|
537
535
|
|
@@ -543,15 +541,6 @@ describe('createPipeline', () => {
|
|
543
541
|
expect(result).toEqual(`run pipeline1 from pipeline0`);
|
544
542
|
});
|
545
543
|
|
546
|
-
it('isAsyncPipeline', () => {
|
547
|
-
const pipeline = createAsyncPipeline();
|
548
|
-
|
549
|
-
expect(isAsyncPipeline(pipeline)).toBeTruthy();
|
550
|
-
expect(isAsyncPipeline({})).toBeFalsy();
|
551
|
-
expect(isAsyncPipeline('test')).toBeFalsy();
|
552
|
-
expect(isAsyncPipeline(null)).toBeFalsy();
|
553
|
-
});
|
554
|
-
|
555
544
|
it('support hooks', async () => {
|
556
545
|
const pipeline = createAsyncPipeline<number, number>();
|
557
546
|
|
@@ -599,7 +588,8 @@ describe('createPipeline', () => {
|
|
599
588
|
it('should throw error when all middlewares calling next() and onLast is not exist', async () => {
|
600
589
|
const pipeline = createAsyncPipeline<number, number>();
|
601
590
|
|
602
|
-
|
591
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
592
|
+
pipeline.use((input, next) => Promise.resolve().then(() => next(input)));
|
603
593
|
|
604
594
|
await expect(() => pipeline.run(0)).rejects.toThrowError();
|
605
595
|
});
|
package/tests/sync.test.ts
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
2
2
|
/* eslint-disable max-lines */
|
3
|
+
import {
|
4
|
+
createPipeline,
|
5
|
+
createAsyncPipeline,
|
6
|
+
createContext,
|
7
|
+
createContainer,
|
8
|
+
} from 'farrow-pipeline';
|
3
9
|
import { main } from './fixtures/sync/core';
|
4
10
|
import foo from './fixtures/sync/base/foo';
|
5
11
|
import bar, { getBar } from './fixtures/sync/base/bar';
|
6
12
|
import dFoo from './fixtures/sync/dynamic/foo';
|
7
13
|
import dBar, { getNumber, setNumber } from './fixtures/sync/dynamic/bar';
|
8
|
-
import { createContext, createContainer } from '@/context';
|
9
14
|
import { createManager, createAsyncManager, useRunner } from '@/manager';
|
10
15
|
import { createWaterfall, createAsyncWaterfall } from '@/waterfall';
|
11
|
-
import { createPipeline, createAsyncPipeline } from '@/pipeline';
|
12
16
|
import {
|
13
17
|
createWorkflow,
|
14
18
|
createAsyncWorkflow,
|
package/tests/waterfall.test.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { enable, disable } from 'farrow-pipeline/asyncHooks.node';
|
1
2
|
import { sleep } from './helpers';
|
2
3
|
import {
|
3
4
|
createWaterfall,
|
@@ -7,7 +8,6 @@ import {
|
|
7
8
|
isWaterfall,
|
8
9
|
isAsyncWaterfall,
|
9
10
|
} from '@/index';
|
10
|
-
import { enable, disable } from '@/asyncHooksImpl';
|
11
11
|
|
12
12
|
describe('waterfall', () => {
|
13
13
|
it('base usage', () => {
|
@@ -1,63 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright Lucifier129 and other contributors.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file at
|
6
|
-
* https://github.com/farrow-js/farrow/blob/master/LICENSE
|
7
|
-
*
|
8
|
-
*/
|
9
|
-
import NodeAsyncHooks from 'async_hooks';
|
10
|
-
import * as asyncHooksInterface from "./asyncHooksInterface";
|
11
|
-
|
12
|
-
const createAsyncHooks = () => {
|
13
|
-
const store = new Map(); // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
14
|
-
|
15
|
-
const hooks = NodeAsyncHooks.createHook({
|
16
|
-
init: (asyncId, _, triggerAsyncId) => {
|
17
|
-
if (store.has(triggerAsyncId)) {
|
18
|
-
const value = store.get(triggerAsyncId);
|
19
|
-
store.set(asyncId, value);
|
20
|
-
}
|
21
|
-
},
|
22
|
-
destroy: asyncId => {
|
23
|
-
if (store.has(asyncId)) {
|
24
|
-
store.delete(asyncId);
|
25
|
-
}
|
26
|
-
}
|
27
|
-
});
|
28
|
-
|
29
|
-
const set = value => {
|
30
|
-
store.set(NodeAsyncHooks.executionAsyncId(), value);
|
31
|
-
};
|
32
|
-
|
33
|
-
const get = () => store.get(NodeAsyncHooks.executionAsyncId());
|
34
|
-
|
35
|
-
const enable = () => {
|
36
|
-
hooks.enable();
|
37
|
-
};
|
38
|
-
|
39
|
-
const disable = () => {
|
40
|
-
hooks.disable();
|
41
|
-
store.clear();
|
42
|
-
};
|
43
|
-
|
44
|
-
return {
|
45
|
-
enable,
|
46
|
-
disable,
|
47
|
-
set,
|
48
|
-
get
|
49
|
-
};
|
50
|
-
};
|
51
|
-
|
52
|
-
export const enable = () => {
|
53
|
-
const hooks = createAsyncHooks();
|
54
|
-
disable();
|
55
|
-
asyncHooksInterface.impl(hooks);
|
56
|
-
hooks.enable();
|
57
|
-
};
|
58
|
-
export const disable = () => {
|
59
|
-
var _asyncHooksInterface$;
|
60
|
-
|
61
|
-
(_asyncHooksInterface$ = asyncHooksInterface.asyncHooks) === null || _asyncHooksInterface$ === void 0 ? void 0 : _asyncHooksInterface$.disable();
|
62
|
-
asyncHooksInterface.reset();
|
63
|
-
};
|
@@ -1,16 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright Lucifier129 and other contributors.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file at
|
6
|
-
* https://github.com/farrow-js/farrow/blob/master/LICENSE
|
7
|
-
*
|
8
|
-
*/
|
9
|
-
// eslint-disable-next-line import/no-mutable-exports
|
10
|
-
export let asyncHooks;
|
11
|
-
export const impl = implimentations => {
|
12
|
-
asyncHooks = implimentations;
|
13
|
-
};
|
14
|
-
export const reset = () => {
|
15
|
-
asyncHooks = undefined;
|
16
|
-
};
|
@@ -1,130 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright Lucifier129 and other contributors.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file at
|
6
|
-
* https://github.com/farrow-js/farrow/blob/master/LICENSE
|
7
|
-
*
|
8
|
-
*/
|
9
|
-
import { createHooks } from "./hook";
|
10
|
-
const ContextSymbol = Symbol('Context');
|
11
|
-
export const isContext = input => Object.prototype.hasOwnProperty.call(input, ContextSymbol);
|
12
|
-
export const assertContext = input => {
|
13
|
-
if (!isContext(input)) {
|
14
|
-
throw new Error(`Expected Context, but received ${input}`);
|
15
|
-
}
|
16
|
-
};
|
17
|
-
export const createContext = value => {
|
18
|
-
const id = Symbol('ContextID'); // eslint-disable-next-line @typescript-eslint/no-shadow
|
19
|
-
|
20
|
-
const create = value => {
|
21
|
-
const use = () => {
|
22
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
23
|
-
const container = useContainer();
|
24
|
-
return Object.seal({
|
25
|
-
get value() {
|
26
|
-
return container.read(Context);
|
27
|
-
},
|
28
|
-
|
29
|
-
set value(v) {
|
30
|
-
container.write(Context, v);
|
31
|
-
}
|
32
|
-
|
33
|
-
});
|
34
|
-
};
|
35
|
-
|
36
|
-
const get = () => {
|
37
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
38
|
-
const container = useContainer();
|
39
|
-
return container.read(Context);
|
40
|
-
};
|
41
|
-
|
42
|
-
const set = v => {
|
43
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
44
|
-
const container = useContainer();
|
45
|
-
container.write(Context, v);
|
46
|
-
};
|
47
|
-
|
48
|
-
const assert = () => {
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
50
|
-
const value = get();
|
51
|
-
|
52
|
-
if (value === null || value === undefined) {
|
53
|
-
throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
54
|
-
`Expected value is not null or undefined, but got: ${value}`);
|
55
|
-
}
|
56
|
-
|
57
|
-
return value;
|
58
|
-
};
|
59
|
-
|
60
|
-
const Context = {
|
61
|
-
id,
|
62
|
-
[ContextSymbol]: value,
|
63
|
-
create,
|
64
|
-
use,
|
65
|
-
get,
|
66
|
-
set,
|
67
|
-
assert
|
68
|
-
};
|
69
|
-
return Context;
|
70
|
-
};
|
71
|
-
|
72
|
-
return create(value);
|
73
|
-
};
|
74
|
-
export const ContainerSymbol = Symbol('Container'); // eslint-disable-next-line @typescript-eslint/no-redeclare
|
75
|
-
|
76
|
-
export const isContainer = input => Boolean(input === null || input === void 0 ? void 0 : input[ContainerSymbol]);
|
77
|
-
export const assertContainer = input => {
|
78
|
-
if (!isContainer(input)) {
|
79
|
-
throw new Error(`Expected Context, but received ${input}`);
|
80
|
-
}
|
81
|
-
};
|
82
|
-
|
83
|
-
const createContextMap = storage => {
|
84
|
-
const contextMap = new Map();
|
85
|
-
Object.values(storage).forEach(context => {
|
86
|
-
contextMap.set(context.id, context);
|
87
|
-
});
|
88
|
-
return contextMap;
|
89
|
-
};
|
90
|
-
|
91
|
-
export const createContainer = (ContextStorage = {}) => {
|
92
|
-
const contextMap = createContextMap(ContextStorage);
|
93
|
-
|
94
|
-
const read = context => {
|
95
|
-
const target = contextMap.get(context.id);
|
96
|
-
|
97
|
-
if (target) {
|
98
|
-
return target[ContextSymbol];
|
99
|
-
}
|
100
|
-
|
101
|
-
return context[ContextSymbol];
|
102
|
-
};
|
103
|
-
|
104
|
-
const write = (context, value) => {
|
105
|
-
contextMap.set(context.id, context.create(value));
|
106
|
-
};
|
107
|
-
|
108
|
-
const container = Object.freeze({
|
109
|
-
[ContainerSymbol]: true,
|
110
|
-
read,
|
111
|
-
write
|
112
|
-
});
|
113
|
-
return container;
|
114
|
-
};
|
115
|
-
const {
|
116
|
-
run,
|
117
|
-
hooks
|
118
|
-
} = createHooks({
|
119
|
-
useContainer: () => {
|
120
|
-
throw new Error(`Can't call useContainer out of scope, it should be placed on top of the function`);
|
121
|
-
}
|
122
|
-
});
|
123
|
-
export const runHooks = run;
|
124
|
-
export const {
|
125
|
-
useContainer
|
126
|
-
} = hooks;
|
127
|
-
export const fromContainer = container => ({
|
128
|
-
useContainer: () => container
|
129
|
-
});
|
130
|
-
export const runWithContainer = (f, container) => runHooks(f, fromContainer(container));
|