@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,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;
@@ -1,164 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useContainer = exports.runWithContainer = exports.runHooks = exports.isContext = exports.isContainer = exports.fromContainer = exports.createContext = exports.createContainer = exports.assertContext = exports.assertContainer = exports.ContainerSymbol = void 0;
7
-
8
- var _hook = require("./hook");
9
-
10
- /**
11
- * Copyright Lucifier129 and other contributors.
12
- *
13
- * This source code is licensed under the MIT license found in the
14
- * LICENSE file at
15
- * https://github.com/farrow-js/farrow/blob/master/LICENSE
16
- *
17
- */
18
- const ContextSymbol = Symbol('Context');
19
-
20
- const isContext = input => Object.prototype.hasOwnProperty.call(input, ContextSymbol);
21
-
22
- exports.isContext = isContext;
23
-
24
- const assertContext = input => {
25
- if (!isContext(input)) {
26
- throw new Error(`Expected Context, but received ${input}`);
27
- }
28
- };
29
-
30
- exports.assertContext = assertContext;
31
-
32
- const createContext = value => {
33
- const id = Symbol('ContextID'); // eslint-disable-next-line @typescript-eslint/no-shadow
34
-
35
- const create = value => {
36
- const use = () => {
37
- // eslint-disable-next-line react-hooks/rules-of-hooks
38
- const container = useContainer();
39
- return Object.seal({
40
- get value() {
41
- return container.read(Context);
42
- },
43
-
44
- set value(v) {
45
- container.write(Context, v);
46
- }
47
-
48
- });
49
- };
50
-
51
- const get = () => {
52
- // eslint-disable-next-line react-hooks/rules-of-hooks
53
- const container = useContainer();
54
- return container.read(Context);
55
- };
56
-
57
- const set = v => {
58
- // eslint-disable-next-line react-hooks/rules-of-hooks
59
- const container = useContainer();
60
- container.write(Context, v);
61
- };
62
-
63
- const assert = () => {
64
- // eslint-disable-next-line @typescript-eslint/no-shadow
65
- const value = get();
66
-
67
- if (value === null || value === undefined) {
68
- throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
69
- `Expected value is not null or undefined, but got: ${value}`);
70
- }
71
-
72
- return value;
73
- };
74
-
75
- const Context = {
76
- id,
77
- [ContextSymbol]: value,
78
- create,
79
- use,
80
- get,
81
- set,
82
- assert
83
- };
84
- return Context;
85
- };
86
-
87
- return create(value);
88
- };
89
-
90
- exports.createContext = createContext;
91
- const ContainerSymbol = Symbol('Container'); // eslint-disable-next-line @typescript-eslint/no-redeclare
92
-
93
- exports.ContainerSymbol = ContainerSymbol;
94
-
95
- const isContainer = input => Boolean(input === null || input === void 0 ? void 0 : input[ContainerSymbol]);
96
-
97
- exports.isContainer = isContainer;
98
-
99
- const assertContainer = input => {
100
- if (!isContainer(input)) {
101
- throw new Error(`Expected Context, but received ${input}`);
102
- }
103
- };
104
-
105
- exports.assertContainer = assertContainer;
106
-
107
- const createContextMap = storage => {
108
- const contextMap = new Map();
109
- Object.values(storage).forEach(context => {
110
- contextMap.set(context.id, context);
111
- });
112
- return contextMap;
113
- };
114
-
115
- const createContainer = (ContextStorage = {}) => {
116
- const contextMap = createContextMap(ContextStorage);
117
-
118
- const read = context => {
119
- const target = contextMap.get(context.id);
120
-
121
- if (target) {
122
- return target[ContextSymbol];
123
- }
124
-
125
- return context[ContextSymbol];
126
- };
127
-
128
- const write = (context, value) => {
129
- contextMap.set(context.id, context.create(value));
130
- };
131
-
132
- const container = Object.freeze({
133
- [ContainerSymbol]: true,
134
- read,
135
- write
136
- });
137
- return container;
138
- };
139
-
140
- exports.createContainer = createContainer;
141
- const {
142
- run,
143
- hooks
144
- } = (0, _hook.createHooks)({
145
- useContainer: () => {
146
- throw new Error(`Can't call useContainer out of scope, it should be placed on top of the function`);
147
- }
148
- });
149
- const runHooks = run;
150
- exports.runHooks = runHooks;
151
- const {
152
- useContainer
153
- } = hooks;
154
- exports.useContainer = useContainer;
155
-
156
- const fromContainer = container => ({
157
- useContainer: () => container
158
- });
159
-
160
- exports.fromContainer = fromContainer;
161
-
162
- const runWithContainer = (f, container) => runHooks(f, fromContainer(container));
163
-
164
- exports.runWithContainer = runWithContainer;