@modern-js/plugin 1.0.1 → 1.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.
Files changed (106) hide show
  1. package/CHANGELOG.md +20 -2
  2. package/dist/js/modern/index.js +2 -3
  3. package/dist/js/modern/manager/async.js +3 -3
  4. package/dist/js/modern/manager/runner.js +1 -1
  5. package/dist/js/modern/manager/sync.js +3 -8
  6. package/dist/js/modern/waterfall/async.js +28 -34
  7. package/dist/js/modern/waterfall/sync.js +29 -36
  8. package/dist/js/modern/workflow/async.js +31 -30
  9. package/dist/js/modern/workflow/parallel.js +20 -29
  10. package/dist/js/modern/workflow/sync.js +20 -27
  11. package/dist/js/node/index.js +12 -25
  12. package/dist/js/node/manager/async.js +6 -6
  13. package/dist/js/node/manager/runner.js +2 -2
  14. package/dist/js/node/manager/sync.js +8 -14
  15. package/dist/js/node/waterfall/async.js +27 -34
  16. package/dist/js/node/waterfall/sync.js +29 -36
  17. package/dist/js/node/workflow/async.js +30 -30
  18. package/dist/js/node/workflow/parallel.js +19 -29
  19. package/dist/js/node/workflow/sync.js +19 -27
  20. package/dist/js/treeshaking/index.js +2 -3
  21. package/dist/js/treeshaking/manager/async.js +3 -3
  22. package/dist/js/treeshaking/manager/runner.js +1 -1
  23. package/dist/js/treeshaking/manager/sync.js +3 -8
  24. package/dist/js/treeshaking/waterfall/async.js +63 -108
  25. package/dist/js/treeshaking/waterfall/sync.js +36 -39
  26. package/dist/js/treeshaking/workflow/async.js +89 -88
  27. package/dist/js/treeshaking/workflow/parallel.js +42 -66
  28. package/dist/js/treeshaking/workflow/sync.js +50 -30
  29. package/dist/types/index.d.ts +2 -3
  30. package/dist/types/manager/runner.d.ts +1 -1
  31. package/dist/types/manager/sync.d.ts +2 -3
  32. package/dist/types/waterfall/async.d.ts +9 -5
  33. package/dist/types/waterfall/sync.d.ts +4 -4
  34. package/dist/types/workflow/async.d.ts +3 -2
  35. package/dist/types/workflow/parallel.d.ts +1 -1
  36. package/dist/types/workflow/sync.d.ts +1 -1
  37. package/jest.config.js +8 -0
  38. package/modern.config.js +1 -9
  39. package/node.d.ts +1 -1
  40. package/node.js +1 -1
  41. package/package.json +13 -7
  42. package/src/index.ts +2 -2
  43. package/src/manager/async.ts +1 -1
  44. package/src/manager/runner.ts +1 -1
  45. package/src/manager/sync.ts +12 -16
  46. package/src/waterfall/async.ts +34 -49
  47. package/src/waterfall/sync.ts +26 -45
  48. package/src/workflow/async.ts +29 -33
  49. package/src/workflow/parallel.ts +17 -35
  50. package/src/workflow/sync.ts +13 -35
  51. package/tests/async.test.ts +14 -10
  52. package/tests/fixtures/async/base/fooManager.ts +1 -1
  53. package/tests/fixtures/async/core/index.ts +1 -1
  54. package/tests/fixtures/async/dynamic/foo.ts +1 -1
  55. package/tests/fixtures/sync/base/fooManager.ts +1 -1
  56. package/tests/fixtures/sync/core/index.ts +1 -1
  57. package/tests/fixtures/sync/dynamic/foo.ts +1 -1
  58. package/tests/pipeline.test.ts +6 -16
  59. package/tests/sync.test.ts +13 -9
  60. package/tests/tsconfig.json +1 -3
  61. package/tests/waterfall.test.ts +3 -3
  62. package/tests/workflow.test.ts +2 -2
  63. package/tsconfig.json +1 -3
  64. package/dist/js/modern/asyncHooksImpl.js +0 -63
  65. package/dist/js/modern/asyncHooksInterface.js +0 -16
  66. package/dist/js/modern/context.js +0 -130
  67. package/dist/js/modern/counter.js +0 -40
  68. package/dist/js/modern/hook.js +0 -47
  69. package/dist/js/modern/pipeline/async.js +0 -97
  70. package/dist/js/modern/pipeline/index.js +0 -2
  71. package/dist/js/modern/pipeline/sync.js +0 -97
  72. package/dist/js/node/asyncHooksImpl.js +0 -82
  73. package/dist/js/node/asyncHooksInterface.js +0 -30
  74. package/dist/js/node/context.js +0 -164
  75. package/dist/js/node/counter.js +0 -52
  76. package/dist/js/node/hook.js +0 -57
  77. package/dist/js/node/pipeline/async.js +0 -110
  78. package/dist/js/node/pipeline/index.js +0 -31
  79. package/dist/js/node/pipeline/sync.js +0 -110
  80. package/dist/js/treeshaking/asyncHooksImpl.js +0 -65
  81. package/dist/js/treeshaking/asyncHooksInterface.js +0 -16
  82. package/dist/js/treeshaking/context.js +0 -137
  83. package/dist/js/treeshaking/counter.js +0 -74
  84. package/dist/js/treeshaking/hook.js +0 -51
  85. package/dist/js/treeshaking/pipeline/async.js +0 -165
  86. package/dist/js/treeshaking/pipeline/index.js +0 -2
  87. package/dist/js/treeshaking/pipeline/sync.js +0 -118
  88. package/dist/types/asyncHooksImpl.d.ts +0 -10
  89. package/dist/types/asyncHooksInterface.d.ts +0 -21
  90. package/dist/types/context.d.ts +0 -47
  91. package/dist/types/counter.d.ts +0 -22
  92. package/dist/types/hook.d.ts +0 -13
  93. package/dist/types/pipeline/async.d.ts +0 -35
  94. package/dist/types/pipeline/index.d.ts +0 -2
  95. package/dist/types/pipeline/sync.d.ts +0 -37
  96. package/src/asyncHooksImpl.ts +0 -64
  97. package/src/asyncHooksInterface.ts +0 -34
  98. package/src/context.ts +0 -184
  99. package/src/counter.ts +0 -78
  100. package/src/hook.ts +0 -46
  101. package/src/pipeline/async.ts +0 -155
  102. package/src/pipeline/index.ts +0 -2
  103. package/src/pipeline/sync.ts +0 -152
  104. package/tests/context.test.ts +0 -114
  105. package/tests/counter.test.ts +0 -31
  106. package/tests/hook.test.ts +0 -113
