@modern-js/plugin 1.2.1 → 1.3.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 (36) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/js/modern/manager/async.js +59 -28
  3. package/dist/js/modern/manager/index.js +2 -1
  4. package/dist/js/modern/manager/sync.js +79 -60
  5. package/dist/js/modern/manager/types.js +0 -0
  6. package/dist/js/modern/waterfall/async.js +0 -1
  7. package/dist/js/modern/waterfall/sync.js +0 -1
  8. package/dist/js/modern/workflow/async.js +1 -2
  9. package/dist/js/modern/workflow/sync.js +0 -1
  10. package/dist/js/node/manager/async.js +57 -25
  11. package/dist/js/node/manager/index.js +13 -0
  12. package/dist/js/node/manager/sync.js +84 -58
  13. package/dist/js/node/manager/types.js +0 -0
  14. package/dist/js/node/waterfall/async.js +0 -1
  15. package/dist/js/node/waterfall/sync.js +0 -1
  16. package/dist/js/node/workflow/async.js +1 -2
  17. package/dist/js/node/workflow/sync.js +0 -1
  18. package/dist/js/treeshaking/manager/async.js +63 -34
  19. package/dist/js/treeshaking/manager/index.js +2 -1
  20. package/dist/js/treeshaking/manager/sync.js +85 -62
  21. package/dist/js/treeshaking/manager/types.js +0 -0
  22. package/dist/js/treeshaking/waterfall/async.js +0 -1
  23. package/dist/js/treeshaking/waterfall/sync.js +0 -1
  24. package/dist/js/treeshaking/workflow/async.js +1 -2
  25. package/dist/js/treeshaking/workflow/sync.js +0 -1
  26. package/dist/types/manager/async.d.ts +60 -21
  27. package/dist/types/manager/index.d.ts +2 -1
  28. package/dist/types/manager/sync.d.ts +74 -43
  29. package/dist/types/manager/types.d.ts +41 -0
  30. package/package.json +3 -3
  31. package/tests/async.test.ts +166 -22
  32. package/tests/fixtures/async/core/index.ts +12 -4
  33. package/tests/fixtures/async/dynamic/foo.ts +2 -2
  34. package/tests/fixtures/sync/core/index.ts +9 -4
  35. package/tests/fixtures/sync/dynamic/foo.ts +2 -2
  36. package/tests/sync.test.ts +158 -21
@@ -1,3 +1,5 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+
1
3
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
4
 
3
5
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -18,47 +20,46 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
18
20
 
19
21
  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; }
20
22
 
21
- // eslint-disable-next-line eslint-comments/disable-enable-pair
22
-
23
- /* eslint-disable max-lines */
24
23
  import { isPipeline, createPipeline, runWithContainer, createContainer } from 'farrow-pipeline';
25
24
  import { isWaterfall, createWaterfall, isAsyncWaterfall, createAsyncWaterfall } from "../waterfall";
26
25
  import { isWorkflow, createWorkflow, isAsyncWorkflow, createAsyncWorkflow, isParallelWorkflow, createParallelWorkflow } from "../workflow";
27
- import { RunnerContext, useRunner } from "./runner"; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
28
-
26
+ import { RunnerContext, useRunner } from "./runner";
29
27
  var SYNC_PLUGIN_SYMBOL = 'SYNC_PLUGIN_SYMBOL';
30
28
  export var DEFAULT_OPTIONS = {
31
29
  name: 'untitled',
32
30
  pre: [],
33
31
  post: [],
34
32
  rivals: [],
35
- required: []
33
+ required: [],
34
+ usePlugins: [],
35
+ registerHook: {}
36
36
  };
37
- export var createManager = function createManager(processes) {
37
+ export var createManager = function createManager(hooks, api) {
38
38
  var index = 0;
39
39
 
40
- var createPlugin = function createPlugin(initializer) {
41
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
42
- return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
43
- name: "No.".concat(index++, " plugin")
44
- }, options), {}, {
45
- SYNC_PLUGIN_SYMBOL: SYNC_PLUGIN_SYMBOL,
46
- initializer: initializer
47
- });
40
+ var currentHooks = _objectSpread({}, hooks);
41
+
42
+ var registerHook = function registerHook(extraHooks) {
43
+ currentHooks = _objectSpread(_objectSpread({}, extraHooks), currentHooks);
48
44
  };
