@modern-js/plugin 1.0.0 → 1.1.2

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.
Files changed (107) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +1 -4
  3. package/dist/js/modern/index.js +2 -3
  4. package/dist/js/modern/manager/async.js +1 -1
  5. package/dist/js/modern/manager/runner.js +1 -1
  6. package/dist/js/modern/manager/sync.js +1 -6
  7. package/dist/js/modern/waterfall/async.js +28 -34
  8. package/dist/js/modern/waterfall/sync.js +29 -36
  9. package/dist/js/modern/workflow/async.js +31 -30
  10. package/dist/js/modern/workflow/parallel.js +20 -29
  11. package/dist/js/modern/workflow/sync.js +20 -27
  12. package/dist/js/node/index.js +12 -25
  13. package/dist/js/node/manager/async.js +4 -4
  14. package/dist/js/node/manager/runner.js +2 -2
  15. package/dist/js/node/manager/sync.js +6 -12
  16. package/dist/js/node/waterfall/async.js +27 -34
  17. package/dist/js/node/waterfall/sync.js +29 -36
  18. package/dist/js/node/workflow/async.js +30 -30
  19. package/dist/js/node/workflow/parallel.js +19 -29
  20. package/dist/js/node/workflow/sync.js +19 -27
  21. package/dist/js/treeshaking/index.js +2 -3
  22. package/dist/js/treeshaking/manager/async.js +1 -1
  23. package/dist/js/treeshaking/manager/runner.js +1 -1
  24. package/dist/js/treeshaking/manager/sync.js +1 -6
  25. package/dist/js/treeshaking/waterfall/async.js +63 -108
  26. package/dist/js/treeshaking/waterfall/sync.js +36 -39
  27. package/dist/js/treeshaking/workflow/async.js +89 -88
  28. package/dist/js/treeshaking/workflow/parallel.js +42 -66
  29. package/dist/js/treeshaking/workflow/sync.js +50 -30
  30. package/dist/types/index.d.ts +2 -3
  31. package/dist/types/manager/runner.d.ts +1 -1
  32. package/dist/types/manager/sync.d.ts +2 -3
  33. package/dist/types/waterfall/async.d.ts +9 -5
  34. package/dist/types/waterfall/sync.d.ts +4 -4
  35. package/dist/types/workflow/async.d.ts +3 -2
  36. package/dist/types/workflow/parallel.d.ts +1 -1
  37. package/dist/types/workflow/sync.d.ts +1 -1
  38. package/node.d.ts +1 -1
  39. package/node.js +1 -1
  40. package/package.json +5 -4
  41. package/src/index.ts +2 -2
  42. package/src/manager/async.ts +1 -1
  43. package/src/manager/runner.ts +1 -1
  44. package/src/manager/sync.ts +12 -16
  45. package/src/waterfall/async.ts +34 -49
  46. package/src/waterfall/sync.ts +26 -45
  47. package/src/workflow/async.ts +29 -33
  48. package/src/workflow/parallel.ts +17 -35
  49. package/src/workflow/sync.ts +13 -35
  50. package/tests/.eslintrc.js +6 -0
  51. package/tests/async.test.ts +7 -3
  52. package/tests/fixtures/async/base/foo.ts +0 -1
  53. package/tests/fixtures/async/base/fooManager.ts +1 -1
  54. package/tests/fixtures/async/core/index.ts +1 -1
  55. package/tests/fixtures/async/dynamic/bar.ts +0 -1
  56. package/tests/fixtures/async/dynamic/foo.ts +1 -1
  57. package/tests/fixtures/sync/base/foo.ts +0 -2
  58. package/tests/fixtures/sync/base/fooManager.ts +1 -1
  59. package/tests/fixtures/sync/core/index.ts +1 -1
  60. package/tests/fixtures/sync/dynamic/bar.ts +0 -1
  61. package/tests/fixtures/sync/dynamic/foo.ts +1 -1
  62. package/tests/pipeline.test.ts +4 -15
  63. package/tests/sync.test.ts +6 -2
  64. package/tests/waterfall.test.ts +1 -2
  65. package/dist/js/modern/asyncHooksImpl.js +0 -63
  66. package/dist/js/modern/asyncHooksInterface.js +0 -16
  67. package/dist/js/modern/context.js +0 -130
  68. package/dist/js/modern/counter.js +0 -40
  69. package/dist/js/modern/hook.js +0 -47
  70. package/dist/js/modern/pipeline/async.js +0 -97
  71. package/dist/js/modern/pipeline/index.js +0 -2
  72. package/dist/js/modern/pipeline/sync.js +0 -97
  73. package/dist/js/node/asyncHooksImpl.js +0 -82
  74. package/dist/js/node/asyncHooksInterface.js +0 -30
  75. package/dist/js/node/context.js +0 -164
  76. package/dist/js/node/counter.js +0 -52
  77. package/dist/js/node/hook.js +0 -57
  78. package/dist/js/node/pipeline/async.js +0 -110
  79. package/dist/js/node/pipeline/index.js +0 -31
  80. package/dist/js/node/pipeline/sync.js +0 -110
  81. package/dist/js/treeshaking/asyncHooksImpl.js +0 -65
  82. package/dist/js/treeshaking/asyncHooksInterface.js +0 -16
  83. package/dist/js/treeshaking/context.js +0 -137
  84. package/dist/js/treeshaking/counter.js +0 -74
  85. package/dist/js/treeshaking/hook.js +0 -51
  86. package/dist/js/treeshaking/pipeline/async.js +0 -165
  87. package/dist/js/treeshaking/pipeline/index.js +0 -2
  88. package/dist/js/treeshaking/pipeline/sync.js +0 -118
  89. package/dist/types/asyncHooksImpl.d.ts +0 -10
  90. package/dist/types/asyncHooksInterface.d.ts +0 -21
  91. package/dist/types/context.d.ts +0 -47
  92. package/dist/types/counter.d.ts +0 -22
  93. package/dist/types/hook.d.ts +0 -13
  94. package/dist/types/pipeline/async.d.ts +0 -35
  95. package/dist/types/pipeline/index.d.ts +0 -2
  96. package/dist/types/pipeline/sync.d.ts +0 -37
  97. package/src/asyncHooksImpl.ts +0 -64
  98. package/src/asyncHooksInterface.ts +0 -34
  99. package/src/context.ts +0 -184
  100. package/src/counter.ts +0 -78
  101. package/src/hook.ts +0 -46
  102. package/src/pipeline/async.ts +0 -155
  103. package/src/pipeline/index.ts +0 -2
  104. package/src/pipeline/sync.ts +0 -152
  105. package/tests/context.test.ts +0 -114
  106. package/tests/counter.test.ts +0 -32
  107. package/tests/hook.test.ts +0 -113
