@modern-js/plugin 1.3.1 → 1.3.4
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 +23 -0
- package/dist/js/modern/farrow-pipeline/asyncHooks.node.js +74 -0
- package/dist/js/modern/farrow-pipeline/asyncHooksInterface.js +12 -0
- package/dist/js/modern/farrow-pipeline/context.js +99 -0
- package/dist/js/modern/farrow-pipeline/counter.js +18 -0
- package/dist/js/modern/farrow-pipeline/hook.js +42 -0
- package/dist/js/modern/farrow-pipeline/index.js +19 -0
- package/dist/js/modern/farrow-pipeline/pipeline.js +93 -0
- package/dist/js/modern/index.js +1 -1
- package/dist/js/modern/manager/async.js +4 -4
- package/dist/js/modern/manager/runner.js +1 -1
- package/dist/js/modern/manager/sync.js +5 -6
- package/dist/js/modern/waterfall/async.js +4 -8
- package/dist/js/modern/waterfall/sync.js +4 -8
- package/dist/js/modern/workflow/async.js +1 -4
- package/dist/js/modern/workflow/parallel.js +2 -3
- package/dist/js/modern/workflow/sync.js +1 -1
- package/dist/js/node/farrow-pipeline/asyncHooks.node.js +93 -0
- package/dist/js/node/farrow-pipeline/asyncHooksInterface.js +26 -0
- package/dist/js/node/farrow-pipeline/context.js +120 -0
- package/dist/js/node/farrow-pipeline/counter.js +27 -0
- package/dist/js/node/farrow-pipeline/hook.js +52 -0
- package/dist/js/node/farrow-pipeline/index.js +18 -0
- package/dist/js/node/farrow-pipeline/pipeline.js +129 -0
- package/dist/js/node/index.js +1 -1
- package/dist/js/node/manager/async.js +4 -4
- package/dist/js/node/manager/runner.js +1 -1
- package/dist/js/node/manager/sync.js +5 -6
- package/dist/js/node/waterfall/async.js +4 -8
- package/dist/js/node/waterfall/sync.js +4 -8
- package/dist/js/node/workflow/async.js +1 -4
- package/dist/js/node/workflow/parallel.js +2 -3
- package/dist/js/node/workflow/sync.js +1 -1
- package/dist/js/treeshaking/farrow-pipeline/asyncHooks.node.js +74 -0
- package/dist/js/treeshaking/farrow-pipeline/asyncHooksInterface.js +12 -0
- package/dist/js/treeshaking/farrow-pipeline/context.js +103 -0
- package/dist/js/treeshaking/farrow-pipeline/counter.js +23 -0
- package/dist/js/treeshaking/farrow-pipeline/hook.js +46 -0
- package/dist/js/treeshaking/farrow-pipeline/index.js +19 -0
- package/dist/js/treeshaking/farrow-pipeline/pipeline.js +100 -0
- package/dist/js/treeshaking/index.js +1 -1
- package/dist/js/treeshaking/manager/async.js +8 -29
- package/dist/js/treeshaking/manager/runner.js +1 -1
- package/dist/js/treeshaking/manager/sync.js +9 -28
- package/dist/js/treeshaking/waterfall/async.js +12 -36
- package/dist/js/treeshaking/waterfall/sync.js +11 -31
- package/dist/js/treeshaking/workflow/async.js +6 -28
- package/dist/js/treeshaking/workflow/parallel.js +6 -26
- package/dist/js/treeshaking/workflow/sync.js +5 -24
- package/dist/types/farrow-pipeline/asyncHooks.node.d.ts +2 -0
- package/dist/types/farrow-pipeline/asyncHooksInterface.d.ts +19 -0
- package/dist/types/farrow-pipeline/context.d.ts +28 -0
- package/dist/types/farrow-pipeline/counter.d.ts +11 -0
- package/dist/types/farrow-pipeline/hook.d.ts +9 -0
- package/dist/types/farrow-pipeline/index.d.ts +19 -0
- package/dist/types/farrow-pipeline/pipeline.d.ts +38 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/manager/runner.d.ts +1 -1
- package/dist/types/manager/sync.d.ts +1 -1
- package/dist/types/manager/types.d.ts +1 -1
- package/dist/types/waterfall/async.d.ts +1 -1
- package/dist/types/waterfall/sync.d.ts +1 -1
- package/dist/types/workflow/async.d.ts +1 -1
- package/dist/types/workflow/sync.d.ts +1 -1
- package/package.json +37 -10
- package/jest.config.js +0 -8
- package/modern.config.js +0 -2
- package/node.d.ts +0 -1
- package/node.js +0 -1
- package/tests/.eslintrc.js +0 -6
- package/tests/async.test.ts +0 -715
- package/tests/fixtures/async/base/bar.ts +0 -22
- package/tests/fixtures/async/base/foo.ts +0 -20
- package/tests/fixtures/async/base/fooManager.ts +0 -47
- package/tests/fixtures/async/core/index.ts +0 -174
- package/tests/fixtures/async/dynamic/bar.ts +0 -18
- package/tests/fixtures/async/dynamic/foo.ts +0 -27
- package/tests/fixtures/sync/base/bar.ts +0 -21
- package/tests/fixtures/sync/base/foo.ts +0 -20
- package/tests/fixtures/sync/base/fooManager.ts +0 -47
- package/tests/fixtures/sync/core/index.ts +0 -171
- package/tests/fixtures/sync/dynamic/bar.ts +0 -22
- package/tests/fixtures/sync/dynamic/foo.ts +0 -27
- package/tests/helpers.ts +0 -4
- package/tests/pipeline.test.ts +0 -607
- package/tests/sync.test.ts +0 -679
- package/tests/tsconfig.json +0 -13
- package/tests/waterfall.test.ts +0 -172
- package/tests/workflow.test.ts +0 -111
- package/tsconfig.json +0 -14
package/tests/async.test.ts
DELETED
@@ -1,715 +0,0 @@
|
|
1
|
-
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
2
|
-
/* eslint-disable max-lines */
|
3
|
-
import { enable, disable } from 'farrow-pipeline/asyncHooks.node';
|
4
|
-
import {
|
5
|
-
createPipeline,
|
6
|
-
createAsyncPipeline,
|
7
|
-
createContext,
|
8
|
-
createContainer,
|
9
|
-
} from 'farrow-pipeline';
|
10
|
-
import type { PluginOptions, AsyncSetup } from '../src';
|
11
|
-
import { createManager, createAsyncManager, useRunner } from '../src/manager';
|
12
|
-
import { createWaterfall, createAsyncWaterfall } from '../src/waterfall';
|
13
|
-
import {
|
14
|
-
createWorkflow,
|
15
|
-
createAsyncWorkflow,
|
16
|
-
createParallelWorkflow,
|
17
|
-
} from '../src/workflow';
|
18
|
-
import { main, TestAsyncHooks, TestAsyncPlugin } from './fixtures/async/core';
|
19
|
-
import foo from './fixtures/async/base/foo';
|
20
|
-
import bar, { getBar } from './fixtures/async/base/bar';
|
21
|
-
import dFoo from './fixtures/async/dynamic/foo';
|
22
|
-
import dBar, { getNumber } from './fixtures/async/dynamic/bar';
|
23
|
-
import { sleep } from './helpers';
|
24
|
-
|
25
|
-
describe('async manager', () => {
|
26
|
-
it('base usage', async () => {
|
27
|
-
const manager = main.clone().usePlugin(foo).usePlugin(bar);
|
28
|
-
|
29
|
-
expect(getBar()).toBe(0);
|
30
|
-
|
31
|
-
const runner = await manager.init({});
|
32
|
-
|
33
|
-
expect(getBar()).toBe(1);
|
34
|
-
|
35
|
-
runner.preDev();
|
36
|
-
|
37
|
-
expect(getBar()).toBe(2);
|
38
|
-
|
39
|
-
runner.postDev();
|
40
|
-
|
41
|
-
expect(getBar()).toBe(3);
|
42
|
-
});
|
43
|
-
|
44
|
-
it('should support async setup function', async () => {
|
45
|
-
const manager = createAsyncManager();
|
46
|
-
|
47
|
-
const countContext = createContext(0);
|
48
|
-
const useCount = () => countContext.use().value;
|
49
|
-
|
50
|
-
const plugin = manager.createPlugin(async () => {
|
51
|
-
await sleep(0);
|
52
|
-
expect(useCount()).toBe(1);
|
53
|
-
});
|
54
|
-
manager.usePlugin(plugin);
|
55
|
-
|
56
|
-
manager.run(() => {
|
57
|
-
countContext.set(1);
|
58
|
-
});
|
59
|
-
|
60
|
-
enable();
|
61
|
-
await manager.init();
|
62
|
-
disable();
|
63
|
-
|
64
|
-
const result0 = manager.run(() => countContext.get());
|
65
|
-
|
66
|
-
expect(result0).toBe(1);
|
67
|
-
});
|
68
|
-
|
69
|
-
it('support support dynamicly register', async () => {
|
70
|
-
const manager = main.clone().usePlugin(dFoo).usePlugin(dBar);
|
71
|
-
|
72
|
-
expect(getNumber()).toBe(0);
|
73
|
-
|
74
|
-
const runner = await manager.init();
|
75
|
-
|
76
|
-
expect(getNumber()).toBe(1);
|
77
|
-
|
78
|
-
runner.preDev();
|
79
|
-
|
80
|
-
expect(getNumber()).toBe(2);
|
81
|
-
});
|
82
|
-
|
83
|
-
it('could without progress hook in plugin', async () => {
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
85
|
-
const foo = createWaterfall<number>();
|
86
|
-
const manager = createAsyncManager({ foo });
|
87
|
-
|
88
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
89
|
-
const plugin = manager.createPlugin(() => {});
|
90
|
-
manager.usePlugin(plugin);
|
91
|
-
|
92
|
-
const runner = await manager.init();
|
93
|
-
|
94
|
-
expect(runner.foo(0)).toBe(0);
|
95
|
-
});
|
96
|
-
|
97
|
-
describe('pre order plugin', () => {
|
98
|
-
it('default order is right order', async () => {
|
99
|
-
const manager = createAsyncManager();
|
100
|
-
|
101
|
-
const list: number[] = [];
|
102
|
-
const plugin0 = manager.createPlugin(
|
103
|
-
() => {
|
104
|
-
list.push(0);
|
105
|
-
},
|
106
|
-
{ name: 'plugin0' },
|
107
|
-
);
|
108
|
-
const plugin1 = manager.createPlugin(
|
109
|
-
() => {
|
110
|
-
list.push(1);
|
111
|
-
},
|
112
|
-
{
|
113
|
-
name: 'plugin1',
|
114
|
-
pre: ['plugin0'],
|
115
|
-
},
|
116
|
-
);
|
117
|
-
const plugin2 = manager.createPlugin(
|
118
|
-
() => {
|
119
|
-
list.push(2);
|
120
|
-
},
|
121
|
-
{ name: 'plugin2' },
|
122
|
-
);
|
123
|
-
|
124
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
125
|
-
|
126
|
-
await manager.init();
|
127
|
-
|
128
|
-
expect(list).toStrictEqual([0, 1, 2]);
|
129
|
-
});
|
130
|
-
|
131
|
-
it('default order is incorrect order', async () => {
|
132
|
-
const manager = createAsyncManager();
|
133
|
-
|
134
|
-
const list: number[] = [];
|
135
|
-
const plugin0 = manager.createPlugin(
|
136
|
-
() => {
|
137
|
-
list.push(0);
|
138
|
-
},
|
139
|
-
{
|
140
|
-
name: 'plugin0',
|
141
|
-
pre: ['plugin1'],
|
142
|
-
},
|
143
|
-
);
|
144
|
-
const plugin1 = manager.createPlugin(
|
145
|
-
() => {
|
146
|
-
list.push(1);
|
147
|
-
},
|
148
|
-
{ name: 'plugin1' },
|
149
|
-
);
|
150
|
-
const plugin2 = manager.createPlugin(
|
151
|
-
() => {
|
152
|
-
list.push(2);
|
153
|
-
},
|
154
|
-
{ name: 'plugin2' },
|
155
|
-
);
|
156
|
-
|
157
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
158
|
-
|
159
|
-
await manager.init();
|
160
|
-
|
161
|
-
expect(list).toStrictEqual([1, 0, 2]);
|
162
|
-
});
|
163
|
-
});
|
164
|
-
|
165
|
-
describe('post order plugin', () => {
|
166
|
-
it('default order is right order', async () => {
|
167
|
-
const manager = createAsyncManager();
|
168
|
-
|
169
|
-
const list: number[] = [];
|
170
|
-
const plugin0 = manager.createPlugin(
|
171
|
-
() => {
|
172
|
-
list.push(0);
|
173
|
-
},
|
174
|
-
{
|
175
|
-
name: 'plugin0',
|
176
|
-
post: ['plugin1'],
|
177
|
-
},
|
178
|
-
);
|
179
|
-
const plugin1 = manager.createPlugin(
|
180
|
-
() => {
|
181
|
-
list.push(1);
|
182
|
-
},
|
183
|
-
{ name: 'plugin1' },
|
184
|
-
);
|
185
|
-
const plugin2 = manager.createPlugin(
|
186
|
-
() => {
|
187
|
-
list.push(2);
|
188
|
-
},
|
189
|
-
{ name: 'plugin2' },
|
190
|
-
);
|
191
|
-
|
192
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
193
|
-
|
194
|
-
await manager.init();
|
195
|
-
|
196
|
-
expect(list).toStrictEqual([0, 1, 2]);
|
197
|
-
});
|
198
|
-
|
199
|
-
it('default order is incorrect order', async () => {
|
200
|
-
const manager = createAsyncManager();
|
201
|
-
|
202
|
-
const list: number[] = [];
|
203
|
-
const plugin0 = manager.createPlugin(
|
204
|
-
() => {
|
205
|
-
list.push(0);
|
206
|
-
},
|
207
|
-
{ name: 'plugin0' },
|
208
|
-
);
|
209
|
-
const plugin1 = manager.createPlugin(
|
210
|
-
() => {
|
211
|
-
list.push(1);
|
212
|
-
},
|
213
|
-
{
|
214
|
-
name: 'plugin1',
|
215
|
-
post: ['plugin0'],
|
216
|
-
},
|
217
|
-
);
|
218
|
-
const plugin2 = manager.createPlugin(
|
219
|
-
() => {
|
220
|
-
list.push(2);
|
221
|
-
},
|
222
|
-
{ name: 'plugin2' },
|
223
|
-
);
|
224
|
-
|
225
|
-
manager.usePlugin(plugin0, plugin1, plugin2);
|
226
|
-
|
227
|
-
await manager.init();
|
228
|
-
|
229
|
-
expect(list).toStrictEqual([1, 0, 2]);
|
230
|
-
});
|
231
|
-
|
232
|
-
it('should support more plugin', async () => {
|
233
|
-
const manager = createAsyncManager();
|
234
|
-
|
235
|
-
let status = 0;
|
236
|
-
const plugin1 = manager.createPlugin(
|
237
|
-
() => {
|
238
|
-
status = 1;
|
239
|
-
},
|
240
|
-
{ name: 'plugin1' },
|
241
|
-
);
|
242
|
-
|
243
|
-
const plugin2 = manager.createPlugin(() => {
|
244
|
-
status = 2;
|
245
|
-
});
|
246
|
-
|
247
|
-
const plugin3 = manager.createPlugin(() => {
|
248
|
-
status = 3;
|
249
|
-
});
|
250
|
-
|
251
|
-
const plugin4 = manager.createPlugin(
|
252
|
-
() => {
|
253
|
-
status = 4;
|
254
|
-
},
|
255
|
-
{ post: ['plugin1'] },
|
256
|
-
);
|
257
|
-
|
258
|
-
manager.usePlugin(plugin1);
|
259
|
-
manager.usePlugin(plugin2);
|
260
|
-
manager.usePlugin(plugin3);
|
261
|
-
manager.usePlugin(plugin4);
|
262
|
-
|
263
|
-
await manager.init({});
|
264
|
-
|
265
|
-
expect(status).toBe(1);
|
266
|
-
});
|
267
|
-
});
|
268
|
-
|
269
|
-
describe('rival plugin', () => {
|
270
|
-
it('should throw error when attaching rival plugin', async () => {
|
271
|
-
const manager = createAsyncManager();
|
272
|
-
|
273
|
-
let count = 0;
|
274
|
-
const plugin0 = manager.createPlugin(
|
275
|
-
() => {
|
276
|
-
count = 0;
|
277
|
-
},
|
278
|
-
{ name: 'plugin0' },
|
279
|
-
);
|
280
|
-
const plugin1 = manager.createPlugin(
|
281
|
-
() => {
|
282
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
283
|
-
count += 1;
|
284
|
-
},
|
285
|
-
{
|
286
|
-
name: 'plugin1',
|
287
|
-
rivals: ['plugin0'],
|
288
|
-
},
|
289
|
-
);
|
290
|
-
|
291
|
-
manager.usePlugin(plugin0, plugin1);
|
292
|
-
|
293
|
-
await expect(manager.init).rejects.toThrowError();
|
294
|
-
});
|
295
|
-
|
296
|
-
it('should not throw error without attaching rival plugin', async () => {
|
297
|
-
const manager = createAsyncManager();
|
298
|
-
|
299
|
-
let count = 0;
|
300
|
-
const plugin0 = manager.createPlugin(
|
301
|
-
() => {
|
302
|
-
count = 0;
|
303
|
-
},
|
304
|
-
{ name: 'plugin0' },
|
305
|
-
);
|
306
|
-
const plugin1 = manager.createPlugin(
|
307
|
-
() => {
|
308
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
309
|
-
count += 1;
|
310
|
-
},
|
311
|
-
{
|
312
|
-
name: 'plugin1',
|
313
|
-
rivals: ['plugin2'],
|
314
|
-
},
|
315
|
-
);
|
316
|
-
|
317
|
-
manager.usePlugin(plugin0, plugin1);
|
318
|
-
|
319
|
-
await manager.init();
|
320
|
-
});
|
321
|
-
});
|
322
|
-
|
323
|
-
describe('required plugin', () => {
|
324
|
-
it('should throw error when it is without required plugin', async () => {
|
325
|
-
const manager = createAsyncManager();
|
326
|
-
|
327
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
328
|
-
const plugin0 = manager.createPlugin(() => {}, {
|
329
|
-
name: 'plugin0',
|
330
|
-
required: ['plugin1'],
|
331
|
-
});
|
332
|
-
|
333
|
-
manager.usePlugin(plugin0);
|
334
|
-
|
335
|
-
await expect(manager.init).rejects.toThrowError();
|
336
|
-
});
|
337
|
-
|
338
|
-
it('should not throw error without attaching rival plugin', async () => {
|
339
|
-
const manager = createAsyncManager();
|
340
|
-
|
341
|
-
let count = 0;
|
342
|
-
const plugin0 = manager.createPlugin(
|
343
|
-
() => {
|
344
|
-
count = 0;
|
345
|
-
},
|
346
|
-
{
|
347
|
-
name: 'plugin0',
|
348
|
-
required: ['plugin1'],
|
349
|
-
},
|
350
|
-
);
|
351
|
-
const plugin1 = manager.createPlugin(
|
352
|
-
() => {
|
353
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
354
|
-
count += 1;
|
355
|
-
},
|
356
|
-
{ name: 'plugin1' },
|
357
|
-
);
|
358
|
-
|
359
|
-
manager.usePlugin(plugin0, plugin1);
|
360
|
-
|
361
|
-
await manager.init();
|
362
|
-
});
|
363
|
-
});
|
364
|
-
|
365
|
-
it('should de-duplicate plugins', async () => {
|
366
|
-
const manager = createAsyncManager();
|
367
|
-
|
368
|
-
let count = 0;
|
369
|
-
const plugin = manager.createPlugin(() => {
|
370
|
-
count += 1;
|
371
|
-
});
|
372
|
-
|
373
|
-
manager.usePlugin(plugin, plugin);
|
374
|
-
manager.usePlugin(plugin);
|
375
|
-
|
376
|
-
await manager.init();
|
377
|
-
|
378
|
-
expect(count).toBe(1);
|
379
|
-
});
|
380
|
-
|
381
|
-
it('should support manager clone', async () => {
|
382
|
-
const manager0 = createAsyncManager();
|
383
|
-
|
384
|
-
let count = 0;
|
385
|
-
const plugin = manager0.createPlugin(() => {
|
386
|
-
count += 1;
|
387
|
-
});
|
388
|
-
|
389
|
-
manager0.usePlugin(plugin);
|
390
|
-
await manager0.init();
|
391
|
-
|
392
|
-
expect(count).toBe(1);
|
393
|
-
|
394
|
-
const manager1 = manager0.clone();
|
395
|
-
manager1.usePlugin(plugin);
|
396
|
-
await manager1.init();
|
397
|
-
|
398
|
-
expect(count).toBe(2);
|
399
|
-
});
|
400
|
-
|
401
|
-
it('should support manager clone and override pluginAPI', done => {
|
402
|
-
const myAPI = { hello: () => 1 };
|
403
|
-
const manager = createAsyncManager({}, myAPI);
|
404
|
-
const plugin = {
|
405
|
-
setup(api: typeof myAPI) {
|
406
|
-
expect(api.hello()).toEqual(2);
|
407
|
-
done();
|
408
|
-
},
|
409
|
-
};
|
410
|
-
const clonedManager = manager.clone({
|
411
|
-
hello: () => 2,
|
412
|
-
});
|
413
|
-
|
414
|
-
clonedManager.usePlugin(plugin).init();
|
415
|
-
});
|
416
|
-
|
417
|
-
it('isPlugin if exclusive plugins of manager', () => {
|
418
|
-
const manager0 = createManager();
|
419
|
-
const manager1 = createAsyncManager();
|
420
|
-
|
421
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
422
|
-
const plugin = manager0.createPlugin(() => {});
|
423
|
-
|
424
|
-
expect(manager0.isPlugin(plugin)).toBeTruthy();
|
425
|
-
expect(manager1.isPlugin(plugin)).toBeFalsy();
|
426
|
-
expect(manager1.isPlugin({})).toBeFalsy();
|
427
|
-
expect(manager1.isPlugin('' as any)).toBeFalsy();
|
428
|
-
});
|
429
|
-
|
430
|
-
it('should support clear plugins', async () => {
|
431
|
-
const manager = createAsyncManager();
|
432
|
-
|
433
|
-
let count = 0;
|
434
|
-
const plugin = manager.createPlugin(() => {
|
435
|
-
count += 1;
|
436
|
-
});
|
437
|
-
|
438
|
-
manager.usePlugin(plugin);
|
439
|
-
manager.clear();
|
440
|
-
|
441
|
-
await manager.init();
|
442
|
-
|
443
|
-
expect(count).toBe(0);
|
444
|
-
});
|
445
|
-
|
446
|
-
it('should run under the internal container', async () => {
|
447
|
-
const manager = createAsyncManager();
|
448
|
-
const Count = createContext(0);
|
449
|
-
|
450
|
-
const plugin = manager.createPlugin(() => {
|
451
|
-
Count.set(1);
|
452
|
-
});
|
453
|
-
|
454
|
-
manager.usePlugin(plugin);
|
455
|
-
|
456
|
-
await manager.init();
|
457
|
-
expect(manager.run(Count.get)).toBe(1);
|
458
|
-
|
459
|
-
const container = createContainer();
|
460
|
-
|
461
|
-
await manager.init({ container });
|
462
|
-
expect(manager.run(Count.get, { container })).toBe(1);
|
463
|
-
});
|
464
|
-
|
465
|
-
it('should support all progress', async () => {
|
466
|
-
const manager = createAsyncManager({
|
467
|
-
pipeline: createPipeline(),
|
468
|
-
asyncPipeline: createAsyncPipeline(),
|
469
|
-
waterfall: createWaterfall(),
|
470
|
-
asyncWaterfall: createAsyncWaterfall(),
|
471
|
-
workflow: createWorkflow(),
|
472
|
-
asyncWorkflow: createAsyncWorkflow(),
|
473
|
-
parallelWorkflow: createParallelWorkflow(),
|
474
|
-
});
|
475
|
-
|
476
|
-
const list: string[] = [];
|
477
|
-
const plugin = manager.createPlugin(() => ({
|
478
|
-
pipeline: () => {
|
479
|
-
list.push('pipeline');
|
480
|
-
},
|
481
|
-
asyncPipeline: () => {
|
482
|
-
list.push('asyncPipeline');
|
483
|
-
},
|
484
|
-
waterfall: () => {
|
485
|
-
list.push('waterfall');
|
486
|
-
},
|
487
|
-
asyncWaterfall: () => {
|
488
|
-
list.push('asyncWaterfall');
|
489
|
-
},
|
490
|
-
workflow: () => {
|
491
|
-
list.push('workflow');
|
492
|
-
},
|
493
|
-
asyncWorkflow: () => {
|
494
|
-
list.push('asyncWorkflow');
|
495
|
-
},
|
496
|
-
parallelWorkflow: () => {
|
497
|
-
list.push('parallelWorkflow');
|
498
|
-
},
|
499
|
-
}));
|
500
|
-
manager.usePlugin(plugin);
|
501
|
-
|
502
|
-
const runner = await manager.init();
|
503
|
-
|
504
|
-
runner.pipeline({});
|
505
|
-
await runner.asyncPipeline({});
|
506
|
-
runner.waterfall();
|
507
|
-
await runner.asyncWaterfall();
|
508
|
-
runner.workflow();
|
509
|
-
await runner.asyncWorkflow();
|
510
|
-
await runner.parallelWorkflow();
|
511
|
-
|
512
|
-
expect(list).toStrictEqual([
|
513
|
-
'pipeline',
|
514
|
-
'asyncPipeline',
|
515
|
-
'waterfall',
|
516
|
-
'asyncWaterfall',
|
517
|
-
'workflow',
|
518
|
-
'asyncWorkflow',
|
519
|
-
'parallelWorkflow',
|
520
|
-
]);
|
521
|
-
});
|
522
|
-
|
523
|
-
describe('useRunner', () => {
|
524
|
-
it('should work well', async () => {
|
525
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
526
|
-
const foo = createAsyncPipeline();
|
527
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
528
|
-
const bar = createAsyncPipeline();
|
529
|
-
const manager = createAsyncManager({ foo, bar });
|
530
|
-
|
531
|
-
let count = 0;
|
532
|
-
|
533
|
-
const plugin = manager.createPlugin(() => ({
|
534
|
-
foo: async () => {
|
535
|
-
await sleep(0);
|
536
|
-
const runner = manager.useRunner();
|
537
|
-
runner.bar({});
|
538
|
-
},
|
539
|
-
bar: () => {
|
540
|
-
count = 1;
|
541
|
-
},
|
542
|
-
}));
|
543
|
-
|
544
|
-
manager.usePlugin(plugin);
|
545
|
-
|
546
|
-
const runner = await manager.init();
|
547
|
-
|
548
|
-
enable();
|
549
|
-
await runner.foo({});
|
550
|
-
disable();
|
551
|
-
|
552
|
-
expect(count).toBe(1);
|
553
|
-
});
|
554
|
-
|
555
|
-
it('should throw error useRunner out plugin hook', async () => {
|
556
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
557
|
-
const foo = createAsyncPipeline();
|
558
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
559
|
-
const bar = createAsyncPipeline();
|
560
|
-
const manager = createAsyncManager({ foo, bar });
|
561
|
-
|
562
|
-
let count = 0;
|
563
|
-
|
564
|
-
const plugin = manager.createPlugin(() => {
|
565
|
-
const runner = manager.useRunner();
|
566
|
-
|
567
|
-
return {
|
568
|
-
foo: async () => {
|
569
|
-
await sleep(0);
|
570
|
-
runner.bar({});
|
571
|
-
},
|
572
|
-
bar: () => {
|
573
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
574
|
-
count = 1;
|
575
|
-
},
|
576
|
-
};
|
577
|
-
});
|
578
|
-
|
579
|
-
manager.usePlugin(plugin);
|
580
|
-
|
581
|
-
await expect(manager.init).rejects.toThrowError(
|
582
|
-
new Error(
|
583
|
-
`Can't call useRunner out of scope, it should be placed in hooks of plugin`,
|
584
|
-
),
|
585
|
-
);
|
586
|
-
});
|
587
|
-
|
588
|
-
it('should throw error useRunner out plugin', () => {
|
589
|
-
expect(useRunner).toThrowError(
|
590
|
-
new Error(
|
591
|
-
`Can't call useContainer out of scope, it should be placed on top of the function`,
|
592
|
-
),
|
593
|
-
);
|
594
|
-
});
|
595
|
-
});
|
596
|
-
|
597
|
-
describe('setup api', () => {
|
598
|
-
it('should allow to access api.useHookRunners by default', done => {
|
599
|
-
const manager = createAsyncManager<TestAsyncHooks>();
|
600
|
-
const plugin: TestAsyncPlugin = {
|
601
|
-
name: 'plugin',
|
602
|
-
setup: api => {
|
603
|
-
expect(api.useHookRunners).toBeTruthy();
|
604
|
-
done();
|
605
|
-
},
|
606
|
-
};
|
607
|
-
manager.usePlugin(plugin);
|
608
|
-
manager.init();
|
609
|
-
});
|
610
|
-
|
611
|
-
it('should allow to register extra api', done => {
|
612
|
-
type API = { foo: () => void };
|
613
|
-
|
614
|
-
const manager = createAsyncManager<TestAsyncHooks, API>(
|
615
|
-
{},
|
616
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
617
|
-
{ foo: () => {} },
|
618
|
-
);
|
619
|
-
|
620
|
-
const plugin: PluginOptions<
|
621
|
-
TestAsyncHooks,
|
622
|
-
AsyncSetup<TestAsyncHooks, API>
|
623
|
-
> = {
|
624
|
-
name: 'plugin',
|
625
|
-
setup: api => {
|
626
|
-
expect(api.foo).toBeTruthy();
|
627
|
-
done();
|
628
|
-
},
|
629
|
-
};
|
630
|
-
|
631
|
-
manager.usePlugin(plugin);
|
632
|
-
manager.init();
|
633
|
-
});
|
634
|
-
});
|
635
|
-
|
636
|
-
describe('usePlugins option', () => {
|
637
|
-
it('should allow to use single plugin', async () => {
|
638
|
-
const manager = createAsyncManager<TestAsyncHooks>();
|
639
|
-
|
640
|
-
const list: number[] = [];
|
641
|
-
const plugin0: TestAsyncPlugin = {
|
642
|
-
name: 'plugin0',
|
643
|
-
setup: () => {
|
644
|
-
list.push(0);
|
645
|
-
},
|
646
|
-
};
|
647
|
-
|
648
|
-
const plugin1: TestAsyncPlugin = {
|
649
|
-
name: 'plugin1',
|
650
|
-
usePlugins: [plugin0],
|
651
|
-
setup: () => {
|
652
|
-
list.push(1);
|
653
|
-
},
|
654
|
-
};
|
655
|
-
|
656
|
-
manager.usePlugin(plugin1);
|
657
|
-
|
658
|
-
await manager.init();
|
659
|
-
|
660
|
-
expect(list).toStrictEqual([0, 1]);
|
661
|
-
});
|
662
|
-
|
663
|
-
it('should allow to use multiple plugins', async () => {
|
664
|
-
const manager = createAsyncManager<TestAsyncHooks>();
|
665
|
-
|
666
|
-
const list: number[] = [];
|
667
|
-
const plugin0: TestAsyncPlugin = {
|
668
|
-
name: 'plugin0',
|
669
|
-
setup: () => {
|
670
|
-
list.push(0);
|
671
|
-
},
|
672
|
-
};
|
673
|
-
|
674
|
-
const plugin1 = {
|
675
|
-
name: 'plugin1',
|
676
|
-
usePlugins: [plugin0],
|
677
|
-
setup: () => {
|
678
|
-
list.push(1);
|
679
|
-
},
|
680
|
-
};
|
681
|
-
|
682
|
-
const plugin2 = {
|
683
|
-
name: 'plugin2',
|
684
|
-
usePlugins: [plugin1],
|
685
|
-
setup: () => {
|
686
|
-
list.push(2);
|
687
|
-
},
|
688
|
-
};
|
689
|
-
|
690
|
-
manager.usePlugin(plugin2);
|
691
|
-
|
692
|
-
await manager.init();
|
693
|
-
|
694
|
-
expect(list).toStrictEqual([0, 1, 2]);
|
695
|
-
});
|
696
|
-
|
697
|
-
it('should allow to use function plugin', async () => {
|
698
|
-
const manager = createAsyncManager<TestAsyncHooks>();
|
699
|
-
|
700
|
-
const list: number[] = [];
|
701
|
-
const plugin0: TestAsyncPlugin = {
|
702
|
-
name: 'plugin0',
|
703
|
-
setup: () => {
|
704
|
-
list.push(0);
|
705
|
-
},
|
706
|
-
};
|
707
|
-
|
708
|
-
manager.usePlugin(() => plugin0);
|
709
|
-
|
710
|
-
await manager.init();
|
711
|
-
|
712
|
-
expect(list).toStrictEqual([0]);
|
713
|
-
});
|
714
|
-
});
|
715
|
-
});
|