@modern-js/plugin 1.21.5 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -3
- package/dist/js/modern/farrow-pipeline/context.js +1 -8
- package/dist/js/modern/farrow-pipeline/counter.js +1 -3
- package/dist/js/modern/farrow-pipeline/pipeline.js +0 -16
- package/dist/js/modern/manager/async.js +8 -27
- package/dist/js/modern/manager/shared.js +3 -23
- package/dist/js/modern/manager/sync.js +10 -41
- package/dist/js/modern/utils/pluginDagSort.js +48 -0
- package/dist/js/modern/waterfall/async.js +0 -10
- package/dist/js/modern/waterfall/sync.js +0 -10
- package/dist/js/modern/workflow/async.js +0 -10
- package/dist/js/modern/workflow/parallel.js +0 -8
- package/dist/js/modern/workflow/sync.js +0 -8
- package/dist/js/node/farrow-pipeline/context.js +1 -10
- package/dist/js/node/farrow-pipeline/counter.js +1 -5
- package/dist/js/node/farrow-pipeline/index.js +0 -2
- package/dist/js/node/farrow-pipeline/pipeline.js +0 -23
- package/dist/js/node/index.js +0 -8
- package/dist/js/node/manager/async.js +8 -32
- package/dist/js/node/manager/index.js +0 -6
- package/dist/js/node/manager/shared.js +3 -32
- package/dist/js/node/manager/sync.js +10 -54
- package/dist/js/node/utils/pluginDagSort.js +55 -0
- package/dist/js/node/waterfall/async.js +0 -18
- package/dist/js/node/waterfall/index.js +0 -4
- package/dist/js/node/waterfall/sync.js +0 -18
- package/dist/js/node/workflow/async.js +0 -16
- package/dist/js/node/workflow/index.js +0 -6
- package/dist/js/node/workflow/parallel.js +0 -14
- package/dist/js/node/workflow/sync.js +0 -14
- package/dist/js/treeshaking/farrow-pipeline/context.js +1 -8
- package/dist/js/treeshaking/farrow-pipeline/counter.js +1 -3
- package/dist/js/treeshaking/farrow-pipeline/pipeline.js +0 -16
- package/dist/js/treeshaking/manager/async.js +6 -28
- package/dist/js/treeshaking/manager/shared.js +2 -46
- package/dist/js/treeshaking/manager/sync.js +9 -44
- package/dist/js/treeshaking/utils/pluginDagSort.js +74 -0
- package/dist/js/treeshaking/waterfall/async.js +0 -11
- package/dist/js/treeshaking/waterfall/sync.js +0 -8
- package/dist/js/treeshaking/workflow/async.js +0 -16
- package/dist/js/treeshaking/workflow/parallel.js +0 -8
- package/dist/js/treeshaking/workflow/sync.js +0 -6
- package/dist/types/manager/async.d.ts +0 -9
- package/dist/types/manager/sync.d.ts +0 -9
- package/dist/types/manager/types.d.ts +2 -9
- package/dist/types/utils/pluginDagSort.d.ts +1 -0
- package/package.json +5 -28
package/CHANGELOG.md
CHANGED
@@ -1,10 +1,29 @@
|
|
1
1
|
# @modern-js/plugin
|
2
2
|
|
3
|
-
##
|
3
|
+
## 2.0.0-beta.1
|
4
4
|
|
5
|
-
|
5
|
+
### Major Changes
|
6
6
|
|
7
|
-
|
7
|
+
- dda38c9: chore: v2
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- b8bbe036c7: feat: change type logic
|
12
|
+
feat: 修改类型相关的逻辑
|
13
|
+
- f179749: fix: the sortPlugins api not work
|
14
|
+
|
15
|
+
fix: sortPlugins 没有真实生效
|
16
|
+
|
17
|
+
## 2.0.0-beta.0
|
18
|
+
|
19
|
+
### Major Changes
|
20
|
+
|
21
|
+
- dda38c9: chore: v2
|
22
|
+
|
23
|
+
### Patch Changes
|
24
|
+
|
25
|
+
- b8bbe036c: feat: change type logic
|
26
|
+
feat: 修改类型相关的逻辑
|
8
27
|
|
9
28
|
## 1.21.2
|
10
29
|
|
@@ -2,29 +2,23 @@
|
|
2
2
|
* modified from https://github.com/farrow-js/farrow/tree/master/packages/farrow-pipeline
|
3
3
|
* license at https://github.com/farrow-js/farrow/blob/master/LICENSE
|
4
4
|
*/
|
5
|
+
|
5
6
|
export const createContext = value => {
|
6
7
|
let currentValue;
|
7
|
-
|
8
8
|
const create = value => {
|
9
9
|
currentValue = value;
|
10
|
-
|
11
10
|
const use = () => ({
|
12
11
|
get value() {
|
13
12
|
return currentValue;
|
14
13
|
},
|
15
|
-
|
16
14
|
set value(v) {
|
17
15
|
currentValue = v;
|
18
16
|
}
|
19
|
-
|
20
17
|
});
|
21
|
-
|
22
18
|
const get = () => currentValue;
|
23
|
-
|
24
19
|
const set = v => {
|
25
20
|
currentValue = v;
|
26
21
|
};
|
27
|
-
|
28
22
|
const Context = {
|
29
23
|
create,
|
30
24
|
use,
|
@@ -33,6 +27,5 @@ export const createContext = value => {
|
|
33
27
|
};
|
34
28
|
return Context;
|
35
29
|
};
|
36
|
-
|
37
30
|
return create(value);
|
38
31
|
};
|
@@ -2,15 +2,13 @@
|
|
2
2
|
* modified from https://github.com/farrow-js/farrow/tree/master/packages/farrow-pipeline
|
3
3
|
* license at https://github.com/farrow-js/farrow/blob/master/LICENSE
|
4
4
|
*/
|
5
|
+
|
5
6
|
export const createCounter = callback => {
|
6
7
|
const dispatch = (index, input) => {
|
7
8
|
const next = (nextInput = input) => dispatch(index + 1, nextInput);
|
8
|
-
|
9
9
|
return callback(index, input, next);
|
10
10
|
};
|
11
|
-
|
12
11
|
const start = input => dispatch(0, input);
|
13
|
-
|
14
12
|
return {
|
15
13
|
start,
|
16
14
|
dispatch
|
@@ -1,9 +1,6 @@
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
3
|
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
4
|
/**
|
8
5
|
* modified from https://github.com/farrow-js/farrow/tree/master/packages/farrow-pipeline
|
9
6
|
* license at https://github.com/farrow-js/farrow/blob/master/LICENSE
|
@@ -13,55 +10,42 @@ import { createCounter } from "./counter";
|
|
13
10
|
export { createContext };
|
14
11
|
export const isPipeline = input => Boolean(input === null || input === void 0 ? void 0 : input[PipelineSymbol]);
|
15
12
|
const PipelineSymbol = Symbol.for('MODERN_PIPELINE');
|
16
|
-
|
17
13
|
const getMiddleware = input => {
|
18
14
|
if (typeof input === 'function') {
|
19
15
|
return input;
|
20
16
|
} else if (input && typeof input.middleware === 'function') {
|
21
17
|
return input.middleware;
|
22
18
|
}
|
23
|
-
|
24
19
|
throw new Error(`${input} is not a Middleware`);
|
25
20
|
};
|
26
|
-
|
27
21
|
export const createPipeline = () => {
|
28
22
|
const middlewares = [];
|
29
|
-
|
30
23
|
const use = (...inputs) => {
|
31
24
|
middlewares.push(...inputs.map(getMiddleware));
|
32
25
|
return pipeline;
|
33
26
|
};
|
34
|
-
|
35
27
|
const createCurrentCounter = onLast => {
|
36
28
|
return createCounter((index, input, next) => {
|
37
29
|
if (index >= middlewares.length) {
|
38
30
|
if (onLast) {
|
39
31
|
return onLast(input);
|
40
32
|
}
|
41
|
-
|
42
33
|
throw new Error(`Expect returning a value, but all middlewares just calling next()`);
|
43
34
|
}
|
44
|
-
|
45
35
|
return middlewares[index](input, next);
|
46
36
|
});
|
47
37
|
};
|
48
|
-
|
49
38
|
const currentCounter = createCurrentCounter();
|
50
|
-
|
51
39
|
const getCounter = options => {
|
52
40
|
if (!options) {
|
53
41
|
return currentCounter;
|
54
42
|
}
|
55
|
-
|
56
43
|
return createCurrentCounter(options === null || options === void 0 ? void 0 : options.onLast);
|
57
44
|
};
|
58
|
-
|
59
45
|
const run = (input, options) => getCounter(options).start(input);
|
60
|
-
|
61
46
|
const middleware = (input, next) => run(input, {
|
62
47
|
onLast: next
|
63
48
|
});
|
64
|
-
|
65
49
|
const pipeline = {
|
66
50
|
[PipelineSymbol]: true,
|
67
51
|
use,
|
@@ -1,74 +1,62 @@
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
3
|
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
4
|
import { generateRunner, DEFAULT_OPTIONS } from "./sync";
|
8
5
|
import { checkPlugins, isObject, hasOwnProperty, sortPlugins, includePlugin } from "./shared";
|
9
6
|
const ASYNC_PLUGIN_SYMBOL = 'ASYNC_PLUGIN_SYMBOL';
|
10
7
|
export const createAsyncManager = (hooks, api) => {
|
11
8
|
let index = 0;
|
12
9
|
let runners;
|
13
|
-
|
14
10
|
let currentHooks = _objectSpread({}, hooks);
|
15
|
-
|
16
11
|
const useRunner = () => runners;
|
17
|
-
|
18
12
|
const registerHook = extraHooks => {
|
19
13
|
currentHooks = _objectSpread(_objectSpread({}, extraHooks), currentHooks);
|
20
14
|
};
|
21
|
-
|
22
15
|
const isPlugin = input => isObject(input) && hasOwnProperty(input, ASYNC_PLUGIN_SYMBOL) && input[ASYNC_PLUGIN_SYMBOL] === ASYNC_PLUGIN_SYMBOL;
|
23
|
-
|
24
16
|
const pluginAPI = _objectSpread(_objectSpread({}, api), {}, {
|
25
17
|
useHookRunners: useRunner
|
26
18
|
});
|
27
|
-
|
28
19
|
const clone = overrideAPI => {
|
29
20
|
let plugins = [];
|
30
|
-
|
31
21
|
const addPlugin = plugin => {
|
32
22
|
if (!includePlugin(plugins, plugin)) {
|
33
23
|
plugins.push(_objectSpread({}, plugin));
|
34
24
|
}
|
35
25
|
};
|
36
|
-
|
37
26
|
const usePlugin = (...input) => {
|
38
27
|
for (const plugin of input) {
|
39
28
|
// already created by createPlugin
|
40
29
|
if (isPlugin(plugin)) {
|
41
30
|
addPlugin(plugin);
|
42
|
-
}
|
31
|
+
}
|
32
|
+
// using function to return plugin options
|
43
33
|
else if (typeof plugin === 'function') {
|
44
34
|
const options = plugin();
|
45
35
|
addPlugin(createPlugin(options.setup, options));
|
46
|
-
}
|
36
|
+
}
|
37
|
+
// plain plugin object
|
47
38
|
else if (isObject(plugin)) {
|
48
39
|
addPlugin(createPlugin(plugin.setup, plugin));
|
49
|
-
}
|
40
|
+
}
|
41
|
+
// unknown plugin
|
50
42
|
else {
|
51
43
|
console.warn(`Unknown plugin: ${JSON.stringify(plugin)}`);
|
52
44
|
}
|
53
45
|
}
|
54
|
-
|
55
46
|
return manager;
|
56
47
|
};
|
57
|
-
|
58
|
-
|
48
|
+
const createPlugin = (
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
59
50
|
setup = () => {}, options = {}) => {
|
60
51
|
var _options$usePlugins;
|
61
|
-
|
62
52
|
if ((_options$usePlugins = options.usePlugins) !== null && _options$usePlugins !== void 0 && _options$usePlugins.length) {
|
63
53
|
options.usePlugins.forEach(plugin => {
|
64
54
|
usePlugin(createPlugin(plugin.setup, plugin));
|
65
55
|
});
|
66
56
|
}
|
67
|
-
|
68
57
|
if (options.registerHook) {
|
69
58
|
registerHook(options.registerHook);
|
70
59
|
}
|
71
|
-
|
72
60
|
return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
|
73
61
|
name: `No.${index++} plugin`
|
74
62
|
}, options), {}, {
|
@@ -76,24 +64,18 @@ export const createAsyncManager = (hooks, api) => {
|
|
76
64
|
setup
|
77
65
|
});
|
78
66
|
};
|
79
|
-
|
80
67
|
const clear = () => {
|
81
68
|
plugins = [];
|
82
69
|
};
|
83
|
-
|
84
70
|
const init = async () => {
|
85
71
|
const sortedPlugins = sortPlugins(plugins);
|
86
|
-
|
87
72
|
const mergedPluginAPI = _objectSpread(_objectSpread({}, pluginAPI), overrideAPI);
|
88
|
-
|
89
73
|
checkPlugins(sortedPlugins);
|
90
74
|
const hooksList = await Promise.all(sortedPlugins.map(plugin => plugin.setup(mergedPluginAPI)));
|
91
75
|
runners = generateRunner(hooksList, currentHooks);
|
92
76
|
return runners;
|
93
77
|
};
|
94
|
-
|
95
78
|
const run = cb => cb();
|
96
|
-
|
97
79
|
const manager = {
|
98
80
|
createPlugin,
|
99
81
|
isPlugin,
|
@@ -107,6 +89,5 @@ export const createAsyncManager = (hooks, api) => {
|
|
107
89
|
};
|
108
90
|
return manager;
|
109
91
|
};
|
110
|
-
|
111
92
|
return clone();
|
112
93
|
};
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { dagSort } from "../utils/pluginDagSort";
|
1
2
|
export const checkPlugins = plugins => {
|
2
3
|
plugins.forEach(origin => {
|
3
4
|
origin.rivals.forEach(rival => {
|
@@ -15,29 +16,8 @@ export const checkPlugins = plugins => {
|
|
15
16
|
});
|
16
17
|
};
|
17
18
|
export function sortPlugins(input) {
|
18
|
-
|
19
|
-
|
20
|
-
for (let i = 0; i < plugins.length; i++) {
|
21
|
-
const plugin = plugins[i];
|
22
|
-
|
23
|
-
for (const pre of plugin.pre) {
|
24
|
-
for (let j = i + 1; j < plugins.length; j++) {
|
25
|
-
if (plugins[j].name === pre) {
|
26
|
-
plugins = [...plugins.slice(0, i), plugins[j], ...plugins.slice(i, j), ...plugins.slice(j + 1, plugins.length)];
|
27
|
-
}
|
28
|
-
}
|
29
|
-
}
|
30
|
-
|
31
|
-
for (const post of plugin.post) {
|
32
|
-
for (let j = 0; j < i; j++) {
|
33
|
-
if (plugins[j].name === post) {
|
34
|
-
plugins = [...plugins.slice(0, j), ...plugins.slice(j + 1, i + 1), plugins[j], ...plugins.slice(i + 1, plugins.length)];
|
35
|
-
}
|
36
|
-
}
|
37
|
-
}
|
38
|
-
}
|
39
|
-
|
40
|
-
return plugins;
|
19
|
+
const plugins = input.slice();
|
20
|
+
return dagSort(plugins);
|
41
21
|
}
|
42
22
|
export const includePlugin = (plugins, input) => plugins.some(plugin => plugin.name === input.name);
|
43
23
|
export const isObject = obj => obj !== null && typeof obj === 'object';
|
@@ -1,9 +1,6 @@
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
3
|
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
4
|
import { isPipeline, createPipeline } from "../farrow-pipeline";
|
8
5
|
import { isWaterfall, createWaterfall, isAsyncWaterfall, createAsyncWaterfall } from "../waterfall";
|
9
6
|
import { isWorkflow, createWorkflow, isAsyncWorkflow, createAsyncWorkflow, isParallelWorkflow, createParallelWorkflow } from "../workflow";
|
@@ -21,64 +18,56 @@ export const DEFAULT_OPTIONS = {
|
|
21
18
|
export const createManager = (hooks, api) => {
|
22
19
|
let index = 0;
|
23
20
|
let runners;
|
24
|
-
|
25
21
|
let currentHooks = _objectSpread({}, hooks);
|
26
|
-
|
27
22
|
const useRunner = () => runners;
|
28
|
-
|
29
23
|
const registerHook = extraHooks => {
|
30
24
|
currentHooks = _objectSpread(_objectSpread({}, extraHooks), currentHooks);
|
31
25
|
};
|
32
|
-
|
33
26
|
const isPlugin = input => isObject(input) && hasOwnProperty(input, SYNC_PLUGIN_SYMBOL) && input[SYNC_PLUGIN_SYMBOL] === SYNC_PLUGIN_SYMBOL;
|
34
|
-
|
35
27
|
const pluginAPI = _objectSpread(_objectSpread({}, api), {}, {
|
36
28
|
useHookRunners: useRunner
|
37
29
|
});
|
38
|
-
|
39
30
|
const clone = overrideAPI => {
|
40
31
|
let plugins = [];
|
41
|
-
|
42
32
|
const addPlugin = plugin => {
|
43
33
|
if (!includePlugin(plugins, plugin)) {
|
44
34
|
plugins.push(_objectSpread({}, plugin));
|
45
35
|
}
|
46
36
|
};
|
47
|
-
|
48
37
|
const usePlugin = (...input) => {
|
49
38
|
input.forEach(plugin => {
|
50
39
|
// already created by createPlugin
|
51
40
|
if (isPlugin(plugin)) {
|
52
41
|
addPlugin(plugin);
|
53
|
-
}
|
42
|
+
}
|
43
|
+
// using function to return plugin options
|
54
44
|
else if (typeof plugin === 'function') {
|
55
45
|
const options = plugin();
|
56
46
|
addPlugin(createPlugin(options.setup, options));
|
57
|
-
}
|
47
|
+
}
|
48
|
+
// plain plugin object
|
58
49
|
else if (isObject(plugin)) {
|
59
50
|
addPlugin(createPlugin(plugin.setup, plugin));
|
60
|
-
}
|
51
|
+
}
|
52
|
+
// unknown plugin
|
61
53
|
else {
|
62
54
|
console.warn(`Unknown plugin: ${JSON.stringify(plugin)}`);
|
63
55
|
}
|
64
56
|
});
|
65
57
|
return manager;
|
66
58
|
};
|
67
|
-
|
68
|
-
|
59
|
+
const createPlugin = (
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
69
61
|
setup = () => {}, options = {}) => {
|
70
62
|
var _options$usePlugins;
|
71
|
-
|
72
63
|
if ((_options$usePlugins = options.usePlugins) !== null && _options$usePlugins !== void 0 && _options$usePlugins.length) {
|
73
64
|
options.usePlugins.forEach(plugin => {
|
74
65
|
usePlugin(createPlugin(plugin.setup, plugin));
|
75
66
|
});
|
76
67
|
}
|
77
|
-
|
78
68
|
if (options.registerHook) {
|
79
69
|
registerHook(options.registerHook);
|
80
70
|
}
|
81
|
-
|
82
71
|
return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
|
83
72
|
name: `No.${index++} plugin`
|
84
73
|
}, options), {}, {
|
@@ -86,24 +75,18 @@ export const createManager = (hooks, api) => {
|
|
86
75
|
setup
|
87
76
|
});
|
88
77
|
};
|
89
|
-
|
90
78
|
const clear = () => {
|
91
79
|
plugins = [];
|
92
80
|
};
|
93
|
-
|
94
81
|
const init = () => {
|
95
82
|
const sortedPlugins = sortPlugins(plugins);
|
96
|
-
|
97
83
|
const mergedPluginAPI = _objectSpread(_objectSpread({}, pluginAPI), overrideAPI);
|
98
|
-
|
99
84
|
checkPlugins(sortedPlugins);
|
100
85
|
const hooksList = sortedPlugins.map(plugin => plugin.setup(mergedPluginAPI));
|
101
86
|
runners = generateRunner(hooksList, currentHooks);
|
102
87
|
return runners;
|
103
88
|
};
|
104
|
-
|
105
89
|
const run = cb => cb();
|
106
|
-
|
107
90
|
const manager = {
|
108
91
|
createPlugin,
|
109
92
|
isPlugin,
|
@@ -117,72 +100,58 @@ export const createManager = (hooks, api) => {
|
|
117
100
|
};
|
118
101
|
return manager;
|
119
102
|
};
|
120
|
-
|
121
103
|
return clone();
|
122
104
|
};
|
123
105
|
export const generateRunner = (hooksList, hooksMap) => {
|
124
106
|
const runner = {};
|
125
107
|
const cloneShape = cloneHooksMap(hooksMap);
|
126
|
-
|
127
108
|
if (hooksMap) {
|
128
109
|
for (const key in cloneShape) {
|
129
110
|
for (const hooks of hooksList) {
|
130
111
|
if (!hooks) {
|
131
112
|
continue;
|
132
113
|
}
|
133
|
-
|
134
114
|
if (hooks[key]) {
|
135
115
|
cloneShape[key].use(hooks[key]);
|
136
116
|
}
|
137
|
-
}
|
117
|
+
}
|
118
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
138
119
|
// @ts-expect-error
|
139
|
-
|
140
|
-
|
141
120
|
runner[key] = (input, options) => cloneShape[key].run(input, _objectSpread({}, options));
|
142
121
|
}
|
143
122
|
}
|
144
|
-
|
145
123
|
return runner;
|
146
124
|
};
|
147
125
|
export const cloneHook = hook => {
|
148
126
|
if (isWaterfall(hook)) {
|
149
127
|
return createWaterfall();
|
150
128
|
}
|
151
|
-
|
152
129
|
if (isAsyncWaterfall(hook)) {
|
153
130
|
return createAsyncWaterfall();
|
154
131
|
}
|
155
|
-
|
156
132
|
if (isWorkflow(hook)) {
|
157
133
|
return createWorkflow();
|
158
134
|
}
|
159
|
-
|
160
135
|
if (isAsyncWorkflow(hook)) {
|
161
136
|
return createAsyncWorkflow();
|
162
137
|
}
|
163
|
-
|
164
138
|
if (isParallelWorkflow(hook)) {
|
165
139
|
return createParallelWorkflow();
|
166
140
|
}
|
167
|
-
|
168
141
|
if (isPipeline(hook)) {
|
169
142
|
return createPipeline();
|
170
143
|
}
|
171
|
-
|
172
144
|
throw new Error(`Unknown hook: ${hook}`);
|
173
145
|
};
|
174
146
|
export const cloneHooksMap = record => {
|
175
147
|
if (!record) {
|
176
148
|
return record;
|
177
149
|
}
|
178
|
-
|
179
150
|
const result = {};
|
180
|
-
|
181
151
|
for (const key in record) {
|
182
152
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
183
153
|
// @ts-expect-error
|
184
154
|
result[key] = cloneHook(record[key]);
|
185
155
|
}
|
186
|
-
|
187
156
|
return result;
|
188
157
|
};
|
@@ -0,0 +1,48 @@
|
|
1
|
+
export const dagSort = (plugins, key = 'name', preKey = 'pre', postKey = 'post') => {
|
2
|
+
let allLines = [];
|
3
|
+
function getPluginByAny(q) {
|
4
|
+
const target = plugins.find(item => typeof q === 'string' ? item[key] === q : item[key] === q[key]);
|
5
|
+
// current plugin design can't guarantee the plugins in pre/post existed
|
6
|
+
if (!target) {
|
7
|
+
throw new Error(`plugin ${q} not existed`);
|
8
|
+
}
|
9
|
+
return target;
|
10
|
+
}
|
11
|
+
plugins.forEach(item => {
|
12
|
+
item[preKey].forEach(p => {
|
13
|
+
// compatibility: do not add the plugin-name that plugins not have
|
14
|
+
if (plugins.find(ap => ap.name === p)) {
|
15
|
+
allLines.push([getPluginByAny(p)[key], getPluginByAny(item)[key]]);
|
16
|
+
}
|
17
|
+
});
|
18
|
+
item[postKey].forEach(pt => {
|
19
|
+
// compatibility: do not add the plugin-name that plugins not have
|
20
|
+
if (plugins.find(ap => ap.name === pt)) {
|
21
|
+
allLines.push([getPluginByAny(item)[key], getPluginByAny(pt)[key]]);
|
22
|
+
}
|
23
|
+
});
|
24
|
+
});
|
25
|
+
|
26
|
+
// search the zero input plugin
|
27
|
+
let zeroEndPoints = plugins.filter(item => !allLines.find(l => l[1] === item[key]));
|
28
|
+
const sortedPoint = [];
|
29
|
+
while (zeroEndPoints.length) {
|
30
|
+
const zep = zeroEndPoints.shift();
|
31
|
+
sortedPoint.push(getPluginByAny(zep));
|
32
|
+
allLines = allLines.filter(l => l[0] !== getPluginByAny(zep)[key]);
|
33
|
+
const restPoints = plugins.filter(item => !sortedPoint.find(sp => sp[key] === item[key]));
|
34
|
+
zeroEndPoints = restPoints.filter(
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-loop-func
|
36
|
+
item => !allLines.find(l => l[1] === item[key]));
|
37
|
+
}
|
38
|
+
// if has ring, throw error
|
39
|
+
if (allLines.length) {
|
40
|
+
const restInRingPoints = {};
|
41
|
+
allLines.forEach(l => {
|
42
|
+
restInRingPoints[l[0]] = true;
|
43
|
+
restInRingPoints[l[1]] = true;
|
44
|
+
});
|
45
|
+
throw new Error(`plugins dependences has loop: ${Object.keys(restInRingPoints).join(',')}`);
|
46
|
+
}
|
47
|
+
return sortedPoint;
|
48
|
+
};
|
@@ -1,9 +1,6 @@
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
3
|
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
4
|
import { createAsyncPipeline } from "../farrow-pipeline";
|
8
5
|
const ASYNC_WATERFALL_SYMBOL = Symbol.for('MODERN_ASYNC_WATERFALL');
|
9
6
|
export const getAsyncBrook = input => {
|
@@ -12,36 +9,29 @@ export const getAsyncBrook = input => {
|
|
12
9
|
} else if (input && typeof input.middleware === 'function') {
|
13
10
|
return input.middleware;
|
14
11
|
}
|
15
|
-
|
16
12
|
throw new Error(`${input} is not a AsyncBrook or { brook: AsyncBrook }`);
|
17
13
|
};
|
18
14
|
export const createAsyncWaterfall = () => {
|
19
15
|
const pipeline = createAsyncPipeline();
|
20
|
-
|
21
16
|
const use = (...input) => {
|
22
17
|
pipeline.use(...input.map(getAsyncBrook).map(mapAsyncBrookToAsyncMiddleware));
|
23
18
|
return waterfall;
|
24
19
|
};
|
25
|
-
|
26
20
|
const run = (input, options) => pipeline.run(input, _objectSpread(_objectSpread({}, options), {}, {
|
27
21
|
onLast: input => input
|
28
22
|
}));
|
29
|
-
|
30
23
|
const middleware = input => {
|
31
24
|
return pipeline.run(input, {
|
32
25
|
onLast: input => input
|
33
26
|
});
|
34
27
|
};
|
35
|
-
|
36
28
|
const waterfall = _objectSpread(_objectSpread({}, pipeline), {}, {
|
37
29
|
use,
|
38
30
|
run,
|
39
31
|
middleware,
|
40
32
|
[ASYNC_WATERFALL_SYMBOL]: true
|
41
33
|
});
|
42
|
-
|
43
34
|
return waterfall;
|
44
35
|
};
|
45
36
|
export const isAsyncWaterfall = input => Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_WATERFALL_SYMBOL]);
|
46
|
-
|
47
37
|
const mapAsyncBrookToAsyncMiddleware = brook => async (input, next) => next(await brook(input));
|
@@ -1,9 +1,6 @@
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
3
|
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
4
|
import { createPipeline } from "../farrow-pipeline";
|
8
5
|
const WATERFALL_SYMBOL = Symbol.for('MODERN_WATERFALL');
|
9
6
|
export const getBrook = input => {
|
@@ -12,36 +9,29 @@ export const getBrook = input => {
|
|
12
9
|
} else if (input && typeof input.middleware === 'function') {
|
13
10
|
return input.middleware;
|
14
11
|
}
|
15
|
-
|
16
12
|
throw new Error(`${input} is not a Brook or { brook: Brook }`);
|
17
13
|
};
|
18
14
|
export const createWaterfall = () => {
|
19
15
|
const pipeline = createPipeline();
|
20
|
-
|
21
16
|
const use = (...brooks) => {
|
22
17
|
pipeline.use(...brooks.map(getBrook).map(mapBrookToMiddleware));
|
23
18
|
return waterfall;
|
24
19
|
};
|
25
|
-
|
26
20
|
const run = (input, options) => pipeline.run(input, _objectSpread(_objectSpread({}, options), {}, {
|
27
21
|
onLast: input => input
|
28
22
|
}));
|
29
|
-
|
30
23
|
const middleware = input => {
|
31
24
|
return pipeline.run(input, {
|
32
25
|
onLast: input => input
|
33
26
|
});
|
34
27
|
};
|
35
|
-
|
36
28
|
const waterfall = _objectSpread(_objectSpread({}, pipeline), {}, {
|
37
29
|
use,
|
38
30
|
run,
|
39
31
|
middleware,
|
40
32
|
[WATERFALL_SYMBOL]: true
|
41
33
|
});
|
42
|
-
|
43
34
|
return waterfall;
|
44
35
|
};
|
45
36
|
export const isWaterfall = input => Boolean(input === null || input === void 0 ? void 0 : input[WATERFALL_SYMBOL]);
|
46
|
-
|
47
37
|
const mapBrookToMiddleware = brook => (input, next) => next(brook(input));
|
@@ -1,43 +1,33 @@
|
|
1
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
3
|
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
4
|
import { createAsyncPipeline } from "../farrow-pipeline";
|
8
5
|
const ASYNC_WORKFLOW_SYMBOL = Symbol.for('MODERN_ASYNC_WORKFLOW');
|
9
6
|
export const isAsyncWorkflow = input => Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_WORKFLOW_SYMBOL]);
|
10
7
|
export const createAsyncWorkflow = () => {
|
11
8
|
const pipeline = createAsyncPipeline();
|
12
|
-
|
13
9
|
const use = (...input) => {
|
14
10
|
pipeline.use(...input.map(mapAsyncWorkerToAsyncMiddleware));
|
15
11
|
return workflow;
|
16
12
|
};
|
17
|
-
|
18
13
|
const run = async input => {
|
19
14
|
const result = pipeline.run(input, {
|
20
15
|
onLast: () => []
|
21
16
|
});
|
22
|
-
|
23
17
|
if (isPromise(result)) {
|
24
18
|
return result.then(result => result.filter(Boolean));
|
25
19
|
} else {
|
26
20
|
return result.filter(Boolean);
|
27
21
|
}
|
28
22
|
};
|
29
|
-
|
30
23
|
const workflow = _objectSpread(_objectSpread({}, pipeline), {}, {
|
31
24
|
use,
|
32
25
|
run,
|
33
26
|
[ASYNC_WORKFLOW_SYMBOL]: true
|
34
27
|
});
|
35
|
-
|
36
28
|
return workflow;
|
37
29
|
};
|
38
|
-
|
39
30
|
const mapAsyncWorkerToAsyncMiddleware = worker => async (input, next) => [await worker(input), ...(await next(input))];
|
40
|
-
|
41
31
|
function isPromise(obj) {
|
42
32
|
return Boolean(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
43
33
|
}
|