@@ -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
- } from '../context';
4
+ createPipeline,
5
+ Middleware,
6
+ } from 'farrow-pipeline';
10
7
 
11
8
  const WATERFALL_SYMBOL = Symbol('WATERFALL_SYMBOL');
12
9
 
13
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
14
- export type Brook<I = unknown> = (I: I) => I | void;
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.brook === 'function') {
23
- return input.brook;
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
- brook: Brook<I>;
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 middlewares: Brooks<I> = [];
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'] = (...input) => {
96
- middlewares.push(...input.map(getBrook));
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
- const container = options?.container ?? currentContainer;
102
- const hooks =
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
- return runner.start(input);
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
- return run(input, { container });
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
- brook,
122
- [WATERFALL_SYMBOL]: true as const,
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));
@@ -1,14 +1,13 @@
1
- import { createAsyncCounter } from '../counter';
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) => O | Promise<O>;
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) => Promise<O[]>;
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 middlewares: AsyncWorkers<I, O> = [];
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
- middlewares.push(...input);
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 container = options?.container ?? currentContainer;
83
- const hooks =
84
- container === currentContainer ? currentHooks : fromContainer(container);
85
- const runner =
86
- container === currentContainer
87
- ? currentRunner
88
- : createCurrentRunner(hooks);
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
+ }
@@ -1,6 +1,5 @@
1
- import { createAsyncCounter } from '../counter';
2
- import { Hooks, runHooks, fromContainer, createContainer } from '../context';
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 middlewares: AsyncWorkers<I, O> = [];
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
- middlewares.push(...input);
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
- const container = options?.container ?? currentContainer;
98
- const hooks =
99
- container === currentContainer ? currentHooks : fromContainer(container);
100
- const runner =
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
- use,
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)];
@@ -1,11 +1,4 @@
1
- import { createCounter } from '../counter';
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 middlewares: Workers<I, O> = [];
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
- middlewares.push(...input);
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 container = options?.container ?? currentContainer;
84
- const hooks =
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)];
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ extends: ['@modern-js'],
3
+ parserOptions: {
4
+ project: require.resolve('./tsconfig.json'),
5
+ },
6
+ };
@@ -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,
@@ -2,7 +2,6 @@ import { createPlugin } from '../core';
2
2
  import { initFooPlugins } from './fooManager';
3
3
 
4
4
  const foo = createPlugin(
5
- // eslint-disable-next-line @typescript-eslint/require-await
6
5
  async () => {
7
6
  const fooManage = initFooPlugins();
8
7
 
@@ -3,7 +3,7 @@ import {
3
3
  createWaterfall,
4
4
  createWorkflow,
5
5
  createManager,
6
- } from '../../../..';
6
+ } from '../../../../src';
7
7
 
8
8
  // foo plugin
9
9
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -3,7 +3,7 @@ import {
3
3
  createWaterfall,
4
4
  createWorkflow,
5
5
  createContext,
6
- } from '../../../..';
6
+ } from '../../../../src';
7
7
 
8
8
  // eslint-disable-next-line @typescript-eslint/ban-types
