@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,52 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createCounter = exports.createAsyncCounter = 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 @typescript-eslint/no-invalid-void-type
17
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
18
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
19
- const createCounter = callback => {
20
- const dispatch = (index, input) => {
21
- const next = (nextInput = input) => dispatch(index + 1, nextInput);
22
-
23
- return callback(index, input, next);
24
- };
25
-
26
- const start = input => dispatch(0, input);
27
-
28
- return {
29
- start,
30
- dispatch
31
- };
32
- }; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
33
-
34
-
35
- exports.createCounter = createCounter;
36
-
37
- const createAsyncCounter = callback => {
38
- const dispatch = async (index, input) => {
39
- const next = (nextInput = input) => dispatch(index + 1, nextInput);
40
-
41
- return callback(index, input, next);
42
- };
43
-
44
- const start = input => dispatch(0, input);
45
-
46
- return {
47
- start,
48
- dispatch
49
- };
50
- };
51
-
52
- exports.createAsyncCounter = createAsyncCounter;
@@ -1,57 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createHooks = void 0;
7
-
8
- var _asyncHooksInterface = require("./asyncHooksInterface");
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 createHooks = defaultHooks => {
19
- let currentHooks = {};
20
- const hooks = {};
21
-
22
- for (const key in defaultHooks) {
23
- // eslint-disable-next-line @typescript-eslint/no-loop-func
24
- const f = (...args) => {
25
- var _asyncHooks$get;
26
-
27
- // eslint-disable-next-line @typescript-eslint/no-shadow
28
- const hooks = currentHooks === defaultHooks ? (_asyncHooks$get = _asyncHooksInterface.asyncHooks === null || _asyncHooksInterface.asyncHooks === void 0 ? void 0 : _asyncHooksInterface.asyncHooks.get()) !== null && _asyncHooks$get !== void 0 ? _asyncHooks$get : defaultHooks : currentHooks;
29
- let handler = hooks[key];
30
-
31
- if (typeof handler !== 'function') {
32
- handler = defaultHooks[key];
33
- }
34
-
35
- return handler(...args);
36
- };
37
-
38
- hooks[key] = f;
39
- }
40
-
41
- const run = (f, implementations) => {
42
- try {
43
- currentHooks = implementations || defaultHooks;
44
- _asyncHooksInterface.asyncHooks === null || _asyncHooksInterface.asyncHooks === void 0 ? void 0 : _asyncHooksInterface.asyncHooks.set(currentHooks);
45
- return f();
46
- } finally {
47
- currentHooks = defaultHooks;
48
- }
49
- };
50
-
51
- return {
52
- run,
53
- hooks
54
- };
55
- };
56
-
57
- exports.createHooks = createHooks;
@@ -1,110 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useAsyncPipeline = exports.isAsyncPipeline = exports.getAsyncMiddleware = exports.createAsyncPipeline = void 0;
7
-
8
- var _context = require("../context");
9
-
10
- var _counter = require("../counter");
11
-
12
- 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; }
13
-
14
- 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; }
15
-
16
- 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; }
17
-
18
- const ASYNC_PIPELINE_SYMBOL = Symbol('ASYNC_PIPELINE_SYMBOL');
19
-
20
- const isAsyncPipeline = input => Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_PIPELINE_SYMBOL]);
21
-
22
- exports.isAsyncPipeline = isAsyncPipeline;
23
-
24
- const getAsyncMiddleware = input => {
25
- if (typeof input === 'function') {
26
- return input;
27
- } else if (input && typeof input.middleware === 'function') {
28
- return input.middleware;
29
- } // eslint-disable-next-line @typescript-eslint/no-base-to-string,@typescript-eslint/restrict-template-expressions
30
-
31
-
32
- throw new Error(`${input} is not a Middleware or { middleware: Middleware }`);
33
- };
34
-
35
- exports.getAsyncMiddleware = getAsyncMiddleware;
36
-
37
- const createAsyncPipeline = options => {
38
- const config = _objectSpread({}, options);
39
-
40
- const middlewares = [];
41
-
42
- const use = (...inputs) => {
43
- middlewares.push(...inputs.map(getAsyncMiddleware));
44
- return pipeline;
45
- };
46
-
47
- const createCurrentCounter = (hooks, onLast) => (0, _counter.createAsyncCounter)(async (index, input, next) => {
48
- if (index >= middlewares.length) {
49
- if (onLast) {
50
- return onLast(input);
51
- }
52
-
53
- throw new Error(`Expect returning a value, but all middlewares just calling next()`);
54
- }
55
-
56
- const middleware = middlewares[index];
57
- const result = await (0, _context.runHooks)(async () => middleware(input, next), hooks);
58
- return result;
59
- });
60
-
61
- const currentContainer = (0, _context.createContainer)(config.contexts);
62
- const currentHooks = (0, _context.fromContainer)(currentContainer);
63
- const currentCounter = createCurrentCounter(currentHooks); // eslint-disable-next-line @typescript-eslint/no-shadow
64
-
65
- const run = (input, options) => {
66
- var _options$container;
67
-
68
- const container = (_options$container = options === null || options === void 0 ? void 0 : options.container) !== null && _options$container !== void 0 ? _options$container : currentContainer;
69
- const hooks = container === currentContainer ? currentHooks : (0, _context.fromContainer)(container);
70
- let counter = container === currentContainer ? currentCounter : createCurrentCounter(hooks);
71
-
72
- if (options !== null && options !== void 0 && options.onLast) {
73
- counter = createCurrentCounter(hooks, options.onLast);
74
- }
75
-
76
- const result = counter.start(input);
77
- return result;
78
- };
79
-
80
- const middleware = (input, next) => {
81
- // eslint-disable-next-line react-hooks/rules-of-hooks
82
- const container = (0, _context.useContainer)();
83
- return run(input, {
84
- container,
85
- onLast: next
86
- });
87
- };
88
-
89
- const pipeline = {
90
- [ASYNC_PIPELINE_SYMBOL]: true,
91
- use,
92
- run,
93
- middleware
94
- };
95
- return pipeline;
96
- };
97
-
98
- exports.createAsyncPipeline = createAsyncPipeline;
99
-
100
- const useAsyncPipeline = pipeline => {
101
- const container = (0, _context.useContainer)();
102
-
103
- const runPipeline = (input, options) => pipeline.run(input, _objectSpread(_objectSpread({}, options), {}, {
104
- container
105
- }));
106
-
107
- return runPipeline;
108
- };
109
-
110
- exports.useAsyncPipeline = useAsyncPipeline;
@@ -1,31 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _sync = require("./sync");
8
-
9
- Object.keys(_sync).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- if (key in exports && exports[key] === _sync[key]) return;
12
- Object.defineProperty(exports, key, {
13
- enumerable: true,
14
- get: function () {
15
- return _sync[key];
16
- }
17
- });
18
- });
19
-
20
- var _async = require("./async");
21
-
22
- Object.keys(_async).forEach(function (key) {
23
- if (key === "default" || key === "__esModule") return;
24
- if (key in exports && exports[key] === _async[key]) return;
25
- Object.defineProperty(exports, key, {
26
- enumerable: true,
27
- get: function () {
28
- return _async[key];
29
- }
30
- });
31
- });
@@ -1,110 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.usePipeline = exports.isPipeline = exports.getMiddleware = exports.createPipeline = void 0;
7
-
8
- var _context = require("../context");
9
-
10
- var _counter = require("../counter");
11
-
12
- 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; }
13
-
14
- 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; }
15
-
16
- 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; }
17
-
18
- const PIPELINE_SYMBOL = Symbol('PIPELINE_SYMBOL');
19
-
20
- const isPipeline = input => Boolean(input === null || input === void 0 ? void 0 : input[PIPELINE_SYMBOL]);
21
-
22
- exports.isPipeline = isPipeline;
23
-
24
- const getMiddleware = input => {
25
- if (typeof input === 'function') {
26
- return input;
27
- } else if (input && typeof input.middleware === 'function') {
28
- return input.middleware;
29
- } // eslint-disable-next-line @typescript-eslint/no-base-to-string,@typescript-eslint/restrict-template-expressions
30
-
31
-
32
- throw new Error(`${input} is not a Middleware or { middleware: Middleware }`);
33
- };
34
-
35
- exports.getMiddleware = getMiddleware;
36
-
37
- const createPipeline = options => {
38
- const config = _objectSpread({}, options);
39
-
40
- const middlewares = [];
41
-
42
- const use = (...inputs) => {
43
- middlewares.push(...inputs.map(getMiddleware));
44
- return pipeline;
45
- };
46
-
47
- const createCurrentCounter = (hooks, onLast) => (0, _counter.createCounter)((index, input, next) => {
48
- if (index >= middlewares.length) {
49
- if (onLast) {
50
- return onLast(input);
51
- }
52
-
53
- throw new Error(`Expect returning a value, but all middlewares just calling next()`);
54
- }
55
-
56
- const middleware = middlewares[index];
57
- const result = (0, _context.runHooks)(() => middleware(input, next), hooks);
58
- return result;
59
- });
60
-
61
- const currentContainer = (0, _context.createContainer)(config.contexts);
62
- const currentHooks = (0, _context.fromContainer)(currentContainer);
63
- const currentCounter = createCurrentCounter(currentHooks); // eslint-disable-next-line @typescript-eslint/no-shadow
64
-
65
- const run = (input, options) => {
66
- var _options$container;
67
-
68
- const container = (_options$container = options === null || options === void 0 ? void 0 : options.container) !== null && _options$container !== void 0 ? _options$container : currentContainer;
69
- const hooks = container === currentContainer ? currentHooks : (0, _context.fromContainer)(container);
70
- let counter = container === currentContainer ? currentCounter : createCurrentCounter(hooks);
71
-
72
- if (options !== null && options !== void 0 && options.onLast) {
73
- counter = createCurrentCounter(hooks, options.onLast);
74
- }
75
-
76
- const result = counter.start(input);
77
- return result;
78
- };
79
-
80
- const middleware = (input, next) => {
81
- // eslint-disable-next-line react-hooks/rules-of-hooks
82
- const container = (0, _context.useContainer)();
83
- return run(input, {
84
- container,
85
- onLast: next
86
- });
87
- };
88
-
89
- const pipeline = {
90
- [PIPELINE_SYMBOL]: true,
91
- use,
92
- run,
93
- middleware
94
- };
95
- return pipeline;
96
- };
97
-
98
- exports.createPipeline = createPipeline;
99
-
100
- const usePipeline = pipeline => {
101
- const container = (0, _context.useContainer)();
102
-
103
- const runPipeline = (input, options) => pipeline.run(input, _objectSpread(_objectSpread({}, options), {}, {
104
- container
105
- }));
106
-
107
- return runPipeline;
108
- };
109
-
110
- exports.usePipeline = usePipeline;
@@ -1,65 +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
- var createAsyncHooks = function createAsyncHooks() {
13
- var store = new Map(); // eslint-disable-next-line node/no-unsupported-features/node-builtins
14
-
15
- var hooks = NodeAsyncHooks.createHook({
16
- init: function init(asyncId, _, triggerAsyncId) {
17
- if (store.has(triggerAsyncId)) {
18
- var value = store.get(triggerAsyncId);
19
- store.set(asyncId, value);
20
- }
21
- },
22
- destroy: function destroy(asyncId) {
23
- if (store.has(asyncId)) {
24
- store["delete"](asyncId);
25
- }
26
- }
27
- });
28
-
29
- var set = function set(value) {
30
- store.set(NodeAsyncHooks.executionAsyncId(), value);
31
- };
32
-
33
- var get = function get() {
34
- return store.get(NodeAsyncHooks.executionAsyncId());
35
- };
36
-
37
- var enable = function enable() {
38
- hooks.enable();
39
- };
40
-
41
- var disable = function disable() {
42
- hooks.disable();
43
- store.clear();
44
- };
45
-
46
- return {
47
- enable: enable,
48
- disable: disable,
49
- set: set,
50
- get: get
51
- };
52
- };
53
-
54
- export var enable = function enable() {
55
- var hooks = createAsyncHooks();
56
- disable();
57
- asyncHooksInterface.impl(hooks);
58
- hooks.enable();
59
- };
60
- export var disable = function disable() {
61
- var _asyncHooksInterface$;
62
-
63
- (_asyncHooksInterface$ = asyncHooksInterface.asyncHooks) === null || _asyncHooksInterface$ === void 0 ? void 0 : _asyncHooksInterface$.disable();
64
- asyncHooksInterface.reset();
65
- };
@@ -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 var asyncHooks;
11
- export var impl = function impl(implimentations) {
12
- asyncHooks = implimentations;
13
- };
14
- export var reset = function reset() {
15
- asyncHooks = undefined;
16
- };
@@ -1,137 +0,0 @@
1
- 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; }
2
-
3
- /**
4
- * Copyright Lucifier129 and other contributors.
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE file at
8
- * https://github.com/farrow-js/farrow/blob/master/LICENSE
9
- *
10
- */
11
- import { createHooks } from "./hook";
12
- var ContextSymbol = Symbol('Context');
13
- export var isContext = function isContext(input) {
14
- return Object.prototype.hasOwnProperty.call(input, ContextSymbol);
15
- };
16
- export var assertContext = function assertContext(input) {
17
- if (!isContext(input)) {
18
- throw new Error("Expected Context, but received ".concat(input));
19
- }
20
- };
21
- export var createContext = function createContext(value) {
22
- var id = Symbol('ContextID'); // eslint-disable-next-line @typescript-eslint/no-shadow
23
-
24
- var create = function create(value) {
25
- var _Context;
26
-
27
- var use = function use() {
28
- // eslint-disable-next-line react-hooks/rules-of-hooks
29
- var container = useContainer();
30
- return Object.seal({
31
- get value() {
32
- return container.read(Context);
33
- },
34
-
35
- set value(v) {
36
- container.write(Context, v);
37
- }
38
-
39
- });
40
- };
41
-
42
- var get = function get() {
43
- // eslint-disable-next-line react-hooks/rules-of-hooks
44
- var container = useContainer();
45
- return container.read(Context);
46
- };
47
-
48
- var set = function set(v) {
49
- // eslint-disable-next-line react-hooks/rules-of-hooks
50
- var container = useContainer();
51
- container.write(Context, v);
52
- };
53
-
54
- var assert = function assert() {
55
- // eslint-disable-next-line @typescript-eslint/no-shadow
56
- var value = get();
57
-
58
- if (value === null || value === undefined) {
59
- throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
60
- "Expected value is not null or undefined, but got: ".concat(value));
61
- }
62
-
63
- return value;
64
- };
65
-
66
- var Context = (_Context = {
67
- id: id
68
- }, _defineProperty(_Context, ContextSymbol, value), _defineProperty(_Context, "create", create), _defineProperty(_Context, "use", use), _defineProperty(_Context, "get", get), _defineProperty(_Context, "set", set), _defineProperty(_Context, "assert", assert), _Context);
69
- return Context;
70
- };
71
-
72
- return create(value);
73
- };
74
- export var ContainerSymbol = Symbol('Container'); // eslint-disable-next-line @typescript-eslint/no-redeclare
75
-
76
- export var isContainer = function isContainer(input) {
77
- return Boolean(input === null || input === void 0 ? void 0 : input[ContainerSymbol]);
78
- };
79
- export var assertContainer = function assertContainer(input) {
80
- if (!isContainer(input)) {
81
- throw new Error("Expected Context, but received ".concat(input));
82
- }
83
- };
84
-
85
- var createContextMap = function createContextMap(storage) {
86
- var contextMap = new Map();
87
- Object.values(storage).forEach(function (context) {
88
- contextMap.set(context.id, context);
89
- });
90
- return contextMap;
91
- };
92
-
93
- export var createContainer = function createContainer() {
94
- var _Object$freeze;
95
-
96
- var ContextStorage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
97
- var contextMap = createContextMap(ContextStorage);
98
-
99
- var read = function read(context) {
100
- var target = contextMap.get(context.id);
101
-
102
- if (target) {
103
- return target[ContextSymbol];
104
- }
105
-
106
- return context[ContextSymbol];
107
- };
108
-
109
- var write = function write(context, value) {
110
- contextMap.set(context.id, context.create(value));
111
- };
112
-
113
- var container = Object.freeze((_Object$freeze = {}, _defineProperty(_Object$freeze, ContainerSymbol, true), _defineProperty(_Object$freeze, "read", read), _defineProperty(_Object$freeze, "write", write), _Object$freeze));
114
- return container;
115
- };
116
-
117
- var _createHooks = createHooks({
118
- useContainer: function useContainer() {
119
- throw new Error("Can't call useContainer out of scope, it should be placed on top of the function");
120
- }
121
- }),
122
- run = _createHooks.run,
123
- hooks = _createHooks.hooks;
124
-
125
- export var runHooks = run;
126
- var useContainer = hooks.useContainer;
127
- export { useContainer };
128
- export var fromContainer = function fromContainer(container) {
129
- return {
130
- useContainer: function useContainer() {
131
- return container;
132
- }
133
- };
134
- };
135
- export var runWithContainer = function runWithContainer(f, container) {
136
- return runHooks(f, fromContainer(container));
137
- };
@@ -1,74 +0,0 @@
1
- import _regeneratorRuntime from "@babel/runtime/regenerator";
2
-
3
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
-
5
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
16
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
17
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
18
- export var createCounter = function createCounter(callback) {
19
- var dispatch = function dispatch(index, input) {
20
- var next = function next() {
21
- var nextInput = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : input;
22
- return dispatch(index + 1, nextInput);
23
- };
24
-
25
- return callback(index, input, next);
26
- };
27
-
28
- var start = function start(input) {
29
- return dispatch(0, input);
30
- };
31
-
32
- return {
33
- start: start,
34
- dispatch: dispatch
35
- };
36
- }; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
37
-
38
- export var createAsyncCounter = function createAsyncCounter(callback) {
39
- var dispatch = /*#__PURE__*/function () {
40
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(index, input) {
41
- var next;
42
- return _regeneratorRuntime.wrap(function _callee$(_context) {
43
- while (1) {
44
- switch (_context.prev = _context.next) {
45
- case 0:
46
- next = function next() {
47
- var nextInput = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : input;
48
- return dispatch(index + 1, nextInput);
49
- };
50
-
51
- return _context.abrupt("return", callback(index, input, next));
52
-
53
- case 2:
54
- case "end":
55
- return _context.stop();
56
- }
57
- }
58
- }, _callee);
59
- }));
60
-
61
- return function dispatch(_x, _x2) {
62
- return _ref.apply(this, arguments);
63
- };
64
- }();
65
-
66
- var start = function start(input) {
67
- return dispatch(0, input);
68
- };
69
-
70
- return {
71
- start: start,
72
- dispatch: dispatch
73
- };
74
- };