49
45
 
50
46
  var isPlugin = function isPlugin(input) {
51
- return hasOwnProperty(input, SYNC_PLUGIN_SYMBOL) && input[SYNC_PLUGIN_SYMBOL] === SYNC_PLUGIN_SYMBOL;
47
+ return isObject(input) && hasOwnProperty(input, SYNC_PLUGIN_SYMBOL) && input[SYNC_PLUGIN_SYMBOL] === SYNC_PLUGIN_SYMBOL;
52
48
  };
53
49
 
54
- var registe = function registe(extraProcesses) {
55
- // eslint-disable-next-line no-param-reassign
56
- processes = _objectSpread(_objectSpread({}, extraProcesses), processes);
57
- };
50
+ var pluginAPI = _objectSpread(_objectSpread({}, api), {}, {
51
+ useHookRunners: useRunner
52
+ });
58
53
 
59
- var clone = function clone() {
54
+ var clone = function clone(overrideAPI) {
60
55
  var plugins = [];
61
56
 
57
+ var addPlugin = function addPlugin(plugin) {
58
+ if (!includePlugin(plugins, plugin)) {
59
+ plugins.push(_objectSpread({}, plugin));
60
+ }
61
+ };
62
+
62
63
  var usePlugin = function usePlugin() {
63
64
  for (var _len = arguments.length, input = new Array(_len), _key = 0; _key < _len; _key++) {
64
65
  input[_key] = arguments[_key];
@@ -67,30 +68,48 @@ export var createManager = function createManager(processes) {
67
68
  for (var _i = 0, _input = input; _i < _input.length; _i++) {
68
69
  var plugin = _input[_i];
69
70
 
71
+ // already created by createPlugin
70
72
  if (isPlugin(plugin)) {
71
- if (!includePlugin(plugins, plugin)) {
72
- plugins.push(_objectSpread(_objectSpread({}, plugin), {}, {
73
- index: plugins.length
74
- }));
75
- }
76
- } else {
77
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
78
- // @ts-expect-error
79
- console.warn("Unknown plugin: ".concat(plugin.name));
73
+ addPlugin(plugin);
74
+ } // using function to return plugin options
75
+ else if (typeof plugin === 'function') {
76
+ var _options = plugin();
77
+
78
+ addPlugin(createPlugin(_options.setup, _options));
79
+ } // plain plugin object
80
+ else if (isObject(plugin)) {
81
+ addPlugin(createPlugin(plugin.setup, plugin));
82
+ } // unknown plugin
83
+ else {
84
+ console.warn("Unknown plugin: ".concat(JSON.stringify(plugin)));
80
85
  }
81
86
  }
82
87
 
83
- return {
84
- createPlugin: createPlugin,
85
- isPlugin: isPlugin,
86
- usePlugin: usePlugin,
87
- init: init,
88
- run: run,
89
- clear: clear,
90
- registe: registe,
91
- useRunner: useRunner,
92
- clone: clone
93
- };
88
+ return manager;
89
+ };
90
+
91
+ var createPlugin = function createPlugin() {
92
+ var _options$usePlugins;
93
+
94
+ var setup = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
95
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
96
+
97
+ if ((_options$usePlugins = options.usePlugins) !== null && _options$usePlugins !== void 0 && _options$usePlugins.length) {
98
+ options.usePlugins.forEach(function (plugin) {
99
+ usePlugin(createPlugin(plugin.setup, plugin));
100
+ });
101
+ }
102
+
103
+ if (options.registerHook) {
104
+ registerHook(options.registerHook);
105
+ }
106
+
107
+ return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
108
+ name: "No.".concat(index++, " plugin")
109
+ }, options), {}, {
110
+ SYNC_PLUGIN_SYMBOL: SYNC_PLUGIN_SYMBOL,
111
+ setup: setup
112
+ });
94
113
  };
95
114
 
96
115
  var clear = function clear() {
@@ -102,13 +121,16 @@ export var createManager = function createManager(processes) {
102
121
  var init = function init(options) {
103
122
  var container = (options === null || options === void 0 ? void 0 : options.container) || currentContainer;
104
123
  var sortedPlugins = sortPlugins(plugins);
124
+
125
+ var mergedPluginAPI = _objectSpread(_objectSpread({}, pluginAPI), overrideAPI);
126
+
105
127
  checkPlugins(sortedPlugins);
106
128
  var hooksList = sortedPlugins.map(function (plugin) {
107
129
  return runWithContainer(function () {
108
- return plugin.initializer();
130
+ return plugin.setup(mergedPluginAPI);
109
131
  }, container);
110
132
  });
111
- return generateRunner(hooksList, container, processes);
133
+ return generateRunner(hooksList, container, currentHooks);
112
134
  };
113
135
 
114
136
  var run = function run(cb, options) {
@@ -116,26 +138,27 @@ export var createManager = function createManager(processes) {
116
138
  return runWithContainer(cb, container);
117
139
  };
118
140
 
119
- return {
141
+ var manager = {
120
142
  createPlugin: createPlugin,
121
143
  isPlugin: isPlugin,
122
144
  usePlugin: usePlugin,
123
145
  init: init,
124
146
  clear: clear,
125
147
  run: run,
126
- registe: registe,
148
+ registerHook: registerHook,
127
149
  useRunner: useRunner,
128
150
  clone: clone
129
151
  };
152
+ return manager;
130
153
  };
131
154
 
132
155
  return clone();
133
156
  };
134
- export var generateRunner = function generateRunner(hooksList, container, processes) {
157
+ export var generateRunner = function generateRunner(hooksList, container, hooksMap) {
135
158
  var runner = {};
136
- var cloneShape = cloneProgressRecord(processes);
159
+ var cloneShape = closeHooksMap(hooksMap);
137
160
 
138
- if (processes) {
161
+ if (hooksMap) {
139
162
  var _loop = function _loop(key) {
140
163
  var _iterator = _createForOfIteratorHelper(hooksList),
141
164
  _step;
@@ -149,8 +172,6 @@ export var generateRunner = function generateRunner(hooksList, container, proces
149
172
  }
150
173
 
151
174
  if (hooks[key]) {
152
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
153
- // @ts-expect-error
154
175
  cloneShape[key].use(hooks[key]);
155
176
  }
156
177
  } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -177,36 +198,35 @@ export var generateRunner = function generateRunner(hooksList, container, proces
177
198
  container.write(RunnerContext, runner);
178
199
  return runner;
179
200
  };
180
- export var cloneProgress = function cloneProgress(progress) {
181
- if (isWaterfall(progress)) {
201
+ export var cloneHook = function cloneHook(hook) {
202
+ if (isWaterfall(hook)) {
182
203
  return createWaterfall();
183
204
  }
184
205
 
185
- if (isAsyncWaterfall(progress)) {
206
+ if (isAsyncWaterfall(hook)) {
186
207
  return createAsyncWaterfall();
187
208
  }
188
209
 
189
- if (isWorkflow(progress)) {
210
+ if (isWorkflow(hook)) {
190
211
  return createWorkflow();
191
212
  }
192
213
 
193
- if (isAsyncWorkflow(progress)) {
214
+ if (isAsyncWorkflow(hook)) {
194
215
  return createAsyncWorkflow();
195
216
  }
196
217
 
197
- if (isParallelWorkflow(progress)) {
218
+ if (isParallelWorkflow(hook)) {
198
219
  return createParallelWorkflow();
199
220
  }
200
221
 
201
- if (isPipeline(progress)) {
222
+ if (isPipeline(hook)) {
202
223
  return createPipeline();
203
224
  } // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
204
225
 
205
226
 
206
- throw new Error("Unknown progress: ".concat(progress));
207
- }; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
208
-
209
- export var cloneProgressRecord = function cloneProgressRecord(record) {
227
+ throw new Error("Unknown hook: ".concat(hook));
228
+ };
229
+ export var closeHooksMap = function closeHooksMap(record) {
210
230
  if (!record) {
211
231
  return record;
212
232
  }
@@ -216,7 +236,7 @@ export var cloneProgressRecord = function cloneProgressRecord(record) {
216
236
  for (var key in record) {
217
237
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
218
238
  // @ts-expect-error
219
- result[key] = cloneProgress(record[key]);
239
+ result[key] = cloneHook(record[key]);
220
240
  }
221
241
 
222
242
  return result;
@@ -359,6 +379,9 @@ var checkPlugins = function checkPlugins(plugins) {
359
379
  }
360
380
  };
361
381
 
382
+ export var isObject = function isObject(obj) {
383
+ return obj !== null && _typeof(obj) === 'object';
384
+ };
362
385
  export var hasOwnProperty = function hasOwnProperty(obj, prop) {
363
386
  return obj.hasOwnProperty(prop);
364
387
  };
File without changes
@@ -34,7 +34,6 @@ export var getAsyncBrook = function getAsyncBrook(input) {
34
34
 
35
35
  throw new Error("".concat(input, " is not a AsyncBrook or { brook: AsyncBrook }"));
36
36
  };
37
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
38
37
  export var createAsyncWaterfall = function createAsyncWaterfall() {
39
38
  var pipeline = createAsyncPipeline();
40
39
 
@@ -28,7 +28,6 @@ export var getBrook = function getBrook(input) {
28
28
 
29
29
  throw new Error("".concat(input, " is not a Brook or { brook: Brook }"));
30
30
  };
31
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
32
31
  export var createWaterfall = function createWaterfall() {
33
32
  var pipeline = createPipeline();
34
33
 
@@ -28,8 +28,7 @@ import { createAsyncPipeline } from 'farrow-pipeline';
28
28
  var ASYNC_WORKFLOW_SYMBOL = Symbol('ASYNC_WORKFLOW_SYMBOL');
29
29
  export var isAsyncWorkflow = function isAsyncWorkflow(input) {
30
30
  return Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_WORKFLOW_SYMBOL]);
31
- }; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
32
-
31
+ };
33
32
  export var createAsyncWorkflow = function createAsyncWorkflow() {
34
33
  var pipeline = createAsyncPipeline();
35
34
 
@@ -24,7 +24,6 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
24
24
 
25
25
  import { createPipeline } from 'farrow-pipeline';
26
26
  var WORKFLOW_SYMBOL = Symbol('WORKFLOW_SYMBOL');
27
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
28
27
  export var createWorkflow = function createWorkflow() {
29
28
  var pipeline = createPipeline();
30
29
 
@@ -1,27 +1,66 @@
1
- import { ProgressRecord, Progresses2Threads, Progresses2Runners, PluginOptions, ClearDraftProgress, InitOptions } from './sync';
2
- export declare type AsyncInitializer<O> = () => void | O | Promise<O | void>;
1
+ import type { ToRunners, ToThreads, CommonAPI, InitOptions, PluginOptions } from './types';
2
+ /** Setup function of async plugin. */
3
+
4
+ export declare type AsyncSetup<Hooks, API = Record<string, never>> = (api: API & CommonAPI<Hooks>) => Partial<ToThreads<Hooks>> | Promise<Partial<ToThreads<Hooks>> | void> | void;
3
5
  declare const ASYNC_PLUGIN_SYMBOL = "ASYNC_PLUGIN_SYMBOL";
4
- export declare type AsyncPlugin<O> = {
5
- initializer: AsyncInitializer<O>;
6
+ export declare type AsyncPlugin<Hooks, API> = {
6
7
  ASYNC_PLUGIN_SYMBOL: typeof ASYNC_PLUGIN_SYMBOL;
7
- } & Required<PluginOptions>;
8
- export declare type IndexAsyncPlugin<O> = AsyncPlugin<O> & {
9
- index: number;
10
- };
11
- export declare type AsyncPlugins<O> = AsyncPlugin<O>[];
12
- export declare type AsyncIndexPlugins<O> = IndexAsyncPlugin<O>[];
13
- export declare type AsyncPluginFromAsyncManager<M extends AsyncManager<any, any>> = M extends AsyncManager<infer EP, infer PR> ? AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>> : never;
14
- export declare type PluginFromAsyncManager<M extends AsyncManager<any, any>> = M extends AsyncManager<infer EP, infer PR> ? AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>> : never;
15
- export declare type AsyncManager<EP extends Record<string, any>, PR extends ProgressRecord | void = void> = {
16
- createPlugin: (initializer: AsyncInitializer<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>, options?: PluginOptions) => AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>;
17
- isPlugin: (input: Record<string, unknown>) => input is AsyncPlugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>;
18
- usePlugin: (...input: AsyncPlugins<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>) => AsyncManager<EP, PR>;
19
- init: (options?: InitOptions) => Promise<Progresses2Runners<PR & ClearDraftProgress<EP>>>;
8
+ } & Required<PluginOptions<Hooks, AsyncSetup<Hooks, API>>>;
9
+ export declare type PluginFromAsyncManager<M extends AsyncManager<any, any>> = M extends AsyncManager<infer Hooks, infer API> ? AsyncPlugin<Hooks, API> : never;
10
+ export declare type AsyncManager<Hooks, API> = {
11
+ /**
12
+ * Create a sync plugin.
13
+ * @param setup the setup function.
14
+ * @param options optional plugin options.
15
+ */
16
+ createPlugin: (setup?: AsyncSetup<Hooks, API>, options?: PluginOptions<Hooks, AsyncSetup<Hooks, API>>) => AsyncPlugin<Hooks, API>;
17
+ /**
18
+ * Determine if a value is a async plugin.
19
+ * @param input
20
+ */
21
+
22
+ isPlugin: (input: unknown) => input is AsyncPlugin<Hooks, API>;
23
+ /**
24
+ * Register new plugins to current manager.
25
+ * @param plugins one or more plugin.
26
+ */
27
+
28
+ usePlugin: (...plugins: Array<AsyncPlugin<Hooks, API> | PluginOptions<Hooks, AsyncSetup<Hooks, API>> | (() => PluginOptions<Hooks, AsyncSetup<Hooks, API>>)>) => AsyncManager<Hooks, API>;
29
+ /**
30
+ * Init manager, it will call the setup function of all registered plugins.
31
+ * @param options passing a custom container.
32
+ */
33
+
34
+ init: (options?: InitOptions) => Promise<ToRunners<Hooks>>;
35
+ /**
36
+ * Run callback function with current container.
37
+ * @param callback
38
+ * @param options passing a custom container.
39
+ */
40
+
20
41
  run: <O>(cb: () => O, options?: InitOptions) => O;
21
- registe: (newShape: Partial<EP>) => void;
22
- clone: () => AsyncManager<EP, PR>;
42
+ /**
43
+ * Register new hooks.
44
+ * @param newHooks
45
+ */
46
+
47
+ registerHook: (newHooks: Partial<Hooks>) => void;
48
+ /**
49
+ * Return a cloned manager.
50
+ * @param overrideAPI override the default plugin API.
51
+ */
52
+
53
+ clone: (overrideAPI?: Partial<API & CommonAPI<Hooks>>) => AsyncManager<Hooks, API>;
54
+ /**
55
+ * Clear all registered plugins.
56
+ */
57
+
23
58
  clear: () => void;
24
- useRunner: () => Progresses2Runners<PR & ClearDraftProgress<EP>>;
59
+ /**
60
+ * Get all runner functions of the hooks.
61
+ */
62
+
63
+ useRunner: () => ToRunners<Hooks>;
25
64
  };
26
- export declare const createAsyncManager: <EP extends Record<string, any> = {}, PR extends void | ProgressRecord = void>(processes?: PR | undefined) => AsyncManager<EP, PR>;
65
+ export declare const createAsyncManager: <Hooks extends Record<string, any>, API extends Record<string, any> = Record<string, never>>(hooks?: Partial<Hooks> | undefined, api?: API | undefined) => AsyncManager<Hooks, API>;
27
66
  export {};
@@ -1,3 +1,4 @@
1
1
  export * from './sync';
2
2
  export * from './async';
3
- export * from './runner';
3
+ export * from './runner';
4
+ export * from './types';
@@ -1,50 +1,81 @@
1
- import { Middleware, Pipeline, AsyncPipeline, MaybeAsync, Container } from 'farrow-pipeline';
2
- import { Waterfall, Brook, AsyncWaterfall, AsyncBrook } from '../waterfall';
3
- import { Worker, Workflow, AsyncWorker, AsyncWorkflow, ParallelWorkflow } from '../workflow';
4
- export declare type Initializer<O> = () => O | void;
1
+ import { Container } from 'farrow-pipeline';
2
+ import type { Hook, CommonAPI, ToRunners, ToThreads, InitOptions, PluginOptions } from './types';
3
+ /** Setup function of sync plugin. */
4
+
5
+ export declare type Setup<Hooks, API = Record<string, never>> = (api: API) => Partial<ToThreads<Hooks>> | void;
5
6
  declare const SYNC_PLUGIN_SYMBOL = "SYNC_PLUGIN_SYMBOL";
6
- export declare type Plugin<O> = {
7
- initializer: Initializer<O>;
7
+ export declare type Plugin<Hooks, API> = {
8
8
  SYNC_PLUGIN_SYMBOL: typeof SYNC_PLUGIN_SYMBOL;
9
- } & Required<PluginOptions>;
10
- export declare type IndexPlugin<O> = Plugin<O> & {
11
- index: number;
12
- };
13
- export declare type Plugins<O> = Plugin<O>[];
14
- export declare type IndexPlugins<O> = IndexPlugin<O>[];
15
- export declare type PluginOptions = {
16
- name?: string;
17
- pre?: string[];
18
- post?: string[];
19
- rivals?: string[];
20
- required?: string[];
21
- };
22
- export declare type Progress = Waterfall<any> | AsyncWaterfall<any> | Workflow<any, any> | AsyncWorkflow<any, any> | ParallelWorkflow<any> | Pipeline<any, any> | AsyncPipeline<any, any>;
23
- export declare type Progress2Thread<P extends Progress> = P extends Workflow<infer I, infer O> ? Worker<I, O> : P extends AsyncWorkflow<infer I, infer O> ? AsyncWorker<I, O> : P extends ParallelWorkflow<infer I, infer O> ? AsyncWorker<I, O> : P extends Waterfall<infer I> ? Brook<I> : P extends AsyncWaterfall<infer I> ? AsyncBrook<I> : P extends Pipeline<infer I, infer O> ? Middleware<I, O> : P extends AsyncPipeline<infer I, infer O> ? Middleware<I, MaybeAsync<O>> : never;
24
- export declare type ProgressRecord = Record<string, Progress>;
25
- export declare type Progresses2Threads<PS extends ProgressRecord | void> = { [K in keyof PS]: PS[K] extends Progress ? Progress2Thread<PS[K]> : PS[K] extends void ? void : never };
26
- export declare type RunnerFromProgress<P extends Progress> = P extends Waterfall<infer I> ? Waterfall<I>['run'] : P extends AsyncWaterfall<infer I> ? AsyncWaterfall<I>['run'] : P extends Workflow<infer I, infer O> ? Workflow<I, O>['run'] : P extends AsyncWorkflow<infer I, infer O> ? AsyncWorkflow<I, O>['run'] : P extends ParallelWorkflow<infer I, infer O> ? ParallelWorkflow<I, O>['run'] : P extends Pipeline<infer I, infer O> ? Pipeline<I, O>['run'] : P extends AsyncPipeline<infer I, infer O> ? AsyncPipeline<I, O>['run'] : never;
27
- export declare type Progresses2Runners<PS extends ProgressRecord | void> = { [K in keyof PS]: PS[K] extends Progress ? RunnerFromProgress<PS[K]> : PS[K] extends void ? void : never };
28
- export declare type ClearDraftProgress<I extends Record<string, any>> = { [K in keyof I]: I[K] extends Progress ? I[K] : never };
29
- export declare type PluginFromManager<M extends Manager<any, any>> = M extends Manager<infer EP, infer PR> ? Plugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>> : never;
30
- export declare type InitOptions = {
31
- container?: Container;
32
- };
33
- export declare type Manager<EP extends Record<string, any>, PR extends ProgressRecord | void = void> = {
34
- createPlugin: (initializer: Initializer<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>, options?: PluginOptions) => Plugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>;
35
- isPlugin: (input: Record<string, unknown>) => input is Plugin<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>;
36
- usePlugin: (...input: Plugins<Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>>) => Manager<EP, PR>;
37
- init: (options?: InitOptions) => Progresses2Runners<PR & ClearDraftProgress<EP>>;
9
+ } & Required<PluginOptions<Hooks, Setup<Hooks, API>>>;
10
+ export declare type PluginFromManager<M extends Manager<any, any>> = M extends Manager<infer Hooks, infer API> ? Plugin<Hooks, API> : never;
11
+ export declare type Manager<Hooks, API> = {
12
+ /**
13
+ * Create a sync plugin.
14
+ * @param setup the setup function.
15
+ * @param options optional plugin options.
16
+ */
17
+ createPlugin: (setup?: Setup<Hooks, API>, options?: PluginOptions<Hooks, Setup<Hooks, API>>) => Plugin<Hooks, API>;
18
+ /**
19
+ * Determine if a value is a sync plugin.
20
+ * @param input
21
+ */
22
+
23
+ isPlugin: (input: unknown) => input is Plugin<Hooks, API>;
24
+ /**
25
+ * Register new plugins to current manager.
26
+ * @param plugins one or more plugin.
27
+ */
28
+
29
+ usePlugin: (...plugins: Array<Plugin<Hooks, API> | PluginOptions<Hooks, Setup<Hooks, API>> | (() => PluginOptions<Hooks, Setup<Hooks, API>>)>) => Manager<Hooks, API>;
30
+ /**
31
+ * Init manager, it will call the setup function of all registered plugins.
32
+ * @param options passing a custom container.
33
+ */
34
+
35
+ init: (options?: InitOptions) => ToRunners<Hooks>;
36
+ /**
37
+ * Run callback function with current container.
38
+ * @param callback
39
+ * @param options passing a custom container.
40
+ */
41
+
38
42
  run: <O>(cb: () => O, options?: InitOptions) => O;
39
- registe: (newShape: Partial<EP>) => void;
43
+ /**
44
+ * Register new hooks.
45
+ * @param newHooks
46
+ */
47
+
48
+ registerHook: (hewHooks: Partial<Hooks>) => void;
49
+ /**
50
+ * Clear all registered plugins.
51
+ */
52
+
40
53
  clear: () => void;
41
- clone: () => Manager<EP, PR>;
42
- useRunner: () => Progresses2Runners<PR & ClearDraftProgress<EP>>;
54
+ /**
55
+ * Return a cloned manager.
56
+ * @param overrideAPI override the default plugin API.
57
+ */
58
+
59
+ clone: (overrideAPI?: Partial<API & CommonAPI<Hooks>>) => Manager<Hooks, API>;
60
+ /**
61
+ * Get all runner functions of the hooks.
62
+ */
63
+
64
+ useRunner: () => ToRunners<Hooks>;
65
+ };
66
+ export declare const DEFAULT_OPTIONS: {
67
+ name: string;
68
+ pre: never[];
69
+ post: never[];
70
+ rivals: never[];
71
+ required: never[];
72
+ usePlugins: never[];
73
+ registerHook: {};
43
74
  };
44
- export declare const DEFAULT_OPTIONS: Required<PluginOptions>;
45
- export declare const createManager: <EP extends Record<string, any> = {}, PR extends void | ProgressRecord = void>(processes?: PR | undefined) => Manager<EP, PR>;
46
- export declare const generateRunner: <EP extends Record<string, any> = {}, PR extends void | ProgressRecord = void>(hooksList: (void | Partial<Progresses2Threads<PR & ClearDraftProgress<EP>>>)[], container: Container, processes?: PR | undefined) => Progresses2Runners<PR & ClearDraftProgress<EP>>;
47
- export declare const cloneProgress: (progress: Progress) => Progress;
48
- export declare const cloneProgressRecord: <PR extends void | ProgressRecord>(record: PR) => PR;
75
+ export declare const createManager: <Hooks, API extends Record<string, any> = Record<string, never>>(hooks?: Partial<Hooks> | undefined, api?: API | undefined) => Manager<Hooks, API>;
76
+ export declare const generateRunner: <Hooks extends Record<string, any>>(hooksList: (void | Partial<ToThreads<Hooks>>)[], container: Container, hooksMap?: Hooks | undefined) => ToRunners<Hooks>;
77
+ export declare const cloneHook: (hook: Hook) => Hook;
78
+ export declare const closeHooksMap: <Hooks>(record: Hooks) => Hooks;
79
+ export declare const isObject: (obj: unknown) => obj is Record<string, any>;
49
80
  export declare const hasOwnProperty: <X extends Record<string, unknown>, Y extends PropertyKey>(obj: X, prop: Y) => obj is X & Record<Y, unknown>;
50
81
  export {};
@@ -0,0 +1,41 @@
1
+ import type { Pipeline, Container, MaybeAsync, Middleware, AsyncPipeline } from 'farrow-pipeline';
2
+ import type { Brook, Waterfall, AsyncBrook, AsyncWaterfall } from '../waterfall';
3
+ import type { Worker, Workflow, AsyncWorker, AsyncWorkflow, ParallelWorkflow } from '../workflow';
4
+ /** All hook types. */
5
+
6
+ export declare type Hook = Waterfall<any> | AsyncWaterfall<any> | Workflow<any, any> | AsyncWorkflow<any, any> | ParallelWorkflow<any> | Pipeline<any, any> | AsyncPipeline<any, any>;
7
+ export declare type HooksMap = Record<string, Hook>;
8
+ /** Extract the type of callback function from a hook. */
9
+
10
+ export declare type ToThread<P extends Hook> = P extends Workflow<infer I, infer O> ? Worker<I, O> : P extends AsyncWorkflow<infer I, infer O> ? AsyncWorker<I, O> : P extends ParallelWorkflow<infer I, infer O> ? AsyncWorker<I, O> : P extends Waterfall<infer I> ? Brook<I> : P extends AsyncWaterfall<infer I> ? AsyncBrook<I> : P extends Pipeline<infer I, infer O> ? Middleware<I, O> : P extends AsyncPipeline<infer I, infer O> ? Middleware<I, MaybeAsync<O>> : never;
11
+ /** Extract types of callback function from hooks. */
12
+
13
+ export declare type ToThreads<PS> = { [K in keyof PS]: PS[K] extends Hook ? ToThread<PS[K]> : PS[K] extends void ? void : never };
14
+ /** Extract run method from a hook. */
15
+
16
+ export declare type RunnerFromHook<P extends Hook> = P extends Waterfall<infer I> ? Waterfall<I>['run'] : P extends AsyncWaterfall<infer I> ? AsyncWaterfall<I>['run'] : P extends Workflow<infer I, infer O> ? Workflow<I, O>['run'] : P extends AsyncWorkflow<infer I, infer O> ? AsyncWorkflow<I, O>['run'] : P extends ParallelWorkflow<infer I, infer O> ? ParallelWorkflow<I, O>['run'] : P extends Pipeline<infer I, infer O> ? Pipeline<I, O>['run'] : P extends AsyncPipeline<infer I, infer O> ? AsyncPipeline<I, O>['run'] : never;
17
+ /** Extract all run methods from hooks. */
18
+
19
+ export declare type ToRunners<PS> = { [K in keyof PS]: PS[K] extends Hook ? RunnerFromHook<PS[K]> : PS[K] extends void ? void : never };
20
+ /** All options to define a plugin. */
21
+
22
+ export declare type PluginOptions<Hooks, Setup = undefined> = {
23
+ name?: string;
24
+ pre?: string[];
25
+ post?: string[];
26
+ setup?: Setup;
27
+ rivals?: string[];
28
+ required?: string[];
29
+ usePlugins?: PluginOptions<Hooks, Setup>[];
30
+ registerHook?: Partial<Hooks>;
31
+ };
32
+ /** Options of manager.init method. */
33
+
34
+ export declare type InitOptions = {
35
+ container?: Container;
36
+ };
37
+ /** Common api of setup function. */
38
+
39
+ export declare type CommonAPI<Hooks> = {
40
+ useHookRunners: () => ToRunners<Hooks>;
41
+ };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.1",
14
+ "version": "1.3.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -46,11 +46,11 @@
46
46
  "sideEffects": false,
47
47
  "publishConfig": {
48
48
  "registry": "https://registry.npmjs.org/",
49
- "access": "public",
50
- "types": "./dist/types/index.d.ts"
49
+ "access": "public"
51
50
  },
52
51
  "scripts": {
53
52
  "new": "modern new",
53
+ "dev": "modern build --watch",
54
54
  "build": "modern build",
55
55
  "test": "jest --passWithNoTests"
56
56
  },