@shuvi/hook 1.0.0-rc.0 → 1.0.0-rc.11

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.
@@ -1,41 +1,39 @@
1
- import { SyncHook, SyncBailHook, SyncWaterfallHook, AsyncParallelHook, AsyncSeriesWaterfallHook, SyncHookHandler, SyncBailHookHandler, SyncWaterfallHookHandler, AsyncParallelHookHandler, AsyncSeriesWaterfallHookHandler } from './hooks';
1
+ import { SyncHook, SyncBailHook, SyncWaterfallHook, AsyncParallelHook, AsyncSeriesWaterfallHook, SyncHookHandler, SyncBailHookHandler, SyncWaterfallHookHandler, AsyncParallelHookHandler, AsyncSeriesWaterfallHookHandler, AsyncSeriesHook, AsyncSeriesBailHook, AsyncSeriesHookHandler, AsyncSeriesBailHookHandler } from './hooks';
2
2
  export declare type PatchPluginParameter<T, C> = RemoveManagerVoidParameter<AddContextParameter<T, C>>;
3
3
  export declare type AddContextParameter<T, C> = T extends (initalValue: infer I, extraArg: infer E) => infer R ? (initalValue: I, extraArg: E, context: C) => R : T;
4
- export declare type AnyHook = SyncHook<any, any, any> | SyncBailHook<any, any, any> | SyncWaterfallHook<any, any> | AsyncParallelHook<any, any, any> | AsyncSeriesWaterfallHook<any, any>;
4
+ export declare type AnyHook = SyncHook<any, any, any> | SyncBailHook<any, any, any> | SyncWaterfallHook<any, any> | AsyncParallelHook<any, any, any> | AsyncSeriesHook<any, any, any> | AsyncSeriesBailHook<any, any, any> | AsyncSeriesWaterfallHook<any, any>;
5
5
  export interface HookMap {
6
6
  [x: string]: AnyHook;
7
7
  }