9
9
  export type CTX = {};
@@ -8,7 +8,6 @@ const bar = createPlugin(() => {
8
8
  return {
9
9
  // eslint-disable-next-line @typescript-eslint/no-empty-function
10
10
  preDev: () => {},
11
- // eslint-disable-next-line @typescript-eslint/require-await
12
11
  fooWaterfall: async input => {
13
12
  number = 2;
14
13
  return input;
@@ -1,4 +1,4 @@
1
- import { createAsyncWaterfall } from '../../../..';
1
+ import { createAsyncWaterfall } from '../../../../src';
2
2
  import { createPlugin, registeManager, useRunner } from '../core';
3
3
 
4
4
  // declare new lifecircle type
@@ -6,11 +6,9 @@ const foo = createPlugin(
6
6
  const fooManage = initFooPlugins();
7
7
 
8
8
  return {
9
- // eslint-disable-next-line @typescript-eslint/require-await
10
9
  preDev: async () => {
11
10
  fooManage.fooWaterfall();
12
11
  },
13
- // eslint-disable-next-line @typescript-eslint/require-await
14
12
  postDev: async () => {
15
13
  fooManage.fooWorflow();
16
14
  },
@@ -3,7 +3,7 @@ import {
3
3
  createWaterfall,
4
4
  createWorkflow,
5
5
  createManager,
6
- } from '../../../..';
6
+ } from '../../../../src';
7
7
 
8
8
  // foo plugin
9
9
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -3,7 +3,7 @@ import {
3
3
  createWaterfall,
4
4
  createWorkflow,
5
5
  createContext,
6
- } from '../../../..';
6
+ } from '../../../../src';
7
7
 
8
8
  // eslint-disable-next-line @typescript-eslint/ban-types
9
9
  export type CTX = {};
@@ -12,7 +12,6 @@ const bar = createPlugin(() => {
12
12
  return {
13
13
  // eslint-disable-next-line @typescript-eslint/no-empty-function
14
14
  preDev: async () => {},
15
- // eslint-disable-next-line @typescript-eslint/require-await
16
15
  fooWaterfall: async input => {
17
16
  number = 2;
18
17
  return input;
@@ -1,4 +1,4 @@
1
- import { createAsyncWaterfall } from '../../../..';
1
+ import { createAsyncWaterfall } from '../../../../src';
2
2
  import { createPlugin, registeManager, useRunner } from '../core';
3
3
 
4
4
  // declare new lifecircle type
@@ -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 () => {
@@ -528,11 +526,10 @@ describe('createPipeline', () => {
528
526
  const pipeline0 = createAsyncPipeline<string, string>();
529
527
  const pipeline1 = createAsyncPipeline<string, string>();
530
528
 
531
- // eslint-disable-next-line @typescript-eslint/require-await
532
529
  pipeline0.use(async input => `${input} from pipeline0`);
533
530
 
534
531
  pipeline1.use(async input => {
535
- const runPipeline1 = useAsyncPipeline(pipeline0);
532
+ const runPipeline1 = usePipeline(pipeline0);
536
533
 
537
534
  const text = await runPipeline1(' pipeline1');
538
535
 
@@ -544,15 +541,6 @@ describe('createPipeline', () => {
544
541
  expect(result).toEqual(`run pipeline1 from pipeline0`);
545
542
  });
546
543
 
547
- it('isAsyncPipeline', () => {
548
- const pipeline = createAsyncPipeline();
549
-
550
- expect(isAsyncPipeline(pipeline)).toBeTruthy();
551
- expect(isAsyncPipeline({})).toBeFalsy();
552
- expect(isAsyncPipeline('test')).toBeFalsy();
553
- expect(isAsyncPipeline(null)).toBeFalsy();
554
- });
555
-
556
544
  it('support hooks', async () => {
557
545
  const pipeline = createAsyncPipeline<number, number>();
558
546
 
@@ -600,7 +588,8 @@ describe('createPipeline', () => {
600
588
  it('should throw error when all middlewares calling next() and onLast is not exist', async () => {
601
589
  const pipeline = createAsyncPipeline<number, number>();
602
590
 
603
- pipeline.use((input, next) => next(input));
591
+ // eslint-disable-next-line promise/prefer-await-to-then
592
+ pipeline.use((input, next) => Promise.resolve().then(() => next(input)));
604
593
 
605
594
  await expect(() => pipeline.run(0)).rejects.toThrowError();
606
595
  });
@@ -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,
@@ -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', () => {
@@ -25,7 +25,6 @@ describe('waterfall', () => {
25
25
  it('should support async waterfall', async () => {
26
26
  const waterfall = createAsyncWaterfall<{ count: number }>();
27
27
 
28
- // eslint-disable-next-line @typescript-eslint/require-await
29
28
  waterfall.use(async ({ count }) => ({ count: count + 1 }));
30
29
 
31
30
  waterfall.use(a => a);
@@ -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
- };