@shuvi/hook 1.0.0-rc.9 → 1.0.0
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/esm/hookGroup.d.ts +3 -4
- package/esm/hookGroup.js +8 -8
- package/esm/hooks.js +8 -2
- package/lib/hookGroup.d.ts +3 -4
- package/lib/hookGroup.js +10 -9
- package/lib/hooks.js +8 -2
- package/package.json +1 -1
package/esm/hookGroup.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export declare type RunnerType<HM> = {
|
|
|
29
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;
|
|
30
30
|
export declare type IPluginInstance<HM, C> = {
|
|
31
31
|
handlers: IPluginHandlers<HM, C>;
|
|
32
|
-
|
|
32
|
+
PLUGIN_SYMBOL: 'PLUGIN_SYMBOL';
|
|
33
33
|
} & Required<PluginOptions>;
|
|
34
34
|
export declare type IPluginHandlers<HM, C> = Partial<IPluginHandlersFullMap<HM, C>>;
|
|
35
35
|
export declare type IPluginHandlersFullMap<HM, C> = {
|
|
@@ -45,11 +45,10 @@ export declare type PluginOptions = {
|
|
|
45
45
|
group?: number;
|
|
46
46
|
[x: string]: any;
|
|
47
47
|
};
|
|
48
|
-
export declare const
|
|
49
|
-
export declare const SYNC_PLUGIN_SYMBOL = "SYNC_PLUGIN_SYMBOL";
|
|
50
|
-
export declare const isPluginInstance: (plugin: any) => any;
|
|
48
|
+
export declare const isPluginInstance: <T extends IPluginInstance<any, any> = IPluginInstance<any, any>>(plugin: any) => plugin is T;
|
|
51
49
|
export declare type ArrayElements<T extends {}> = {
|
|
52
50
|
[K in keyof T]: T[K][];
|
|
53
51
|
};
|
|
52
|
+
export declare function createPlugin<HM extends HookMap, C = void>(pluginHandlers: IPluginHandlers<HM, C>, options?: PluginOptions): IPluginInstance<HM, C>;
|
|
54
53
|
export declare const createHookManager: <HM extends HookMap, C = void>(hookMap: HM, hasContext?: boolean) => HookManager<HM, C>;
|
|
55
54
|
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,5 +1,5 @@
|
|
|
1
1
|
import { createSyncHook, createSyncBailHook, createSyncWaterfallHook, createAsyncParallelHook, createAsyncSeriesWaterfallHook, createAsyncSeriesBailHook, createAsyncSeriesHook } from './hooks';
|
|
2
|
-
|
|
2
|
+
const DEFAULT_OPTIONS = {
|
|
3
3
|
name: 'untitled',
|
|
4
4
|
pre: [],
|
|
5
5
|
post: [],
|
|
@@ -8,10 +8,10 @@ export const DEFAULT_OPTIONS = {
|
|
|
8
8
|
order: 0,
|
|
9
9
|
group: 0
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
const PLUGIN_SYMBOL = 'PLUGIN_SYMBOL';
|
|
12
12
|
export const isPluginInstance = (plugin) => plugin &&
|
|
13
|
-
plugin.hasOwnProperty(
|
|
14
|
-
plugin.
|
|
13
|
+
plugin.hasOwnProperty(PLUGIN_SYMBOL) &&
|
|
14
|
+
plugin.PLUGIN_SYMBOL === PLUGIN_SYMBOL;
|
|
15
15
|
const sortPlugins = (input) => {
|
|
16
16
|
let plugins = input.slice();
|
|
17
17
|
plugins.sort((a, b) => {
|
|
@@ -113,6 +113,9 @@ const copyHookMap = (hookMap) => {
|
|
|
113
113
|
}
|
|
114
114
|
return newHookMap;
|
|
115
115
|
};
|
|
116
|
+
export function createPlugin(pluginHandlers, options = {}) {
|
|
117
|
+
return Object.assign(Object.assign(Object.assign(Object.assign({}, DEFAULT_OPTIONS), { name: `plugin-id-${uuid()}` }), options), { handlers: pluginHandlers, PLUGIN_SYMBOL });
|
|
118
|
+
}
|
|
116
119
|
export const createHookManager = (hookMap, hasContext = true) => {
|
|
117
120
|
const setupHook = createSyncHook();
|
|
118
121
|
const _hookMap = Object.assign(Object.assign({}, copyHookMap(hookMap)), { setup: setupHook });
|
|
@@ -126,9 +129,6 @@ export const createHookManager = (hookMap, hasContext = true) => {
|
|
|
126
129
|
const setupRunner = getRunner('setup');
|
|
127
130
|
setupRunner({ addHooks });
|
|
128
131
|
};
|
|
129
|
-
const createPlugin = (pluginHandlers, options = {}) => {
|
|
130
|
-
return Object.assign(Object.assign(Object.assign(Object.assign({}, DEFAULT_OPTIONS), { name: `plugin-id-${uuid()}` }), options), { handlers: pluginHandlers, SYNC_PLUGIN_SYMBOL });
|
|
131
|
-
};
|
|
132
132
|
const usePlugin = (...plugins) => {
|
|
133
133
|
if (_initiated) {
|
|
134
134
|
return;
|
|
@@ -246,7 +246,7 @@ export const createHookManager = (hookMap, hasContext = true) => {
|
|
|
246
246
|
}
|
|
247
247
|
});
|
|
248
248
|
return {
|
|
249
|
-
createPlugin,
|
|
249
|
+
createPlugin: createPlugin,
|
|
250
250
|
usePlugin,
|
|
251
251
|
runner: runnerProxy,
|
|
252
252
|
clear,
|
package/esm/hooks.js
CHANGED
|
@@ -63,7 +63,10 @@ export const createSyncWaterfallHook = () => {
|
|
|
63
63
|
for (let i = 0; i < _handlers.length; i++) {
|
|
64
64
|
const handler = _handlers[i];
|
|
65
65
|
// @ts-ignore
|
|
66
|
-
|
|
66
|
+
const result = handler(currentParam, ...otherArgs);
|
|
67
|
+
if (result !== undefined) {
|
|
68
|
+
currentParam = result;
|
|
69
|
+
}
|
|
67
70
|
}
|
|
68
71
|
return currentParam;
|
|
69
72
|
};
|
|
@@ -157,7 +160,10 @@ export const createAsyncSeriesWaterfallHook = () => {
|
|
|
157
160
|
for (let i = 0; i < _handlers.length; i++) {
|
|
158
161
|
const handler = _handlers[i];
|
|
159
162
|
// @ts-ignore
|
|
160
|
-
|
|
163
|
+
const result = yield handler(currentParam, ...otherArgs);
|
|
164
|
+
if (result !== undefined) {
|
|
165
|
+
currentParam = result;
|
|
166
|
+
}
|
|
161
167
|
}
|
|
162
168
|
return currentParam;
|
|
163
169
|
});
|
package/lib/hookGroup.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export declare type RunnerType<HM> = {
|
|
|
29
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;
|
|
30
30
|
export declare type IPluginInstance<HM, C> = {
|
|
31
31
|
handlers: IPluginHandlers<HM, C>;
|
|
32
|
-
|
|
32
|
+
PLUGIN_SYMBOL: 'PLUGIN_SYMBOL';
|
|
33
33
|
} & Required<PluginOptions>;
|
|
34
34
|
export declare type IPluginHandlers<HM, C> = Partial<IPluginHandlersFullMap<HM, C>>;
|
|
35
35
|
export declare type IPluginHandlersFullMap<HM, C> = {
|
|
@@ -45,11 +45,10 @@ export declare type PluginOptions = {
|
|
|
45
45
|
group?: number;
|
|
46
46
|
[x: string]: any;
|
|
47
47
|
};
|
|
48
|
-
export declare const
|
|
49
|
-
export declare const SYNC_PLUGIN_SYMBOL = "SYNC_PLUGIN_SYMBOL";
|
|
50
|
-
export declare const isPluginInstance: (plugin: any) => any;
|
|
48
|
+
export declare const isPluginInstance: <T extends IPluginInstance<any, any> = IPluginInstance<any, any>>(plugin: any) => plugin is T;
|
|
51
49
|
export declare type ArrayElements<T extends {}> = {
|
|
52
50
|
[K in keyof T]: T[K][];
|
|
53
51
|
};
|
|
52
|
+
export declare function createPlugin<HM extends HookMap, C = void>(pluginHandlers: IPluginHandlers<HM, C>, options?: PluginOptions): IPluginInstance<HM, C>;
|
|
54
53
|
export declare const createHookManager: <HM extends HookMap, C = void>(hookMap: HM, hasContext?: boolean) => HookManager<HM, C>;
|
|
55
54
|
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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createHookManager = exports.
|
|
3
|
+
exports.createHookManager = exports.createPlugin = exports.isPluginInstance = void 0;
|
|
4
4
|
const hooks_1 = require("./hooks");
|
|
5
|
-
|
|
5
|
+
const DEFAULT_OPTIONS = {
|
|
6
6
|
name: 'untitled',
|
|
7
7
|
pre: [],
|
|
8
8
|
post: [],
|
|
@@ -11,10 +11,10 @@ exports.DEFAULT_OPTIONS = {
|
|
|
11
11
|
order: 0,
|
|
12
12
|
group: 0
|
|
13
13
|
};
|
|
14
|
-
|
|
14
|
+
const PLUGIN_SYMBOL = 'PLUGIN_SYMBOL';
|
|
15
15
|
const isPluginInstance = (plugin) => plugin &&
|
|
16
|
-
plugin.hasOwnProperty(
|
|
17
|
-
plugin.
|
|
16
|
+
plugin.hasOwnProperty(PLUGIN_SYMBOL) &&
|
|
17
|
+
plugin.PLUGIN_SYMBOL === PLUGIN_SYMBOL;
|
|
18
18
|
exports.isPluginInstance = isPluginInstance;
|
|
19
19
|
const sortPlugins = (input) => {
|
|
20
20
|
let plugins = input.slice();
|
|
@@ -117,6 +117,10 @@ const copyHookMap = (hookMap) => {
|
|
|
117
117
|
}
|
|
118
118
|
return newHookMap;
|
|
119
119
|
};
|
|
120
|
+
function createPlugin(pluginHandlers, options = {}) {
|
|
121
|
+
return Object.assign(Object.assign(Object.assign(Object.assign({}, DEFAULT_OPTIONS), { name: `plugin-id-${uuid()}` }), options), { handlers: pluginHandlers, PLUGIN_SYMBOL });
|
|
122
|
+
}
|
|
123
|
+
exports.createPlugin = createPlugin;
|
|
120
124
|
const createHookManager = (hookMap, hasContext = true) => {
|
|
121
125
|
const setupHook = (0, hooks_1.createSyncHook)();
|
|
122
126
|
const _hookMap = Object.assign(Object.assign({}, copyHookMap(hookMap)), { setup: setupHook });
|
|
@@ -130,9 +134,6 @@ const createHookManager = (hookMap, hasContext = true) => {
|
|
|
130
134
|
const setupRunner = getRunner('setup');
|
|
131
135
|
setupRunner({ addHooks });
|
|
132
136
|
};
|
|
133
|
-
const createPlugin = (pluginHandlers, options = {}) => {
|
|
134
|
-
return Object.assign(Object.assign(Object.assign(Object.assign({}, exports.DEFAULT_OPTIONS), { name: `plugin-id-${uuid()}` }), options), { handlers: pluginHandlers, SYNC_PLUGIN_SYMBOL: exports.SYNC_PLUGIN_SYMBOL });
|
|
135
|
-
};
|
|
136
137
|
const usePlugin = (...plugins) => {
|
|
137
138
|
if (_initiated) {
|
|
138
139
|
return;
|
|
@@ -250,7 +251,7 @@ const createHookManager = (hookMap, hasContext = true) => {
|
|
|
250
251
|
}
|
|
251
252
|
});
|
|
252
253
|
return {
|
|
253
|
-
createPlugin,
|
|
254
|
+
createPlugin: createPlugin,
|
|
254
255
|
usePlugin,
|
|
255
256
|
runner: runnerProxy,
|
|
256
257
|
clear,
|
package/lib/hooks.js
CHANGED
|
@@ -68,7 +68,10 @@ const createSyncWaterfallHook = () => {
|
|
|
68
68
|
for (let i = 0; i < _handlers.length; i++) {
|
|
69
69
|
const handler = _handlers[i];
|
|
70
70
|
// @ts-ignore
|
|
71
|
-
|
|
71
|
+
const result = handler(currentParam, ...otherArgs);
|
|
72
|
+
if (result !== undefined) {
|
|
73
|
+
currentParam = result;
|
|
74
|
+
}
|
|
72
75
|
}
|
|
73
76
|
return currentParam;
|
|
74
77
|
};
|
|
@@ -166,7 +169,10 @@ const createAsyncSeriesWaterfallHook = () => {
|
|
|
166
169
|
for (let i = 0; i < _handlers.length; i++) {
|
|
167
170
|
const handler = _handlers[i];
|
|
168
171
|
// @ts-ignore
|
|
169
|
-
|
|
172
|
+
const result = yield handler(currentParam, ...otherArgs);
|
|
173
|
+
if (result !== undefined) {
|
|
174
|
+
currentParam = result;
|
|
175
|
+
}
|
|
170
176
|
}
|
|
171
177
|
return currentParam;
|
|
172
178
|
});
|