8
- export declare type Setup<EHM> = (utils: {
9
- addHooks: (hook: Partial<EHM>) => void;
8
+ export declare type Setup<HM extends HookMap = {}> = (utils: {
9
+ addHooks: (hook: Partial<HM>) => void;
10
10
  }) => void;
11
- export declare type CreatePlugin<HM extends HookMap, C, EHM extends HookMap> = (pluginHandlers: IPluginHandlers<HM & EHM, C> & {
12
- setup?: Setup<EHM>;
13
- }, options?: PluginOptions) => IPluginInstance<HM & EHM, C>;
14
- export declare type HookManager<HM extends HookMap, C, EHM extends HookMap> = {
15
- createPlugin: (pluginHandlers: IPluginHandlers<HM & EHM, C> & {
16
- setup?: Setup<EHM>;
17
- }, options?: PluginOptions) => IPluginInstance<HM & EHM, C>;
18
- usePlugin: (...plugins: IPluginInstance<HM & EHM, C>[]) => void;
19
- runner: RunnerType<HM, EHM>;
11
+ export declare type CreatePlugin<HM extends HookMap, C> = (pluginHandlers: IPluginHandlers<HM, C> & {
12
+ setup?: Setup;
13
+ }, options?: PluginOptions) => IPluginInstance<HM, C>;
14
+ export declare type HookManager<HM extends HookMap, C> = {
15
+ createPlugin: CreatePlugin<HM, C>;
16
+ usePlugin: (...plugins: IPluginInstance<HM, C>[]) => void;
17
+ runner: RunnerType<HM>;
20
18
  setContext: (context: C) => void;
21
19
  clear: () => void;
22
- addHooks: (hook: Partial<EHM>) => void;
23
- hooks: HM | (HM & EHM);
24
- getPlugins: () => IPluginInstance<HM & EHM, C>[];
20
+ addHooks: <EHM extends HookMap>(hook: Partial<EHM>) => void;
21
+ hooks: HM;
22
+ getPlugins: () => IPluginInstance<HM, C>[];
25
23
  };
26
- export declare type RunnerType<HM, EHM> = {
27
- [K in keyof (HM & EHM)]: HookRunnerType<(HM & EHM)[K]>;
24
+ export declare type RunnerType<HM> = {
25
+ [K in keyof HM]: HookRunnerType<HM[K]>;
28
26
  } & {
29
- setup: Setup<EHM>;
27
+ setup: Setup;
30
28
  };
31
- export declare type HookRunnerType<H> = H extends SyncHook<infer T, infer E, infer R> ? SyncHook<T, E, R>['run'] : H extends SyncBailHook<infer T, infer E, infer R> ? SyncBailHook<T, E, R>['run'] : H extends SyncWaterfallHook<infer T, infer E> ? SyncWaterfallHook<T, E>['run'] : H extends AsyncParallelHook<infer T, infer E, infer R> ? AsyncParallelHook<T, E, R>['run'] : H extends AsyncSeriesWaterfallHook<infer T, infer E> ? AsyncSeriesWaterfallHook<T, E>['run'] : never;
29
+ export declare type HookRunnerType<H> = H extends SyncHook<infer T, infer E, infer R> ? SyncHook<T, E, R>['run'] : H extends SyncBailHook<infer T, infer E, infer R> ? SyncBailHook<T, E, R>['run'] : H extends SyncWaterfallHook<infer T, infer E> ? SyncWaterfallHook<T, E>['run'] : H extends AsyncParallelHook<infer T, infer E, infer R> ? AsyncParallelHook<T, E, R>['run'] : H extends AsyncSeriesHook<infer T, infer E, infer R> ? AsyncSeriesHook<T, E, R>['run'] : H extends AsyncSeriesBailHook<infer T, infer E, infer R> ? AsyncSeriesBailHook<T, E, R>['run'] : H extends AsyncSeriesWaterfallHook<infer T, infer E> ? AsyncSeriesWaterfallHook<T, E>['run'] : never;
32
30
  export declare type IPluginInstance<HM, C> = {
33
31
  handlers: IPluginHandlers<HM, C>;
34
32
  SYNC_PLUGIN_SYMBOL: 'SYNC_PLUGIN_SYMBOL';
35
- } & PluginOptions;
33
+ } & Required<PluginOptions>;
36
34
  export declare type IPluginHandlers<HM, C> = Partial<IPluginHandlersFullMap<HM, C>>;
37
35
  export declare type IPluginHandlersFullMap<HM, C> = {
38
- [K in keyof HM]: HM[K] extends SyncHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncHookHandler<T, E, R>, C> : HM[K] extends SyncBailHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncBailHookHandler<T, E, R>, C> : HM[K] extends SyncWaterfallHook<infer T, infer E> ? PatchPluginParameter<SyncWaterfallHookHandler<T, E>, C> : HM[K] extends AsyncParallelHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncParallelHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesWaterfallHook<infer T, infer E> ? PatchPluginParameter<AsyncSeriesWaterfallHookHandler<T, E>, C> : never;
36
+ [K in keyof HM]: HM[K] extends SyncHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncHookHandler<T, E, R>, C> : HM[K] extends SyncBailHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncBailHookHandler<T, E, R>, C> : HM[K] extends SyncWaterfallHook<infer T, infer E> ? PatchPluginParameter<SyncWaterfallHookHandler<T, E>, C> : HM[K] extends AsyncParallelHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncParallelHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncSeriesHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesBailHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncSeriesBailHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesWaterfallHook<infer T, infer E> ? PatchPluginParameter<AsyncSeriesWaterfallHookHandler<T, E>, C> : never;
39
37
  };
40
38
  export declare type PluginOptions = {
41
39
  name?: string;
@@ -44,6 +42,7 @@ export declare type PluginOptions = {
44
42
  rivals?: string[];
45
43
  required?: string[];
46
44
  order?: number;
45
+ group?: number;
47
46
  [x: string]: any;
48
47
  };
49
48
  export declare const DEFAULT_OPTIONS: Required<PluginOptions>;
@@ -52,5 +51,5 @@ export declare const isPluginInstance: (plugin: any) => any;
52
51
  export declare type ArrayElements<T extends {}> = {
53
52
  [K in keyof T]: T[K][];
54
53
  };
55
- export declare const createHookManager: <HM extends HookMap, C = void, EHM extends HookMap = {}>(hookMap: HM, hasContext?: boolean) => HookManager<HM, C, EHM>;
54
+ export declare const createHookManager: <HM extends HookMap, C = void>(hookMap: HM, hasContext?: boolean) => HookManager<HM, C>;
56
55
  export declare type RemoveManagerVoidParameter<T> = T extends (initalValue: infer I, extraArg: infer E, context: infer C) => infer R ? null extends I ? null extends E ? null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : void extends E ? null extends C ? (initialValue: I, context: C) => R : void extends C ? (initialValue: I) => R : (initialValue: I, context: C) => R : null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : void extends I ? null extends E ? null extends C ? (extraArg: E, context: C) => R : void extends C ? (extraArg: E) => R : (extraArg: E, context: C) => R : void extends E ? null extends C ? (context: C) => R : void extends C ? () => R : (context: C) => R : null extends C ? (extraArg: E, context: C) => R : void extends C ? (extraArg: E) => R : (extraArg: E, context: C) => R : null extends E ? null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : void extends E ? null extends C ? (initialValue: I, context: C) => R : void extends C ? (initialValue: I) => R : (initialValue: I, context: C) => R : null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : T;
package/esm/hookGroup.js CHANGED
@@ -1,11 +1,12 @@
1
- import { createSyncHook, createSyncBailHook, createSyncWaterfallHook, createAsyncParallelHook, createAsyncSeriesWaterfallHook } from './hooks';
1
+ import { createSyncHook, createSyncBailHook, createSyncWaterfallHook, createAsyncParallelHook, createAsyncSeriesWaterfallHook, createAsyncSeriesBailHook, createAsyncSeriesHook } from './hooks';
2
2
  export const DEFAULT_OPTIONS = {
3
3
  name: 'untitled',
4
4
  pre: [],
5
5
  post: [],
6
6
  rivals: [],
7
7
  required: [],
8
- order: 0
8
+ order: 0,
9
+ group: 0
9
10
  };
10
11
  export const SYNC_PLUGIN_SYMBOL = 'SYNC_PLUGIN_SYMBOL';
11
12
  export const isPluginInstance = (plugin) => plugin &&
@@ -13,7 +14,15 @@ export const isPluginInstance = (plugin) => plugin &&
13
14
  plugin.SYNC_PLUGIN_SYMBOL === SYNC_PLUGIN_SYMBOL;
14
15
  const sortPlugins = (input) => {
15
16
  let plugins = input.slice();
16
- plugins.sort((a, b) => a.order - b.order);
17
+ plugins.sort((a, b) => {
18
+ // sort by group first
19
+ if (a.group === b.group) {
20
+ return a.order - b.order;
21
+ }
22
+ else {
23
+ return a.group - b.group;
24
+ }
25
+ });
17
26
  for (let i = 0; i < plugins.length; i++) {
18
27
  let plugin = plugins[i];
19
28
  if (plugin.pre) {
@@ -90,6 +99,12 @@ const copyHookMap = (hookMap) => {
90
99
  case 'AsyncParallelHook':
91
100
  newHookMap[hookName] = createAsyncParallelHook();
92
101
  break;
102
+ case 'AsyncSeriesHook':
103
+ newHookMap[hookName] = createAsyncSeriesHook();
104
+ break;
105
+ case 'AsyncSeriesBailHook':
106
+ newHookMap[hookName] = createAsyncSeriesBailHook();
107
+ break;
93
108
  case 'AsyncSeriesWaterfallHook':
94
109
  newHookMap[hookName] = createAsyncSeriesWaterfallHook();
95
110
  break;
package/esm/hooks.d.ts CHANGED
@@ -5,6 +5,8 @@ export declare type Remove3VoidParameter<T> = T extends (initalValue: infer I, e
5
5
  export declare type SyncHookHandler<I, E, R> = (initalValue: I, extraArg: E) => R;
6
6
  export declare type SyncBailHookHandler<I, E, R> = (initalValue: I, extraArg: E) => R | undefined | void;
7
7
  export declare type SyncWaterfallHookHandler<I, E> = (initalValue: I, extraArgs: E) => I;
8
+ export declare type AsyncSeriesHookHandler<I, E, R> = (initalValue: I, extraArg: E) => Promise<R> | R;
9
+ export declare type AsyncSeriesBailHookHandler<I, E, R> = (initalValue: I, extraArg: E) => Promise<R | undefined | void> | R | undefined | void;
8
10
  export declare type AsyncSeriesWaterfallHookHandler<I, E> = (initalValue: I, extraArg: E) => Promise<I> | I;
9
11
  export declare type AsyncParallelHookHandler<I, E, R> = (initalValue: I, extraArgs: E) => Promise<R> | R;
10
12
  /** Normal hook. */
@@ -28,15 +30,19 @@ export declare type SyncWaterfallHook<I, E = void> = {
28
30
  clear: () => void;
29
31
  type: string;
30
32
  };
31
- /** Normal async hook. No return value
32
- *
33
- * RemoveVoidParameter<(
34
- null extends R ?
35
- (R extends void ? ((initalValue: I, extraArg: E) => Promise<R[]>) :
36
- ((initalValue: I, extraArg: E) => Promise<R[]>)) :
37
- ((initalValue: I, extraArg: E) => Promise<R[]>)
38
- )>
39
- */
33
+ export declare type AsyncSeriesHook<I = void, E = void, R = void> = {
34
+ use: (...handlers: RemoveVoidParameter<AsyncSeriesHookHandler<I, E, R>>[]) => void;
35
+ run: RemoveVoidParameter<(initalValue: I, extraArg: E) => Promise<R[]>>;
36
+ clear: () => void;
37
+ type: string;
38
+ };
39
+ export declare type AsyncSeriesBailHook<I = void, E = void, R = I> = {
40
+ use: (...handlers: RemoveVoidParameter<AsyncSeriesBailHookHandler<I, E, R>>[]) => void;
41
+ run: RemoveVoidParameter<AsyncSeriesBailHookHandler<I, E, R>>;
42
+ clear: () => void;
43
+ type: string;
44
+ };
45
+ /** Normal async parallel hook */
40
46
  export declare type AsyncParallelHook<I = void, E = void, R = void> = {
41
47
  use: (...handlers: RemoveVoidParameter<AsyncParallelHookHandler<I, E, R>>[]) => void;
42
48
  run: RemoveVoidParameter<(initalValue: I, extraArg: E) => Promise<R[]>>;
@@ -54,4 +60,6 @@ export declare const createSyncHook: <I = void, E = void, R = void>() => SyncHoo
54
60
  export declare const createSyncBailHook: <I = void, E = void, R = I>() => SyncBailHook<I, E, R>;
55
61
  export declare const createSyncWaterfallHook: <I = void, E = void>() => SyncWaterfallHook<I, E>;
56
62
  export declare const createAsyncParallelHook: <I = void, E = void, R = void>() => AsyncParallelHook<I, E, R>;
63
+ export declare const createAsyncSeriesHook: <I = void, E = void, R = void>() => AsyncSeriesHook<I, E, R>;
64
+ export declare const createAsyncSeriesBailHook: <I = void, E = void, R = I>() => AsyncSeriesBailHook<I, E, R>;
57
65
  export declare const createAsyncSeriesWaterfallHook: <I = void, E = void>() => AsyncSeriesWaterfallHook<I, E>;
package/esm/hooks.js CHANGED
@@ -97,6 +97,56 @@ export const createAsyncParallelHook = () => {
97
97
  type: 'AsyncParallelHook'
98
98
  };
99
99
  };
100
+ export const createAsyncSeriesHook = () => {
101
+ let _handlers = [];
102
+ const use = (...handlers) => {
103
+ _handlers.push(...handlers);
104
+ };
105
+ const run = (...args) => __awaiter(void 0, void 0, void 0, function* () {
106
+ const result = [];
107
+ for (let i = 0; i < _handlers.length; i++) {
108
+ const handler = _handlers[i];
109
+ // @ts-ignore
110
+ const currentResult = yield handler(...args);
111
+ result.push(currentResult);
112
+ }
113
+ return result;
114
+ });
115
+ const clear = () => {
116
+ _handlers = [];
117
+ };
118
+ return {
119
+ use,
120
+ run,
121
+ clear,
122
+ type: 'AsyncSeriesHook'
123
+ };
124
+ };
125
+ export const createAsyncSeriesBailHook = () => {
126
+ let _handlers = [];
127
+ const use = (...handlers) => {
128
+ _handlers.push(...handlers);
129
+ };
130
+ const run = (...args) => __awaiter(void 0, void 0, void 0, function* () {
131
+ for (let i = 0; i < _handlers.length; i++) {
132
+ const handler = _handlers[i];
133
+ // @ts-ignore
134
+ const result = yield handler(...args);
135
+ if (result !== undefined)
136
+ return result;
137
+ }
138
+ return undefined;
139
+ });
140
+ const clear = () => {
141
+ _handlers = [];
142
+ };
143
+ return {
144
+ use,
145
+ run,
146
+ clear,
147
+ type: 'AsyncSeriesHook'
148
+ };
149
+ };
100
150
  export const createAsyncSeriesWaterfallHook = () => {
101
151
  let _handlers = [];
102
152
  const use = (...handlers) => {
@@ -1,41 +1,39 @@
1
- import { SyncHook, SyncBailHook, SyncWaterfallHook, AsyncParallelHook, AsyncSeriesWaterfallHook, SyncHookHandler, SyncBailHookHandler, SyncWaterfallHookHandler, AsyncParallelHookHandler, AsyncSeriesWaterfallHookHandler } from './hooks';
1
+ import { SyncHook, SyncBailHook, SyncWaterfallHook, AsyncParallelHook, AsyncSeriesWaterfallHook, SyncHookHandler, SyncBailHookHandler, SyncWaterfallHookHandler, AsyncParallelHookHandler, AsyncSeriesWaterfallHookHandler, AsyncSeriesHook, AsyncSeriesBailHook, AsyncSeriesHookHandler, AsyncSeriesBailHookHandler } from './hooks';
2
2
  export declare type PatchPluginParameter<T, C> = RemoveManagerVoidParameter<AddContextParameter<T, C>>;
3
3
  export declare type AddContextParameter<T, C> = T extends (initalValue: infer I, extraArg: infer E) => infer R ? (initalValue: I, extraArg: E, context: C) => R : T;
4
- export declare type AnyHook = SyncHook<any, any, any> | SyncBailHook<any, any, any> | SyncWaterfallHook<any, any> | AsyncParallelHook<any, any, any> | AsyncSeriesWaterfallHook<any, any>;
4
+ export declare type AnyHook = SyncHook<any, any, any> | SyncBailHook<any, any, any> | SyncWaterfallHook<any, any> | AsyncParallelHook<any, any, any> | AsyncSeriesHook<any, any, any> | AsyncSeriesBailHook<any, any, any> | AsyncSeriesWaterfallHook<any, any>;
5
5
  export interface HookMap {
6
6
  [x: string]: AnyHook;
7
7
  }
8
- export declare type Setup<EHM> = (utils: {
9
- addHooks: (hook: Partial<EHM>) => void;
8
+ export declare type Setup<HM extends HookMap = {}> = (utils: {
9
+ addHooks: (hook: Partial<HM>) => void;
10
10
  }) => void;
11
- export declare type CreatePlugin<HM extends HookMap, C, EHM extends HookMap> = (pluginHandlers: IPluginHandlers<HM & EHM, C> & {
12
- setup?: Setup<EHM>;
13
- }, options?: PluginOptions) => IPluginInstance<HM & EHM, C>;
14
- export declare type HookManager<HM extends HookMap, C, EHM extends HookMap> = {
15
- createPlugin: (pluginHandlers: IPluginHandlers<HM & EHM, C> & {
16
- setup?: Setup<EHM>;
17
- }, options?: PluginOptions) => IPluginInstance<HM & EHM, C>;
18
- usePlugin: (...plugins: IPluginInstance<HM & EHM, C>[]) => void;
19
- runner: RunnerType<HM, EHM>;
11
+ export declare type CreatePlugin<HM extends HookMap, C> = (pluginHandlers: IPluginHandlers<HM, C> & {
12
+ setup?: Setup;
13
+ }, options?: PluginOptions) => IPluginInstance<HM, C>;
14
+ export declare type HookManager<HM extends HookMap, C> = {
15
+ createPlugin: CreatePlugin<HM, C>;
16
+ usePlugin: (...plugins: IPluginInstance<HM, C>[]) => void;
17
+ runner: RunnerType<HM>;
20
18
  setContext: (context: C) => void;
21
19
  clear: () => void;
22
- addHooks: (hook: Partial<EHM>) => void;
23
- hooks: HM | (HM & EHM);
24
- getPlugins: () => IPluginInstance<HM & EHM, C>[];
20
+ addHooks: <EHM extends HookMap>(hook: Partial<EHM>) => void;
21
+ hooks: HM;
22
+ getPlugins: () => IPluginInstance<HM, C>[];
25
23
  };
26
- export declare type RunnerType<HM, EHM> = {
27
- [K in keyof (HM & EHM)]: HookRunnerType<(HM & EHM)[K]>;
24
+ export declare type RunnerType<HM> = {
25
+ [K in keyof HM]: HookRunnerType<HM[K]>;
28
26
  } & {
29
- setup: Setup<EHM>;
27
+ setup: Setup;
30
28
  };
31
- export declare type HookRunnerType<H> = H extends SyncHook<infer T, infer E, infer R> ? SyncHook<T, E, R>['run'] : H extends SyncBailHook<infer T, infer E, infer R> ? SyncBailHook<T, E, R>['run'] : H extends SyncWaterfallHook<infer T, infer E> ? SyncWaterfallHook<T, E>['run'] : H extends AsyncParallelHook<infer T, infer E, infer R> ? AsyncParallelHook<T, E, R>['run'] : H extends AsyncSeriesWaterfallHook<infer T, infer E> ? AsyncSeriesWaterfallHook<T, E>['run'] : never;
29
+ export declare type HookRunnerType<H> = H extends SyncHook<infer T, infer E, infer R> ? SyncHook<T, E, R>['run'] : H extends SyncBailHook<infer T, infer E, infer R> ? SyncBailHook<T, E, R>['run'] : H extends SyncWaterfallHook<infer T, infer E> ? SyncWaterfallHook<T, E>['run'] : H extends AsyncParallelHook<infer T, infer E, infer R> ? AsyncParallelHook<T, E, R>['run'] : H extends AsyncSeriesHook<infer T, infer E, infer R> ? AsyncSeriesHook<T, E, R>['run'] : H extends AsyncSeriesBailHook<infer T, infer E, infer R> ? AsyncSeriesBailHook<T, E, R>['run'] : H extends AsyncSeriesWaterfallHook<infer T, infer E> ? AsyncSeriesWaterfallHook<T, E>['run'] : never;
32
30
  export declare type IPluginInstance<HM, C> = {
33
31
  handlers: IPluginHandlers<HM, C>;
34
32
  SYNC_PLUGIN_SYMBOL: 'SYNC_PLUGIN_SYMBOL';
35
- } & PluginOptions;
33
+ } & Required<PluginOptions>;
36
34
  export declare type IPluginHandlers<HM, C> = Partial<IPluginHandlersFullMap<HM, C>>;
37
35
  export declare type IPluginHandlersFullMap<HM, C> = {
38
- [K in keyof HM]: HM[K] extends SyncHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncHookHandler<T, E, R>, C> : HM[K] extends SyncBailHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncBailHookHandler<T, E, R>, C> : HM[K] extends SyncWaterfallHook<infer T, infer E> ? PatchPluginParameter<SyncWaterfallHookHandler<T, E>, C> : HM[K] extends AsyncParallelHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncParallelHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesWaterfallHook<infer T, infer E> ? PatchPluginParameter<AsyncSeriesWaterfallHookHandler<T, E>, C> : never;
36
+ [K in keyof HM]: HM[K] extends SyncHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncHookHandler<T, E, R>, C> : HM[K] extends SyncBailHook<infer T, infer E, infer R> ? PatchPluginParameter<SyncBailHookHandler<T, E, R>, C> : HM[K] extends SyncWaterfallHook<infer T, infer E> ? PatchPluginParameter<SyncWaterfallHookHandler<T, E>, C> : HM[K] extends AsyncParallelHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncParallelHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncSeriesHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesBailHook<infer T, infer E, infer R> ? PatchPluginParameter<AsyncSeriesBailHookHandler<T, E, R>, C> : HM[K] extends AsyncSeriesWaterfallHook<infer T, infer E> ? PatchPluginParameter<AsyncSeriesWaterfallHookHandler<T, E>, C> : never;
39
37
  };
40
38
  export declare type PluginOptions = {
41
39
  name?: string;
@@ -44,6 +42,7 @@ export declare type PluginOptions = {
44
42
  rivals?: string[];
45
43
  required?: string[];
46
44
  order?: number;
45
+ group?: number;
47
46
  [x: string]: any;
48
47
  };
49
48
  export declare const DEFAULT_OPTIONS: Required<PluginOptions>;
@@ -52,5 +51,5 @@ export declare const isPluginInstance: (plugin: any) => any;
52
51
  export declare type ArrayElements<T extends {}> = {
53
52
  [K in keyof T]: T[K][];
54
53
  };
55
- export declare const createHookManager: <HM extends HookMap, C = void, EHM extends HookMap = {}>(hookMap: HM, hasContext?: boolean) => HookManager<HM, C, EHM>;
54
+ export declare const createHookManager: <HM extends HookMap, C = void>(hookMap: HM, hasContext?: boolean) => HookManager<HM, C>;
56
55
  export declare type RemoveManagerVoidParameter<T> = T extends (initalValue: infer I, extraArg: infer E, context: infer C) => infer R ? null extends I ? null extends E ? null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : void extends E ? null extends C ? (initialValue: I, context: C) => R : void extends C ? (initialValue: I) => R : (initialValue: I, context: C) => R : null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : void extends I ? null extends E ? null extends C ? (extraArg: E, context: C) => R : void extends C ? (extraArg: E) => R : (extraArg: E, context: C) => R : void extends E ? null extends C ? (context: C) => R : void extends C ? () => R : (context: C) => R : null extends C ? (extraArg: E, context: C) => R : void extends C ? (extraArg: E) => R : (extraArg: E, context: C) => R : null extends E ? null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : void extends E ? null extends C ? (initialValue: I, context: C) => R : void extends C ? (initialValue: I) => R : (initialValue: I, context: C) => R : null extends C ? (initialValue: I, extraArg: E, context: C) => R : void extends C ? (initialValue: I, extraArg: E) => R : (initialValue: I, extraArg: E, context: C) => R : T;
package/lib/hookGroup.js CHANGED
@@ -8,7 +8,8 @@ exports.DEFAULT_OPTIONS = {
8
8
  post: [],
9
9
  rivals: [],
10
10
  required: [],
11
- order: 0
11
+ order: 0,
12
+ group: 0
12
13
  };
13
14
  exports.SYNC_PLUGIN_SYMBOL = 'SYNC_PLUGIN_SYMBOL';
14
15
  const isPluginInstance = (plugin) => plugin &&
@@ -17,7 +18,15 @@ const isPluginInstance = (plugin) => plugin &&
17
18
  exports.isPluginInstance = isPluginInstance;
18
19
  const sortPlugins = (input) => {
19
20
  let plugins = input.slice();
20
- plugins.sort((a, b) => a.order - b.order);
21
+ plugins.sort((a, b) => {
22
+ // sort by group first
23
+ if (a.group === b.group) {
24
+ return a.order - b.order;
25
+ }
26
+ else {
27
+ return a.group - b.group;
28
+ }
29
+ });
21
30
  for (let i = 0; i < plugins.length; i++) {
22
31
  let plugin = plugins[i];
23
32
  if (plugin.pre) {
@@ -94,6 +103,12 @@ const copyHookMap = (hookMap) => {
94
103
  case 'AsyncParallelHook':
95
104
  newHookMap[hookName] = (0, hooks_1.createAsyncParallelHook)();
96
105
  break;
106
+ case 'AsyncSeriesHook':
107
+ newHookMap[hookName] = (0, hooks_1.createAsyncSeriesHook)();
108
+ break;
109
+ case 'AsyncSeriesBailHook':
110
+ newHookMap[hookName] = (0, hooks_1.createAsyncSeriesBailHook)();
111
+ break;
97
112
  case 'AsyncSeriesWaterfallHook':
98
113
  newHookMap[hookName] = (0, hooks_1.createAsyncSeriesWaterfallHook)();
99
114
  break;
package/lib/hooks.d.ts CHANGED
@@ -5,6 +5,8 @@ export declare type Remove3VoidParameter<T> = T extends (initalValue: infer I, e
5
5
  export declare type SyncHookHandler<I, E, R> = (initalValue: I, extraArg: E) => R;
6
6
  export declare type SyncBailHookHandler<I, E, R> = (initalValue: I, extraArg: E) => R | undefined | void;
7
7
  export declare type SyncWaterfallHookHandler<I, E> = (initalValue: I, extraArgs: E) => I;
8
+ export declare type AsyncSeriesHookHandler<I, E, R> = (initalValue: I, extraArg: E) => Promise<R> | R;
9
+ export declare type AsyncSeriesBailHookHandler<I, E, R> = (initalValue: I, extraArg: E) => Promise<R | undefined | void> | R | undefined | void;
8
10
  export declare type AsyncSeriesWaterfallHookHandler<I, E> = (initalValue: I, extraArg: E) => Promise<I> | I;
9
11
  export declare type AsyncParallelHookHandler<I, E, R> = (initalValue: I, extraArgs: E) => Promise<R> | R;
10
12
  /** Normal hook. */
@@ -28,15 +30,19 @@ export declare type SyncWaterfallHook<I, E = void> = {
28
30
  clear: () => void;
29
31
  type: string;
30
32
  };
31
- /** Normal async hook. No return value
32
- *
33
- * RemoveVoidParameter<(
34
- null extends R ?
35
- (R extends void ? ((initalValue: I, extraArg: E) => Promise<R[]>) :
36
- ((initalValue: I, extraArg: E) => Promise<R[]>)) :
37
- ((initalValue: I, extraArg: E) => Promise<R[]>)
38
- )>
39
- */
33
+ export declare type AsyncSeriesHook<I = void, E = void, R = void> = {
34
+ use: (...handlers: RemoveVoidParameter<AsyncSeriesHookHandler<I, E, R>>[]) => void;
35
+ run: RemoveVoidParameter<(initalValue: I, extraArg: E) => Promise<R[]>>;
36
+ clear: () => void;
37
+ type: string;
38
+ };
39
+ export declare type AsyncSeriesBailHook<I = void, E = void, R = I> = {
40
+ use: (...handlers: RemoveVoidParameter<AsyncSeriesBailHookHandler<I, E, R>>[]) => void;
41
+ run: RemoveVoidParameter<AsyncSeriesBailHookHandler<I, E, R>>;
42
+ clear: () => void;
43
+ type: string;
44
+ };
45
+ /** Normal async parallel hook */
40
46
  export declare type AsyncParallelHook<I = void, E = void, R = void> = {
41
47
  use: (...handlers: RemoveVoidParameter<AsyncParallelHookHandler<I, E, R>>[]) => void;
42
48
  run: RemoveVoidParameter<(initalValue: I, extraArg: E) => Promise<R[]>>;
@@ -54,4 +60,6 @@ export declare const createSyncHook: <I = void, E = void, R = void>() => SyncHoo
54
60
  export declare const createSyncBailHook: <I = void, E = void, R = I>() => SyncBailHook<I, E, R>;
55
61
  export declare const createSyncWaterfallHook: <I = void, E = void>() => SyncWaterfallHook<I, E>;
56
62
  export declare const createAsyncParallelHook: <I = void, E = void, R = void>() => AsyncParallelHook<I, E, R>;
63
+ export declare const createAsyncSeriesHook: <I = void, E = void, R = void>() => AsyncSeriesHook<I, E, R>;
64
+ export declare const createAsyncSeriesBailHook: <I = void, E = void, R = I>() => AsyncSeriesBailHook<I, E, R>;
57
65
  export declare const createAsyncSeriesWaterfallHook: <I = void, E = void>() => AsyncSeriesWaterfallHook<I, E>;
package/lib/hooks.js CHANGED
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  });
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.createAsyncSeriesWaterfallHook = exports.createAsyncParallelHook = exports.createSyncWaterfallHook = exports.createSyncBailHook = exports.createSyncHook = void 0;
14
+ exports.createAsyncSeriesWaterfallHook = exports.createAsyncSeriesBailHook = exports.createAsyncSeriesHook = exports.createAsyncParallelHook = exports.createSyncWaterfallHook = exports.createSyncBailHook = exports.createSyncHook = void 0;
15
15
  const createSyncHook = () => {
16
16
  let _handlers = [];
17
17
  const use = (...handlers) => {
@@ -104,6 +104,58 @@ const createAsyncParallelHook = () => {
104
104
  };
105
105
  };
106
106
  exports.createAsyncParallelHook = createAsyncParallelHook;
107
+ const createAsyncSeriesHook = () => {
108
+ let _handlers = [];
109
+ const use = (...handlers) => {
110
+ _handlers.push(...handlers);
111
+ };
112
+ const run = (...args) => __awaiter(void 0, void 0, void 0, function* () {
113
+ const result = [];
114
+ for (let i = 0; i < _handlers.length; i++) {
115
+ const handler = _handlers[i];
116
+ // @ts-ignore
117
+ const currentResult = yield handler(...args);
118
+ result.push(currentResult);
119
+ }
120
+ return result;
121
+ });
122
+ const clear = () => {
123
+ _handlers = [];
124
+ };
125
+ return {
126
+ use,
127
+ run,
128
+ clear,
129
+ type: 'AsyncSeriesHook'
130
+ };
131
+ };
132
+ exports.createAsyncSeriesHook = createAsyncSeriesHook;
133
+ const createAsyncSeriesBailHook = () => {
134
+ let _handlers = [];
135
+ const use = (...handlers) => {
136
+ _handlers.push(...handlers);
137
+ };
138
+ const run = (...args) => __awaiter(void 0, void 0, void 0, function* () {
139
+ for (let i = 0; i < _handlers.length; i++) {
140
+ const handler = _handlers[i];
141
+ // @ts-ignore
142
+ const result = yield handler(...args);
143
+ if (result !== undefined)
144
+ return result;
145
+ }
146
+ return undefined;
147
+ });
148
+ const clear = () => {
149
+ _handlers = [];
150
+ };
151
+ return {
152
+ use,
153
+ run,
154
+ clear,
155
+ type: 'AsyncSeriesHook'
156
+ };
157
+ };
158
+ exports.createAsyncSeriesBailHook = createAsyncSeriesBailHook;
107
159
  const createAsyncSeriesWaterfallHook = () => {
108
160
  let _handlers = [];
109
161
  const use = (...handlers) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/hook",
3
- "version": "1.0.0-rc.0",
3
+ "version": "1.0.0-rc.11",
4
4
  "license": "MIT",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -12,7 +12,6 @@
12
12
  "engines": {
13
13
  "node": ">= 12.0.0"
14
14
  },
15
- "author": "Zheng Yu Tay",
16
15
  "scripts": {
17
16
  "dev": "run-p watch:*",
18
17
  "watch:esm": "tsc -p tsconfig.build.json -m esnext --outDir esm -w",
@@ -21,5 +20,6 @@
21
20
  "build": "run-p build:*",
22
21
  "build:esm": "tsc -p tsconfig.build.json -m esnext --outDir esm",
23
22
  "build:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib"
24
- }
25
- }
23
+ },
24
+ "author": "Zheng Yu Tay"
25
+ }