@@ -1,4 +1,4 @@
1
- import { sleep } from './helpers';
1
+ import { enable, disable } from 'farrow-pipeline/asyncHooks.node';
2
2
  import {
3
3
  createWaterfall,
4
4
  createAsyncWaterfall,
@@ -6,8 +6,8 @@ import {
6
6
  createContainer,
7
7
  isWaterfall,
8
8
  isAsyncWaterfall,
9
- } from '@/index';
10
- import { enable, disable } from '@/asyncHooksImpl';
9
+ } from '../src';
10
+ import { sleep } from './helpers';
11
11
 
12
12
  describe('waterfall', () => {
13
13
  it('base usage', () => {
@@ -1,4 +1,3 @@
1
- import { sleep } from './helpers';
2
1
  import {
3
2
  createWorkflow,
4
3
  createAsyncWorkflow,
@@ -6,7 +5,8 @@ import {
6
5
  isWorkflow,
7
6
  isAsyncWorkflow,
8
7
  isParallelWorkflow,
9
- } from '@/workflow';
8
+ } from '../src/workflow';
9
+ import { sleep } from './helpers';
10
10
 
11
11
  describe('workflow', () => {
12
12
  describe('sync', () => {
package/tsconfig.json CHANGED
@@ -7,9 +7,7 @@
7
7
  "isolatedModules": true,
8
8
  "target": "ES5",
9
9
  "esModuleInterop": true,
10
- "paths": {
11
- "@/*": ["./src/*"]
12
- },
10
+ "paths": {},
13
11
  "types": ["node", "jest"]
14
12
  },
15
13
  "include": ["src"]
@@ -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));
@@ -1,40 +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 @typescript-eslint/no-invalid-void-type
10
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
11
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
12
- export const createCounter = callback => {
13
- const dispatch = (index, input) => {
14
- const next = (nextInput = input) => dispatch(index + 1, nextInput);
15
-
16
- return callback(index, input, next);
17
- };
18
-
19
- const start = input => dispatch(0, input);
20
-
21
- return {
22
- start,
23
- dispatch
24
- };
25
- }; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
26
-
27
- export const createAsyncCounter = callback => {
28
- const dispatch = async (index, input) => {
29
- const next = (nextInput = input) => dispatch(index + 1, nextInput);
30
-
31
- return callback(index, input, next);
32
- };
33
-
34
- const start = input => dispatch(0, input);
35
-
36
- return {
37
- start,
38
- dispatch
39
- };
40
- };
@@ -1,47 +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 { asyncHooks } from "./asyncHooksInterface";
10
- export const createHooks = defaultHooks => {
11
- let currentHooks = {};
12
- const hooks = {};
13
-
14
- for (const key in defaultHooks) {
15
- // eslint-disable-next-line @typescript-eslint/no-loop-func
16
- const f = (...args) => {
17
- var _asyncHooks$get;
18
-
19
- // eslint-disable-next-line @typescript-eslint/no-shadow
20
- const hooks = currentHooks === defaultHooks ? (_asyncHooks$get = asyncHooks === null || asyncHooks === void 0 ? void 0 : asyncHooks.get()) !== null && _asyncHooks$get !== void 0 ? _asyncHooks$get : defaultHooks : currentHooks;
21
- let handler = hooks[key];
22
-
23
- if (typeof handler !== 'function') {
24
- handler = defaultHooks[key];
25
- }
26
-
27
- return handler(...args);
28
- };
29
-
30
- hooks[key] = f;
31
- }
32
-
33
- const run = (f, implementations) => {
34
- try {
35
- currentHooks = implementations || defaultHooks;
36
- asyncHooks === null || asyncHooks === void 0 ? void 0 : asyncHooks.set(currentHooks);
37
- return f();
38
- } finally {
39
- currentHooks = defaultHooks;
40
- }
41
- };
42
-
43
- return {
44
- run,
45
- hooks
46
- };
47
- };
@@ -1,97 +0,0 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
2
-
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
-
5
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
-
7
- /**
8
- * Copyright Lucifier129 and other contributors.
9
- *
10
- * This source code is licensed under the MIT license found in the
11
- * LICENSE file at
12
- * https://github.com/farrow-js/farrow/blob/master/LICENSE
13
- *
14
- */
15
- import { createContainer, fromContainer, runHooks, useContainer } from "../context";
16
- import { createAsyncCounter } from "../counter";
17
- const ASYNC_PIPELINE_SYMBOL = Symbol('ASYNC_PIPELINE_SYMBOL');
18
- export const isAsyncPipeline = input => Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_PIPELINE_SYMBOL]);
19
- export const getAsyncMiddleware = input => {
20
- if (typeof input === 'function') {
21
- return input;
22
- } else if (input && typeof input.middleware === 'function') {
23
- return input.middleware;
24
- } // eslint-disable-next-line @typescript-eslint/no-base-to-string,@typescript-eslint/restrict-template-expressions
25
-
26
-
27
- throw new Error(`${input} is not a Middleware or { middleware: Middleware }`);
28
- };
29
- export const createAsyncPipeline = options => {
30
- const config = _objectSpread({}, options);
31
-
32
- const middlewares = [];
33
-
34
- const use = (...inputs) => {
35
- middlewares.push(...inputs.map(getAsyncMiddleware));
36
- return pipeline;
37
- };
38
-
39
- const createCurrentCounter = (hooks, onLast) => createAsyncCounter(async (index, input, next) => {
40
- if (index >= middlewares.length) {
41
- if (onLast) {
42
- return onLast(input);
43
- }
44
-
45
- throw new Error(`Expect returning a value, but all middlewares just calling next()`);
46
- }
47
-
48
- const middleware = middlewares[index];
49
- const result = await runHooks(async () => middleware(input, next), hooks);
50
- return result;
51
- });
52
-
53
- const currentContainer = createContainer(config.contexts);
54
- const currentHooks = fromContainer(currentContainer);
55
- const currentCounter = createCurrentCounter(currentHooks); // eslint-disable-next-line @typescript-eslint/no-shadow
56
-
57
- const run = (input, options) => {
58
- var _options$container;
59
-
60
- const container = (_options$container = options === null || options === void 0 ? void 0 : options.container) !== null && _options$container !== void 0 ? _options$container : currentContainer;
61
- const hooks = container === currentContainer ? currentHooks : fromContainer(container);
62
- let counter = container === currentContainer ? currentCounter : createCurrentCounter(hooks);
63
-
64
- if (options !== null && options !== void 0 && options.onLast) {
65
- counter = createCurrentCounter(hooks, options.onLast);
66
- }
67
-
68
- const result = counter.start(input);
69
- return result;
70
- };
71
-
72
- const middleware = (input, next) => {
73
- // eslint-disable-next-line react-hooks/rules-of-hooks
74
- const container = useContainer();
75
- return run(input, {
76
- container,
77
- onLast: next
78
- });
79
- };
80
-
81
- const pipeline = {
82
- [ASYNC_PIPELINE_SYMBOL]: true,
83
- use,
84
- run,
85
- middleware
86
- };
87
- return pipeline;
88
- };
89
- export const useAsyncPipeline = pipeline => {
90
- const container = useContainer();
91
-
92
- const runPipeline = (input, options) => pipeline.run(input, _objectSpread(_objectSpread({}, options), {}, {
93
- container
94
- }));
95
-
96
- return runPipeline;
97
- };
@@ -1,2 +0,0 @@
1
- export * from "./sync";
2
- export * from "./async";
@@ -1,97 +0,0 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
2
-
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
-
5
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
-
7
- /**
8
- * Copyright Lucifier129 and other contributors.
9
- *
10
- * This source code is licensed under the MIT license found in the
11
- * LICENSE file at
12
- * https://github.com/farrow-js/farrow/blob/master/LICENSE
13
- *
14
- */
15
- import { createContainer, fromContainer, runHooks, useContainer } from "../context";
16
- import { createCounter } from "../counter";
17
- const PIPELINE_SYMBOL = Symbol('PIPELINE_SYMBOL');
18
- export const isPipeline = input => Boolean(input === null || input === void 0 ? void 0 : input[PIPELINE_SYMBOL]);
19
- export const getMiddleware = input => {
20
- if (typeof input === 'function') {
21
- return input;
22
- } else if (input && typeof input.middleware === 'function') {
23
- return input.middleware;
24
- } // eslint-disable-next-line @typescript-eslint/no-base-to-string,@typescript-eslint/restrict-template-expressions
25
-
26
-
27
- throw new Error(`${input} is not a Middleware or { middleware: Middleware }`);
28
- };
29
- export const createPipeline = options => {
30
- const config = _objectSpread({}, options);
31
-
32
- const middlewares = [];
33
-
34
- const use = (...inputs) => {
35
- middlewares.push(...inputs.map(getMiddleware));
36
- return pipeline;
37
- };
38
-
39
- const createCurrentCounter = (hooks, onLast) => createCounter((index, input, next) => {
40
- if (index >= middlewares.length) {
41
- if (onLast) {
42
- return onLast(input);
43
- }
44
-
45
- throw new Error(`Expect returning a value, but all middlewares just calling next()`);
46
- }
47
-
48
- const middleware = middlewares[index];
49
- const result = runHooks(() => middleware(input, next), hooks);
50
- return result;
51
- });
52
-
53
- const currentContainer = createContainer(config.contexts);
54
- const currentHooks = fromContainer(currentContainer);
55
- const currentCounter = createCurrentCounter(currentHooks); // eslint-disable-next-line @typescript-eslint/no-shadow
56
-
57
- const run = (input, options) => {
58
- var _options$container;
59
-
60
- const container = (_options$container = options === null || options === void 0 ? void 0 : options.container) !== null && _options$container !== void 0 ? _options$container : currentContainer;
61
- const hooks = container === currentContainer ? currentHooks : fromContainer(container);
62
- let counter = container === currentContainer ? currentCounter : createCurrentCounter(hooks);
63
-
64
- if (options !== null && options !== void 0 && options.onLast) {
65
- counter = createCurrentCounter(hooks, options.onLast);
66
- }
67
-
68
- const result = counter.start(input);
69
- return result;
70
- };
71
-
72
- const middleware = (input, next) => {
73
- // eslint-disable-next-line react-hooks/rules-of-hooks
74
- const container = useContainer();
75
- return run(input, {
76
- container,
77
- onLast: next
78
- });
79
- };
80
-
81
- const pipeline = {
82
- [PIPELINE_SYMBOL]: true,
83
- use,
84
- run,
85
- middleware
86
- };
87
- return pipeline;
88
- };
89
- export const usePipeline = pipeline => {
90
- const container = useContainer();
91
-
92
- const runPipeline = (input, options) => pipeline.run(input, _objectSpread(_objectSpread({}, options), {}, {
93
- container
94
- }));
95
-
96
- return runPipeline;
97
- };
@@ -1,82 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.enable = exports.disable = void 0;
7
-
8
- var _async_hooks = _interopRequireDefault(require("async_hooks"));
9
-
10
- var asyncHooksInterface = _interopRequireWildcard(require("./asyncHooksInterface"));
11
-
12
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
-
14
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
-
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
- /**
19
- * Copyright Lucifier129 and other contributors.
20
- *
21
- * This source code is licensed under the MIT license found in the
22
- * LICENSE file at
23
- * https://github.com/farrow-js/farrow/blob/master/LICENSE
24
- *
25
- */
26
- const createAsyncHooks = () => {
27
- const store = new Map(); // eslint-disable-next-line node/no-unsupported-features/node-builtins
28
-
29
- const hooks = _async_hooks.default.createHook({
30
- init: (asyncId, _, triggerAsyncId) => {
31
- if (store.has(triggerAsyncId)) {
32
- const value = store.get(triggerAsyncId);
33
- store.set(asyncId, value);
34
- }
35
- },
36
- destroy: asyncId => {
37
- if (store.has(asyncId)) {
38
- store.delete(asyncId);
39
- }
40
- }
41
- });
42
-
43
- const set = value => {
44
- store.set(_async_hooks.default.executionAsyncId(), value);
45
- };
46
-
47
- const get = () => store.get(_async_hooks.default.executionAsyncId());
48
-
49
- const enable = () => {
50
- hooks.enable();
51
- };
52
-
53
- const disable = () => {
54
- hooks.disable();
55
- store.clear();
56
- };
57
-
58
- return {
59
- enable,
60
- disable,
61
- set,
62
- get
63
- };
64
- };
65
-
66
- const enable = () => {
67
- const hooks = createAsyncHooks();
68
- disable();
69
- asyncHooksInterface.impl(hooks);
70
- hooks.enable();
71
- };
72
-
73
- exports.enable = enable;
74
-
75
- const disable = () => {
76
- var _asyncHooksInterface$;
77
-
78
- (_asyncHooksInterface$ = asyncHooksInterface.asyncHooks) === null || _asyncHooksInterface$ === void 0 ? void 0 : _asyncHooksInterface$.disable();
79
- asyncHooksInterface.reset();
80
- };
81
-
82
- exports.disable = disable;
@@ -1,30 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.reset = exports.impl = exports.asyncHooks = void 0;
7
-
8
- /**
9
- * Copyright Lucifier129 and other contributors.
10
- *
11
- * This source code is licensed under the MIT license found in the
12
- * LICENSE file at
13
- * https://github.com/farrow-js/farrow/blob/master/LICENSE
14
- *
15
- */
16
- // eslint-disable-next-line import/no-mutable-exports
17
- let asyncHooks;
18
- exports.asyncHooks = asyncHooks;
19
-
20
- const impl = implimentations => {
21
- exports.asyncHooks = asyncHooks = implimentations;
22
- };
23
-
24
- exports.impl = impl;
25
-
26
- const reset = () => {
27
- exports.asyncHooks = asyncHooks = undefined;
28
- };
29
-
30
- exports.reset = reset;