@modern-js/plugin 1.2.0 → 1.3.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 +16 -0
- package/dist/js/modern/manager/async.js +59 -28
- package/dist/js/modern/manager/index.js +2 -1
- package/dist/js/modern/manager/sync.js +79 -60
- package/dist/js/modern/manager/types.js +0 -0
- package/dist/js/modern/waterfall/async.js +4 -5
- package/dist/js/modern/waterfall/sync.js +0 -1
- package/dist/js/modern/workflow/async.js +1 -2
- package/dist/js/modern/workflow/sync.js +0 -1
- package/dist/js/node/manager/async.js +57 -25
- package/dist/js/node/manager/index.js +13 -0
- package/dist/js/node/manager/sync.js +84 -58
- package/dist/js/node/manager/types.js +0 -0
- package/dist/js/node/waterfall/async.js +4 -5
- package/dist/js/node/waterfall/sync.js +0 -1
- package/dist/js/node/workflow/async.js +1 -2
- package/dist/js/node/workflow/sync.js +0 -1
- package/dist/js/treeshaking/manager/async.js +63 -34
- package/dist/js/treeshaking/manager/index.js +2 -1
- package/dist/js/treeshaking/manager/sync.js +85 -62
- package/dist/js/treeshaking/manager/types.js +0 -0
- package/dist/js/treeshaking/waterfall/async.js +4 -5
- package/dist/js/treeshaking/waterfall/sync.js +0 -1
- package/dist/js/treeshaking/workflow/async.js +1 -2
- package/dist/js/treeshaking/workflow/sync.js +0 -1
- package/dist/types/manager/async.d.ts +60 -21
- package/dist/types/manager/index.d.ts +2 -1
- package/dist/types/manager/sync.d.ts +74 -43
- package/dist/types/manager/types.d.ts +41 -0
- package/dist/types/waterfall/async.d.ts +2 -2
- package/package.json +2 -1
- package/tests/async.test.ts +143 -22
- package/tests/fixtures/async/core/index.ts +12 -4
- package/tests/fixtures/async/dynamic/foo.ts +2 -2
- package/tests/fixtures/sync/core/index.ts +9 -4
- package/tests/fixtures/sync/dynamic/foo.ts +2 -2
- package/tests/sync.test.ts +136 -21
- package/src/index.ts +0 -5
- package/src/manager/async.ts +0 -248
- package/src/manager/index.ts +0 -3
- package/src/manager/runner.ts +0 -15
- package/src/manager/sync.ts +0 -458
- package/src/waterfall/async.ts +0 -109
- package/src/waterfall/index.ts +0 -2
- package/src/waterfall/sync.ts +0 -110
- package/src/workflow/async.ts +0 -96
- package/src/workflow/index.ts +0 -3
- package/src/workflow/parallel.ts +0 -97
- package/src/workflow/sync.ts +0 -82
package/src/manager/async.ts
DELETED
@@ -1,248 +0,0 @@
|
|
1
|
-
import { runWithContainer, createContainer } from 'farrow-pipeline';
|
2
|
-
import {
|
3
|
-
ProgressRecord,
|
4
|
-
Progresses2Threads,
|
5
|
-
Progresses2Runners,
|
6
|
-
PluginOptions,
|
7
|
-
ClearDraftProgress,
|
8
|
-
InitOptions,
|
9
|
-
generateRunner,
|
10
|
-
hasOwnProperty,
|
11
|
-
DEFAULT_OPTIONS,
|
12
|
-
} from './sync';
|
13
|
-
import { useRunner } from './runner';
|
14
|
-
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
16
|
-
export type AsyncInitializer<O> = () => void | O | Promise<O | void>;
|
17
|
-
|
18
|
-
const ASYNC_PLUGIN_SYMBOL = 'ASYNC_PLUGIN_SYMBOL';
|
19
|
-
|
20
|
-
export type AsyncPlugin<O> = {
|
21
|
-
initializer: AsyncInitializer<O>;
|
22
|
-
ASYNC_PLUGIN_SYMBOL: typeof ASYNC_PLUGIN_SYMBOL;
|
23
|
-
} & Required<PluginOptions>;
|
24
|
-
|
25
|
-
export type IndexAsyncPlugin<O> = AsyncPlugin<O> & {
|
26
|
-
index: number;
|
27
|
-
};
|
28
|
-
|
29
|
-
export type AsyncPlugins<O> = AsyncPlugin<O>[];
|
30
|
-
export type AsyncIndexPlugins<O> = IndexAsyncPlugin<O>[];
|
31
|
-
|
32
|
-
export type AsyncPluginFromAsyncManager<M extends AsyncManager<any, any>> =
|
33
|
-
M extends AsyncManager<infer EP, infer PR>
|
34
|
-
? AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>
|
35
|
-
: never;
|
36
|
-
|
37
|
-
export type PluginFromAsyncManager<M extends AsyncManager<any, any>> =
|
38
|
-
M extends AsyncManager<infer EP, infer PR>
|
39
|
-
? AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>
|
40
|
-
: never;
|
41
|
-
|
42
|
-
export type AsyncManager<
|
43
|
-
EP extends Record<string, any>,
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
45
|
-
PR extends ProgressRecord | void = void,
|
46
|
-
> = {
|
47
|
-
createPlugin: (
|
48
|
-
initializer: AsyncInitializer<
|
49
|
-
Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>
|
50
|
-
>,
|
51
|
-
options?: PluginOptions,
|
52
|
-
) => AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>;
|
53
|
-
isPlugin: (
|
54
|
-
input: Record<string, unknown>,
|
55
|
-
) => input is AsyncPlugin<
|
56
|
-
Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>
|
57
|
-
>;
|
58
|
-
usePlugin: (
|
59
|
-
...input: AsyncPlugins<
|
60
|
-
Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>
|
61
|
-
>
|
62
|
-
) => AsyncManager<EP, PR>;
|
63
|
-
init: (
|
64
|
-
options?: InitOptions,
|
65
|
-
) => Promise<Progresses2Runners<PR & ClearDraftProgress<EP>>>;
|
66
|
-
run: <O>(cb: () => O, options?: InitOptions) => O;
|
67
|
-
registe: (newShape: Partial<EP>) => void;
|
68
|
-
clone: () => AsyncManager<EP, PR>;
|
69
|
-
clear: () => void;
|
70
|
-
useRunner: () => Progresses2Runners<PR & ClearDraftProgress<EP>>;
|
71
|
-
};
|
72
|
-
|
73
|
-
export const createAsyncManager = <
|
74
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
75
|
-
EP extends Record<string, any> = {},
|
76
|
-
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
77
|
-
PR extends ProgressRecord | void = void,
|
78
|
-
>(
|
79
|
-
processes?: PR,
|
80
|
-
): AsyncManager<EP, PR> => {
|
81
|
-
let index = 0;
|
82
|
-
const createPlugin: AsyncManager<EP, PR>['createPlugin'] = (
|
83
|
-
initializer,
|
84
|
-
options = {},
|
85
|
-
) => ({
|
86
|
-
...DEFAULT_OPTIONS,
|
87
|
-
name: `No.${index++} plugin`,
|
88
|
-
...options,
|
89
|
-
ASYNC_PLUGIN_SYMBOL,
|
90
|
-
initializer,
|
91
|
-
});
|
92
|
-
|
93
|
-
const isPlugin: AsyncManager<EP, PR>['isPlugin'] = (
|
94
|
-
input,
|
95
|
-
): input is AsyncPlugin<
|
96
|
-
Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>
|
97
|
-
> =>
|
98
|
-
hasOwnProperty(input, ASYNC_PLUGIN_SYMBOL) &&
|
99
|
-
input[ASYNC_PLUGIN_SYMBOL] === ASYNC_PLUGIN_SYMBOL;
|
100
|
-
|
101
|
-
const registe: AsyncManager<EP, PR>['registe'] = extraProcesses => {
|
102
|
-
// eslint-disable-next-line no-param-reassign
|
103
|
-
processes = {
|
104
|
-
...extraProcesses,
|
105
|
-
...processes,
|
106
|
-
} as any;
|
107
|
-
};
|
108
|
-
|
109
|
-
const clone = () => {
|
110
|
-
let plugins: AsyncIndexPlugins<
|
111
|
-
Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>
|
112
|
-
> = [];
|
113
|
-
|
114
|
-
const usePlugin: AsyncManager<EP, PR>['usePlugin'] = (...input) => {
|
115
|
-
for (const plugin of input) {
|
116
|
-
if (isPlugin(plugin)) {
|
117
|
-
if (!includeAsyncPlugin(plugins, plugin)) {
|
118
|
-
plugins.push({
|
119
|
-
...plugin,
|
120
|
-
index: plugins.length,
|
121
|
-
});
|
122
|
-
}
|
123
|
-
} else {
|
124
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
125
|
-
// @ts-expect-error
|
126
|
-
console.warn(`Unknown plugin: ${plugin.name}`);
|
127
|
-
}
|
128
|
-
}
|
129
|
-
|
130
|
-
return manager;
|
131
|
-
};
|
132
|
-
|
133
|
-
const clear = () => {
|
134
|
-
plugins = [];
|
135
|
-
};
|
136
|
-
|
137
|
-
const currentContainer = createContainer();
|
138
|
-
|
139
|
-
const init: AsyncManager<EP, PR>['init'] = async options => {
|
140
|
-
const container = options?.container || currentContainer;
|
141
|
-
|
142
|
-
const sortedPlugins = sortAsyncPlugins(plugins);
|
143
|
-
|
144
|
-
checkAsyncPlugins(sortedPlugins);
|
145
|
-
|
146
|
-
const hooksList = await Promise.all(
|
147
|
-
sortedPlugins.map(plugin =>
|
148
|
-
runWithContainer(() => plugin.initializer(), container),
|
149
|
-
),
|
150
|
-
);
|
151
|
-
|
152
|
-
return generateRunner<EP, PR>(hooksList, container, processes);
|
153
|
-
};
|
154
|
-
|
155
|
-
const run: AsyncManager<EP, PR>['run'] = (cb, options) => {
|
156
|
-
const container = options?.container || currentContainer;
|
157
|
-
|
158
|
-
return runWithContainer(cb, container);
|
159
|
-
};
|
160
|
-
|
161
|
-
const manager = {
|
162
|
-
createPlugin,
|
163
|
-
isPlugin,
|
164
|
-
usePlugin,
|
165
|
-
init,
|
166
|
-
run,
|
167
|
-
clear,
|
168
|
-
clone,
|
169
|
-
registe,
|
170
|
-
useRunner,
|
171
|
-
};
|
172
|
-
|
173
|
-
return manager;
|
174
|
-
};
|
175
|
-
|
176
|
-
return clone();
|
177
|
-
};
|
178
|
-
|
179
|
-
const includeAsyncPlugin = <O>(
|
180
|
-
plugins: AsyncPlugins<O>,
|
181
|
-
input: AsyncPlugin<O>,
|
182
|
-
): boolean => {
|
183
|
-
for (const plugin of plugins) {
|
184
|
-
if (plugin.name === input.name) {
|
185
|
-
return true;
|
186
|
-
}
|
187
|
-
}
|
188
|
-
|
189
|
-
return false;
|
190
|
-
};
|
191
|
-
|
192
|
-
const sortAsyncPlugins = <O>(
|
193
|
-
input: AsyncIndexPlugins<O>,
|
194
|
-
): AsyncIndexPlugins<O> => {
|
195
|
-
let plugins = input.slice();
|
196
|
-
|
197
|
-
for (let i = 0; i < plugins.length; i++) {
|
198
|
-
const plugin = plugins[i];
|
199
|
-
|
200
|
-
for (const pre of plugin.pre) {
|
201
|
-
for (let j = i + 1; j < plugins.length; j++) {
|
202
|
-
if (plugins[j].name === pre) {
|
203
|
-
plugins = [
|
204
|
-
...plugins.slice(0, i),
|
205
|
-
plugins[j],
|
206
|
-
...plugins.slice(i, j),
|
207
|
-
...plugins.slice(j + 1, plugins.length),
|
208
|
-
];
|
209
|
-
}
|
210
|
-
}
|
211
|
-
}
|
212
|
-
|
213
|
-
for (const post of plugin.post) {
|
214
|
-
for (let j = 0; j < i; j++) {
|
215
|
-
if (plugins[j].name === post) {
|
216
|
-
plugins = [
|
217
|
-
...plugins.slice(0, j),
|
218
|
-
...plugins.slice(j + 1, i + 1),
|
219
|
-
plugins[j],
|
220
|
-
...plugins.slice(i + 1, plugins.length),
|
221
|
-
];
|
222
|
-
}
|
223
|
-
}
|
224
|
-
}
|
225
|
-
}
|
226
|
-
|
227
|
-
return plugins;
|
228
|
-
};
|
229
|
-
|
230
|
-
const checkAsyncPlugins = <O>(plugins: AsyncIndexPlugins<O>) => {
|
231
|
-
for (const origin of plugins) {
|
232
|
-
for (const rival of origin.rivals) {
|
233
|
-
for (const plugin of plugins) {
|
234
|
-
if (rival === plugin.name) {
|
235
|
-
throw new Error(`${origin.name} has rival ${plugin.name}`);
|
236
|
-
}
|
237
|
-
}
|
238
|
-
}
|
239
|
-
|
240
|
-
for (const required of origin.required) {
|
241
|
-
if (!plugins.some(plugin => plugin.name === required)) {
|
242
|
-
throw new Error(
|
243
|
-
`The plugin: ${required} is required when plugin: ${origin.name} is exist.`,
|
244
|
-
);
|
245
|
-
}
|
246
|
-
}
|
247
|
-
}
|
248
|
-
};
|
package/src/manager/index.ts
DELETED
package/src/manager/runner.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import { createContext } from 'farrow-pipeline';
|
2
|
-
|
3
|
-
export const RunnerContext = createContext<any>(null);
|
4
|
-
|
5
|
-
export const useRunner = () => {
|
6
|
-
const runner = RunnerContext.use().value;
|
7
|
-
|
8
|
-
if (!runner) {
|
9
|
-
throw new Error(
|
10
|
-
`Can't call useRunner out of scope, it should be placed in hooks of plugin`,
|
11
|
-
);
|
12
|
-
}
|
13
|
-
|
14
|
-
return runner;
|
15
|
-
};
|