fastevent 2.0.2 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/devTools.js +1 -1
- package/dist/devTools.js.map +1 -1
- package/dist/devTools.mjs +1 -1
- package/dist/devTools.mjs.map +1 -1
- package/dist/eventbus/index.d.mts +1005 -0
- package/dist/eventbus/index.d.ts +1005 -0
- package/dist/eventbus/index.js +2 -0
- package/dist/eventbus/index.js.map +1 -0
- package/dist/eventbus/index.mjs +2 -0
- package/dist/eventbus/index.mjs.map +1 -0
- package/dist/executors/index.d.mts +147 -0
- package/dist/executors/index.d.ts +147 -0
- package/dist/executors/index.js +2 -0
- package/dist/executors/index.js.map +1 -0
- package/dist/executors/index.mjs +2 -0
- package/dist/executors/index.mjs.map +1 -0
- package/dist/index.d.mts +255 -144
- package/dist/index.d.ts +255 -144
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/pipes/index.d.mts +116 -0
- package/dist/pipes/index.d.ts +116 -0
- package/dist/pipes/index.js +2 -0
- package/dist/pipes/index.js.map +1 -0
- package/dist/pipes/index.mjs +2 -0
- package/dist/pipes/index.mjs.map +1 -0
- package/package.json +18 -3
- package/readme.md +217 -772
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/executors/first.ts","../../src/executors/last.ts","../../src/executors/race.ts","../../src/executors/random.ts","../../src/executors/balance.ts","../../src/utils/isFunction.ts","../../src/utils/isString.ts","../../src/utils/expandable.ts","../../src/executors/series.ts","../../src/executors/waterfall.ts","../../src/executors/parallel.ts"],"names":["first","__name","listeners","message","args","execute","forEach","listener","last","length","race","winner","abortController","abortSignal","AbortController","signal","Promise","map","resolve","then","result","abort","random","index","Math","floor","balance","count","i","undefined","isFunction","fn","isString","str","series","options","reverse","onNext","onReturns","onError","Object","assign","_","cur","results","stepResult","item","e","behavior","waterfall","previous","payload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AA,IAAMK,EAAYhD,CAAAA,CAAAA,CAACkC,CACfD,EAAAA,CAAAA,CAAOM,OAAOC,MAAO,CAAA,EAAIN,CAAAA,CAAAA,CAAS,CAErCI,OAAAA,CAAS,OAETF,CAAAA,MAAAA,CAAQpC,CAAA,CAAA,CAACsB,CAAe2B,CAAAA,CAAAA,CAAe/C,CAAAA,GAAAA,CACnCA,EAAQgD,OAAUD,CAAAA,EACtB,CAFQ,CAAA,QAAA,CAGZ,CAAA,CAAA,CARqB,CAAA,WAAA,ECbZE,IAAAA,EAAAA,CAAWnD,CAAA,CAAA,IACb,CAACC,CAAAA,CAAWC,CAASC,CAAAA,CAAAA,CAAMC,CACvBH,GAAAA,CAAAA,CAAUe,GAAIV,CAAAA,CAAAA,EAAYF,CAAQE,CAAAA,CAAAA,CAAS,CAAA,CAAA,CAAIJ,CAASC,CAAAA,CAAAA,CAAM,IAAA,CAAA,CAFrD,CAAA,UAAA","file":"index.mjs","sourcesContent":["import { FastListenerExecutor } from \"./types\"\n\n/**\n * 执行第一个监听器的执行器函数\n * \n * @param listeners - 监听器数组,每个元素是一个包含监听器函数的元组\n * @param message - 要传递给监听器的消息对象\n * @param args - 要传递给监听器的额外参数\n * @param execute - 执行监听器的函数\n * @returns 返回一个数组,包含第一个监听器的执行结果\n */\nexport const first = (): FastListenerExecutor => {\n return (listeners, message, args, execute) => {\n listeners.forEach(listener => listener[2]--)\n listeners[0][2]++\n return [\n execute(listeners[0][0], message, args)\n ]\n }\n}","import { FastListenerExecutor } from \"./types\"\n\n/**\n * 执行监听器列表中的最后一个监听器\n * @param listeners - 监听器数组\n * @param message - 事件消息\n * @param args - 事件参数\n * @param execute - 执行器函数\n * @returns 返回包含最后一个监听器执行结果的数组\n */\nexport const last = (): FastListenerExecutor => {\n return (listeners, message, args, execute) => {\n listeners.forEach(listener => listener[2]--)\n listeners[listeners.length - 1][2]++\n return [\n execute(listeners[listeners.length - 1][0], message, args)\n ]\n }\n}","import { FastListenerMeta } from \"../types\"\nimport { FastListenerExecutor } from \"./types\"\n\n/**\n * 竞态执行器 - 同时执行多个监听器,只返回最快完成的结果\n * \n * @param listeners - 监听器元数据列表\n * @param message - 要处理的消息\n * @param args - 执行参数\n * @param execute - 执行函数\n * @returns 包含最快完成监听器结果的数组\n * \n * @description\n * - 只有第一个完成的监听器结果会被返回\n * - 其他未完成的监听器会被中止\n * - 如果没有提供中止信号,会自动创建一个\n * - 胜出的监听器计数会被恢复\n */\nexport const race = (): FastListenerExecutor => {\n return (listeners, message, args, execute) => {\n let winner: FastListenerMeta | undefined\n let abortController: AbortController | undefined\n if (!args || (args && !args.abortSignal)) {\n abortController = new AbortController()\n if (!args) args = {}\n args.abortSignal = abortController.signal\n }\n const result = Promise.race(listeners.map(listener => {\n listener[2]--\n return Promise.resolve(execute(listener[0], message, args)).then((result: any) => {\n if (!winner) {\n winner = listener\n listener[2]++\n }\n abortController?.abort()\n return result\n })\n }))\n return [\n result\n ]\n }\n}","import { FastListenerExecutor } from \"./types\"\n\n/**\n * 随机执行器 - 从监听器列表中随机选择一个监听器执行\n * \n * @param listeners - 监听器列表,每个元素为 [监听器函数, 优先级, 执行次数] 的元组\n * @param message - 要处理的消息对象\n * @param args - 传递给监听器的额外参数\n * @param execute - 执行监听器的函数\n * @returns 返回包含执行结果的数组\n * \n * @remarks\n * - 随机选择一个监听器执行\n * - 所有监听器的执行次数计数减1\n * - 被选中执行的监听器执行次数加1\n */\nexport const random = (): FastListenerExecutor => {\n return (listeners, message, args, execute) => {\n const index = Math.floor(Math.random() * listeners.length)\n listeners.forEach(listener => listener[2]--)\n listeners[index][2]++\n return [\n execute(listeners[index][0], message, args)\n ]\n }\n}","import { FastListenerExecutor } from \"./types\"\n\n/**\n * 负载均衡执行器,用于在多个监听器中选择执行次数最少的进行调用\n * @param listeners 监听器列表\n * @param message 消息对象\n * @param args 参数列表\n * @param execute 执行函数\n * @returns 返回包含单个执行结果的数组\n * \n * 执行策略:\n * 1. 遍历所有监听器,找出执行次数最少的监听器\n * 2. 由于监听器执行后会自动+1,因此先对所有监听器执行次数-1,以抵消后续的+1\n * 3. 只执行选中的监听器,其他监听器不执行\n */\nexport const balance = (): FastListenerExecutor => {\n return (listeners, message, args, execute) => {\n // 找出listeners里面所有执行器的执行次数最少的项\n let count: number\n let index: number = 0\n listeners.forEach((listener, i) => {\n // 为什么所有监听器的执行次数均减1?\n // 因为监听器执行完毕后会自动+1,但是在balance执行器中,只有一个会监听器会执行,因此在此先减一用来抵消后续的+1\n listener[2]--\n if (count === undefined || count > listener[2]) {\n count = listener[2]\n index = i\n }\n })\n listeners[index][2]++\n return [execute(listeners[index][0], message, args)]\n }\n}","export function isFunction(fn: any): fn is Function {\n return fn && typeof (fn) == \"function\"\n}","export function isString(str: any): str is string {\n return str && typeof (str) === \"string\"\n}","/**\n * \n * \n * 用于包括输入值为可展开的对象\n * \n * \n * 用在emit事件\n * \n * emitter1 = new FastEvent()\n * emitter2 = new FastEvent()\n * \n * emitter1.on(\"xxx\",(message)=>{\n * return emitter2.emit(message)\n * })\n * \n * emitter2.on(\"xxx\",()=>1)\n * emitter2.on(\"xxx\",()=>2)\n * \n * const results = emitter1.emit(\"xxx\")\n * // results == [[1,2]] 而不 [1,2],因为emitter2.emit返回的是一个[]\n * \n * expandable的作用就是对emit结果进行包括,当isExpandable时,在emit内部进行展开\n * \n * \n * \n * \n * emitter1.on(\"xxx\",(message)=>{\n * // 告诉emitter2.emit返回的是一个expandable对象\n * // 然后在内部就展开此对象\n * return emitter2.emit(message)\n * })\n * \n* const results = emitter1.emit(\"xxx\")\n * // results == [1,2]\n * \n * 为了实现对结果数据的展开处理,在emit内部需要对监听器的执行结果进行依次检查\n * 这多了一个迭代操作,为了不影响性能,可以关闭此特性\n * options.expandEmitResults = false\n * \n * \n */\n\nexport const __expandable__ = Symbol.for('__expandable__')\n\nexport function expandable(value: any) {\n value[__expandable__] = true\n return value\n}\n\nexport function isExpandable(value: any) {\n return value && value[__expandable__]\n}","import { FastEventListener, FastEventListenerArgs, FastEventMessage } from \"../types\"\nimport { isString } from \"../utils\"\nimport { isFunction } from \"../utils/isFunction\"\nimport { FastListenerExecutor } from \"./types\"\n\n\nexport type SeriesExecutorOptions = {\n reverse?: boolean // 反向执行监听器\n /**\n * 控制监听器如何返回结果\n * \n * 提供一个reduce函数对所有监听器返回结果进行reduce操作\n * series({\n * onReturns: (result, cur)=>{\n * if(!result) result = []\n * return result.push(cur)\n * }\n * })\n */\n onReturns?: (results: any, cur: any) => any\n // 当调用下一个监听器前执行,返回false或触发错误均不再执行后续的监听器\n // 可以在此修改message\n onNext?: (index: number, previous: any, message: FastEventMessage, args: FastEventListenerArgs, results: any) => boolean\n // 当执行监听器出错时的回调,返回false中止后续执行\n onError?: 'skip' | 'abort' | 'error' | ((e: any, message: FastEventMessage, args: FastEventListenerArgs) => void | 'skip' | 'abort' | 'error')\n}\n\nexport const series = (options?: SeriesExecutorOptions): FastListenerExecutor => {\n const { reverse, onNext, onReturns, onError } = Object.assign({\n reverse: false,\n onError: () => { },\n onNext: () => true,\n onReturns: (_: any, cur: any) => cur\n }, options) as SeriesExecutorOptions\n return async (listeners, message, args, execute) => {\n let results: any = undefined\n let stepResult: any = undefined\n let index: number = 0\n // 全部执行次数-1\n listeners.forEach(listener => listener[2]--)\n for (let i = 0; i < listeners.length; i++) {\n const item = listeners[reverse ? listeners.length - 1 - i : i]\n const listener = item[0] as FastEventListener<any, any, any>\n try {\n if (isFunction(onNext) && onNext(++index, stepResult, message, args, results) === false) {\n break\n }\n stepResult = await execute(listener, message, args, false)\n if (isFunction(onReturns)) {\n results = onReturns(results, stepResult)\n } else {\n results = stepResult\n }\n // 实际执行次数+1\n item[2]++\n } catch (e: any) {\n try {\n const behavior = isString(onError) ? onError : isFunction(onError) && onError!(e, message, args)\n if (behavior === 'skip') {\n continue\n } else if (behavior === 'error') {\n results = e\n break\n } else if (behavior === 'abort') {\n break\n }\n } catch { }\n }\n }\n return results\n }\n\n}","import { FastEventMessage } from \"../types\"\nimport { series, SeriesExecutorOptions } from \"./series\"\nimport { FastListenerExecutor } from \"./types\"\n\nexport type WaterfallExecutorOptions = Omit<SeriesExecutorOptions, 'onError' | 'onReturns' | 'onStep'>\n\n\n/**\n * 创建一个瀑布流执行器,用于按顺序执行监听器,并将前一个监听器的结果传递给下一个监听器\n * 如果出错就不再执行后续的监听器\n * \n * @param options - 执行器配置选项\n * @returns 返回一个 FastListener 执行器实例\n * \n * @remarks\n * - 当任一监听器执行出错时,会中断后续监听器的执行\n * - 每个监听器的执行结果会作为下一个监听器的 payload 参数\n */\nexport const waterfall = (options?: WaterfallExecutorOptions): FastListenerExecutor => {\n return series(Object.assign({}, options, {\n // 出错时不再执行后续的监听器\n onError: 'abort',\n // 将结果作为payload传给下一个监听器\n onNext: (index: number, previous: any, message: FastEventMessage) => {\n message.payload = previous\n }\n }) as unknown as SeriesExecutorOptions)\n}","import { FastListenerExecutor } from \"./types\"\n\n/**\n * \n */\nexport const parallel = (): FastListenerExecutor => {\n return (listeners, message, args, execute) => {\n return listeners.map(listener => execute(listener[0], message, args, true))\n }\n}"]}
|
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,6 @@ type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
|
|
|
3
3
|
interface FaseEventMessageExtends {
|
|
4
4
|
}
|
|
5
5
|
interface FastEventMeta {
|
|
6
|
-
priority?: number;
|
|
7
6
|
}
|
|
8
7
|
type FastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
9
8
|
[K in keyof Events]: {
|
|
@@ -16,7 +15,7 @@ type FastEventEmitMessage<Events extends Record<string, any> = Record<string, an
|
|
|
16
15
|
[K in keyof Events]: {
|
|
17
16
|
type: Exclude<K, number | symbol>;
|
|
18
17
|
payload?: Events[K];
|
|
19
|
-
meta?: FastEventMeta & M & Record<string, any>;
|
|
18
|
+
meta?: Partial<FastEventMeta> & M & Record<string, any>;
|
|
20
19
|
};
|
|
21
20
|
}[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
|
|
22
21
|
type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: FastEventMessage<{
|
|
@@ -35,7 +34,7 @@ type FastListenerNode = {
|
|
|
35
34
|
type FastEventSubscriber = {
|
|
36
35
|
off: () => void;
|
|
37
36
|
/**
|
|
38
|
-
* 为什么要有一个listener引用?
|
|
37
|
+
* 为什么要有一个listener引用? 主要用于移除监听器时使用
|
|
39
38
|
*
|
|
40
39
|
* - 正常情况下
|
|
41
40
|
* const subscriber = emitter.on('event', listener)
|
|
@@ -51,11 +50,11 @@ type FastEventSubscriber = {
|
|
|
51
50
|
* subscriber.off() 可以正常生效
|
|
52
51
|
* scope.off('event', listener) // 无法生效
|
|
53
52
|
* scope.off(listener) // 无法生效
|
|
54
|
-
* 因为在scope
|
|
55
|
-
* 因此在事件注册表中登记的不是listener
|
|
53
|
+
* 因为在scope中,为了让监听器可以处理scope的逻辑,对listener进行了包装,
|
|
54
|
+
* 因此在事件注册表中登记的不是listener,而是经过包装的监听器
|
|
56
55
|
* subscriber.off() 可以正常生效
|
|
57
56
|
* 如果要使用scope.off或emitter.off
|
|
58
|
-
* 需要使用subscriber.listener, subscriber.listener
|
|
57
|
+
* 需要使用subscriber.listener, subscriber.listener记录了原始的监听器引用
|
|
59
58
|
* subscriber.listener===listener
|
|
60
59
|
*
|
|
61
60
|
* scope.off('event', subscriber.listener) // 生效
|
|
@@ -66,26 +65,24 @@ type FastEventSubscriber = {
|
|
|
66
65
|
};
|
|
67
66
|
type FastListeners = FastListenerNode;
|
|
68
67
|
type FastEventOptions<Meta = Record<string, any>, Context = any> = {
|
|
69
|
-
id
|
|
70
|
-
debug
|
|
71
|
-
delimiter
|
|
72
|
-
context
|
|
73
|
-
ignoreErrors
|
|
74
|
-
meta
|
|
75
|
-
onAddListener?: (type: string
|
|
76
|
-
onRemoveListener?: (type: string
|
|
68
|
+
id: string;
|
|
69
|
+
debug: boolean;
|
|
70
|
+
delimiter: string;
|
|
71
|
+
context: Context;
|
|
72
|
+
ignoreErrors: boolean;
|
|
73
|
+
meta: Meta;
|
|
74
|
+
onAddListener?: (type: string, listener: FastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
|
|
75
|
+
onRemoveListener?: (type: string, listener: FastEventListener) => void;
|
|
77
76
|
onClearListeners?: () => void;
|
|
78
|
-
onListenerError?: ((
|
|
79
|
-
onBeforeExecuteListener?: (message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void;
|
|
77
|
+
onListenerError?: ((error: Error, listener: FastEventListener, message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
|
|
78
|
+
onBeforeExecuteListener?: (message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
|
|
80
79
|
onAfterExecuteListener?: (message: FastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
|
|
81
80
|
/**
|
|
82
81
|
* 全局执行器
|
|
83
|
-
* allSettled: 使用Promise.allSettled()执行所有监听器
|
|
84
|
-
* race: 使用Promise.race()执行所有监听器,只有第一个执行完成就返回,其他监听器执行结果会被忽略
|
|
85
|
-
* balance: 尽可能平均执行各个侦听器
|
|
86
|
-
* sequence: 按照侦听器添加顺序依次执行
|
|
87
82
|
*/
|
|
88
|
-
executor?:
|
|
83
|
+
executor?: FastListenerExecutor;
|
|
84
|
+
onMessage?: FastEventListener;
|
|
85
|
+
expandEmitResults?: boolean;
|
|
89
86
|
};
|
|
90
87
|
interface FastEvents {
|
|
91
88
|
}
|
|
@@ -100,7 +97,6 @@ type FastEventListenOptions<Events extends Record<string, any> = Record<string,
|
|
|
100
97
|
off?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
101
98
|
pipes?: FastListenerPipe[];
|
|
102
99
|
};
|
|
103
|
-
type FastListenerExecutorArgs = 'default' | 'allSettled' | 'race' | 'balance' | 'first' | 'last' | 'random' | IFastListenerExecutor;
|
|
104
100
|
type FastEventListenerArgs<M = Record<string, any>> = {
|
|
105
101
|
retain?: boolean;
|
|
106
102
|
meta?: Partial<M> & Record<string, any>;
|
|
@@ -109,10 +105,14 @@ type FastEventListenerArgs<M = Record<string, any>> = {
|
|
|
109
105
|
*
|
|
110
106
|
* allSettled: 使用Promise.allSettled()执行所有监听器
|
|
111
107
|
* race: 使用Promise.race()执行所有监听器,只有第一个执行完成就返回,其他监听器执行结果会被忽略
|
|
112
|
-
* balance:
|
|
113
|
-
* sequence:
|
|
108
|
+
* balance: 尽可能平均执行各个监听器
|
|
109
|
+
* sequence: 按照监听器添加顺序依次执行
|
|
110
|
+
*/
|
|
111
|
+
executor?: FastListenerExecutor;
|
|
112
|
+
/**
|
|
113
|
+
* 当emit参数解析完成后的回调,用于修改emit参数
|
|
114
114
|
*/
|
|
115
|
-
|
|
115
|
+
parseArgs?: (message: FastEventMessage, args: FastEventListenerArgs) => void;
|
|
116
116
|
};
|
|
117
117
|
type Merge<T extends object, U extends object> = {
|
|
118
118
|
[K in keyof T | keyof U]: K extends keyof U ? U[K] : K extends keyof T ? T[K] : never;
|
|
@@ -120,26 +120,155 @@ type Merge<T extends object, U extends object> = {
|
|
|
120
120
|
type RequiredItems<T extends object, Items extends string[]> = Omit<T, Items[number]> & {
|
|
121
121
|
[K in Items[number] & keyof T]-?: Exclude<T[K], undefined>;
|
|
122
122
|
};
|
|
123
|
+
type Expand<T> = T extends infer O ? {
|
|
124
|
+
[K in keyof O]: O[K];
|
|
125
|
+
} : never;
|
|
126
|
+
type OptionalItems<T, K extends keyof T> = Expand<Omit<T, K> & {
|
|
127
|
+
[P in K]?: T[P];
|
|
128
|
+
}>;
|
|
129
|
+
type DeepPartial<T> = {
|
|
130
|
+
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
|
|
131
|
+
};
|
|
123
132
|
type Fallback<T, F> = [
|
|
124
133
|
T
|
|
125
134
|
] extends [never] ? F : T extends undefined ? F : T;
|
|
126
|
-
type
|
|
135
|
+
type ChangeFieldType<Record, Name extends string, Type = any> = Expand<Omit<Record, Name> & {
|
|
136
|
+
[K in Name]: Type;
|
|
137
|
+
}>;
|
|
138
|
+
type OverrideOptions<T> = ChangeFieldType<Required<T>, 'context', never>;
|
|
139
|
+
type ObjectKeys<T, I = string> = {
|
|
140
|
+
[P in keyof T]: P extends I ? P : never;
|
|
141
|
+
}[keyof T];
|
|
142
|
+
/**
|
|
143
|
+
* 从类型数组中移除重复项,返回保留唯一类型的元组
|
|
144
|
+
* @template T - 输入的任意类型数组(元组)
|
|
145
|
+
* @template Result - 内部使用的累积结果数组(默认空数组)
|
|
146
|
+
* @returns {any[]} 去重后的类型元组,保留首次出现的顺序
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* type T1 = Unique<[number, string, number]>; // [number, string]
|
|
150
|
+
* type T2 = Unique<[1, 2, 2, 3]>; // [1, 2, 3]
|
|
151
|
+
* type T3 = Unique<['a', 'b', 'a']>; // ['a', 'b']
|
|
152
|
+
*/
|
|
153
|
+
type Unique<T extends any[], Result extends any[] = []> = T extends [infer First, ...infer Rest] ? First extends Result[number] ? Unique<Rest, Result> : Unique<Rest, [...Result, First]> : Result;
|
|
154
|
+
type Overloads<T> = Unique<T extends {
|
|
155
|
+
(...args: infer A1): infer R1;
|
|
156
|
+
(...args: infer A2): infer R2;
|
|
157
|
+
(...args: infer A3): infer R3;
|
|
158
|
+
(...args: infer A4): infer R4;
|
|
159
|
+
(...args: infer A5): infer R5;
|
|
160
|
+
(...args: infer A6): infer R6;
|
|
161
|
+
(...args: infer A7): infer R7;
|
|
162
|
+
(...args: infer A8): infer R8;
|
|
163
|
+
} ? [
|
|
164
|
+
(...args: A1) => R1,
|
|
165
|
+
((...args: A2) => R2),
|
|
166
|
+
((...args: A3) => R3),
|
|
167
|
+
((...args: A4) => R4),
|
|
168
|
+
((...args: A5) => R5),
|
|
169
|
+
((...args: A6) => R6),
|
|
170
|
+
((...args: A7) => R7),
|
|
171
|
+
((...args: A8) => R8)
|
|
172
|
+
] : T extends {
|
|
173
|
+
(...args: infer A1): infer R1;
|
|
174
|
+
(...args: infer A2): infer R2;
|
|
175
|
+
(...args: infer A3): infer R3;
|
|
176
|
+
(...args: infer A4): infer R4;
|
|
177
|
+
(...args: infer A5): infer R5;
|
|
178
|
+
(...args: infer A6): infer R6;
|
|
179
|
+
(...args: infer A7): infer R7;
|
|
180
|
+
} ? [
|
|
181
|
+
(...args: A1) => R1,
|
|
182
|
+
((...args: A2) => R2),
|
|
183
|
+
((...args: A3) => R3),
|
|
184
|
+
((...args: A4) => R4),
|
|
185
|
+
((...args: A5) => R5),
|
|
186
|
+
((...args: A6) => R6),
|
|
187
|
+
((...args: A7) => R7)
|
|
188
|
+
] : T extends {
|
|
189
|
+
(...args: infer A1): infer R1;
|
|
190
|
+
(...args: infer A2): infer R2;
|
|
191
|
+
(...args: infer A3): infer R3;
|
|
192
|
+
(...args: infer A4): infer R4;
|
|
193
|
+
(...args: infer A5): infer R5;
|
|
194
|
+
(...args: infer A6): infer R6;
|
|
195
|
+
} ? [
|
|
196
|
+
(...args: A1) => R1,
|
|
197
|
+
((...args: A2) => R2),
|
|
198
|
+
((...args: A3) => R3),
|
|
199
|
+
((...args: A4) => R4),
|
|
200
|
+
((...args: A5) => R5),
|
|
201
|
+
((...args: A6) => R6)
|
|
202
|
+
] : T extends {
|
|
203
|
+
(...args: infer A1): infer R1;
|
|
204
|
+
(...args: infer A2): infer R2;
|
|
205
|
+
(...args: infer A3): infer R3;
|
|
206
|
+
(...args: infer A4): infer R4;
|
|
207
|
+
(...args: infer A5): infer R5;
|
|
208
|
+
} ? [
|
|
209
|
+
(...args: A1) => R1,
|
|
210
|
+
((...args: A2) => R2),
|
|
211
|
+
((...args: A3) => R3),
|
|
212
|
+
((...args: A4) => R4),
|
|
213
|
+
((...args: A5) => R5)
|
|
214
|
+
] : T extends {
|
|
215
|
+
(...args: infer A1): infer R1;
|
|
216
|
+
(...args: infer A2): infer R2;
|
|
217
|
+
(...args: infer A3): infer R3;
|
|
218
|
+
(...args: infer A4): infer R4;
|
|
219
|
+
} ? [
|
|
220
|
+
(...args: A1) => R1,
|
|
221
|
+
((...args: A2) => R2),
|
|
222
|
+
((...args: A3) => R3),
|
|
223
|
+
((...args: A4) => R4)
|
|
224
|
+
] : T extends {
|
|
225
|
+
(...args: infer A1): infer R1;
|
|
226
|
+
(...args: infer A2): infer R2;
|
|
227
|
+
(...args: infer A3): infer R3;
|
|
228
|
+
} ? [
|
|
229
|
+
((...args: A1) => R1),
|
|
230
|
+
((...args: A2) => R2),
|
|
231
|
+
((...args: A3) => R3)
|
|
232
|
+
] : T extends {
|
|
233
|
+
(...args: infer A1): infer R1;
|
|
234
|
+
(...args: infer A2): infer R2;
|
|
235
|
+
} ? [
|
|
236
|
+
((...args: A1) => R1),
|
|
237
|
+
((...args: A2) => R2)
|
|
238
|
+
] : T extends {
|
|
239
|
+
(...args: infer A1): infer R1;
|
|
240
|
+
} ? [
|
|
241
|
+
(...args: A1) => R1
|
|
242
|
+
] : [
|
|
243
|
+
T
|
|
244
|
+
]>;
|
|
245
|
+
type Dict<V = any> = Record<Exclude<string, number | symbol>, V>;
|
|
246
|
+
|
|
247
|
+
type FastListenerExecutor = (listeners: FastListenerMeta[], message: FastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: FastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
|
|
127
248
|
|
|
128
|
-
type FastEventScopeOptions<Meta, Context> = {
|
|
129
|
-
meta
|
|
130
|
-
context
|
|
131
|
-
executor?:
|
|
249
|
+
type FastEventScopeOptions<Meta = Record<string, any>, Context = any> = {
|
|
250
|
+
meta: FastEventScopeMeta & FastEventMeta & Meta;
|
|
251
|
+
context: Context;
|
|
252
|
+
executor?: FastListenerExecutor;
|
|
253
|
+
onMessage?: FastEventListener;
|
|
132
254
|
};
|
|
133
255
|
type FastEventScopeMeta = {
|
|
134
256
|
scope: string;
|
|
135
257
|
};
|
|
136
|
-
declare class FastEventScope<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context =
|
|
137
|
-
|
|
258
|
+
declare class FastEventScope<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, Types extends keyof Events = keyof Events, FinalMeta extends Record<string, any> = FastEventMeta & FastEventScopeMeta & Meta> {
|
|
259
|
+
__FastEventScope__: boolean;
|
|
260
|
+
private _options;
|
|
261
|
+
types: {
|
|
262
|
+
events: Events;
|
|
263
|
+
meta: FinalMeta;
|
|
264
|
+
context: Fallback<Context, typeof this>;
|
|
265
|
+
};
|
|
138
266
|
prefix: string;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
get
|
|
267
|
+
emitter: FastEvent<Events>;
|
|
268
|
+
constructor(options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>);
|
|
269
|
+
get context(): Fallback<Context, typeof this>;
|
|
270
|
+
get options(): FastEventScopeOptions<FinalMeta, Context>;
|
|
271
|
+
bind(emitter: FastEvent<any>, prefix: string, options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>): void;
|
|
143
272
|
/**
|
|
144
273
|
* 获取作用域监听器
|
|
145
274
|
* 当启用作用域时,对原始监听器进行包装,添加作用域前缀处理逻辑
|
|
@@ -150,14 +279,20 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
150
279
|
private _getScopeListener;
|
|
151
280
|
private _getScopeType;
|
|
152
281
|
private _fixScopeType;
|
|
153
|
-
on<T extends Types = Types>(type: T,
|
|
154
|
-
on<T extends string>(type: T,
|
|
155
|
-
on(type: '**',
|
|
156
|
-
|
|
157
|
-
|
|
282
|
+
on<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
283
|
+
on<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
284
|
+
on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
285
|
+
on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
286
|
+
on<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
287
|
+
on(type: '**', listener: FastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
288
|
+
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
289
|
+
once<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
290
|
+
once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
291
|
+
once<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
292
|
+
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
158
293
|
onAny<P = any>(listener: FastEventAnyListener<{
|
|
159
294
|
[K: string]: P;
|
|
160
|
-
}, FinalMeta, Context
|
|
295
|
+
}, FinalMeta, Fallback<Context, typeof this>>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
161
296
|
off(listener: FastEventListener<any, any, any>): void;
|
|
162
297
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
163
298
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
@@ -165,6 +300,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
165
300
|
off(type: Types): void;
|
|
166
301
|
offAll(): void;
|
|
167
302
|
clear(): void;
|
|
303
|
+
emit(type: Types, directive: symbol): void;
|
|
304
|
+
emit(type: string, directive: symbol): void;
|
|
168
305
|
emit<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
169
306
|
emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? Events[Types] : any, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
170
307
|
emit<R = any>(message: FastEventEmitMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
@@ -215,7 +352,13 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
215
352
|
* profileScope.emit('update', { name: 'John' });
|
|
216
353
|
* ```
|
|
217
354
|
*/
|
|
218
|
-
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: FastEventScopeOptions<Partial<FinalMeta> & M, C
|
|
355
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): FastEventScope<ScopeEvents<Events, P> & E, FinalMeta & M, C>;
|
|
356
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeInstance extends FastEventScope<any, any, any> = FastEventScope<ScopeEvents<ScopeEvents<Events, P> & E, P> & E, FinalMeta & M, C>>(prefix: P, scopeObj: ScopeInstance, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): ScopeInstance;
|
|
357
|
+
/**
|
|
358
|
+
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
359
|
+
* @param message
|
|
360
|
+
*/
|
|
361
|
+
onMessage(message: FastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
|
|
219
362
|
}
|
|
220
363
|
|
|
221
364
|
/**
|
|
@@ -225,7 +368,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
225
368
|
* @template Meta - 事件元数据类型,默认为任意键值对对象
|
|
226
369
|
* @template Types - 事件类型的键名类型,默认为Events的键名类型
|
|
227
370
|
*/
|
|
228
|
-
declare class FastEvent<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, AllEvents extends Record<string, any> = Events & FastEvents, Types extends keyof AllEvents = Exclude<keyof (AllEvents), number | symbol
|
|
371
|
+
declare class FastEvent<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, AllEvents extends Record<string, any> = Events & FastEvents, Types extends keyof AllEvents = Expand<Exclude<keyof (AllEvents), number | symbol>>> {
|
|
372
|
+
__FastEvent__: boolean;
|
|
229
373
|
/** 事件监听器树结构,存储所有注册的事件监听器 */
|
|
230
374
|
listeners: FastListeners;
|
|
231
375
|
/** 事件发射器的配置选项 */
|
|
@@ -234,11 +378,15 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
234
378
|
private _delimiter;
|
|
235
379
|
/** 事件监听器执行时的上下文对象 */
|
|
236
380
|
private _context;
|
|
237
|
-
/**
|
|
381
|
+
/** 保留的事件消息映射,Key是事件名称,Value是保留的事件消息 */
|
|
238
382
|
retainedMessages: Map<string, any>;
|
|
239
383
|
/** 当前注册的监听器总数 */
|
|
240
384
|
listenerCount: number;
|
|
241
|
-
|
|
385
|
+
types: {
|
|
386
|
+
events: AllEvents;
|
|
387
|
+
meta: Expand<FastEventMeta & Meta & Record<string, any>>;
|
|
388
|
+
context: Expand<Fallback<Context, typeof this>>;
|
|
389
|
+
};
|
|
242
390
|
/**
|
|
243
391
|
* 创建FastEvent实例
|
|
244
392
|
* @param options - 事件发射器的配置选项
|
|
@@ -250,17 +398,28 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
250
398
|
* - context: null - 监听器执行上下文
|
|
251
399
|
* - ignoreErrors: true - 是否忽略监听器执行错误
|
|
252
400
|
*/
|
|
253
|
-
constructor(options?: FastEventOptions<Meta, Context
|
|
401
|
+
constructor(options?: Partial<FastEventOptions<Meta, Context>>);
|
|
254
402
|
/** 获取事件发射器的配置选项 */
|
|
255
|
-
get options():
|
|
256
|
-
get context(): Context
|
|
403
|
+
get options(): FastEventOptions<Meta, Context>;
|
|
404
|
+
get context(): Fallback<Context, typeof this>;
|
|
405
|
+
get meta(): Meta;
|
|
257
406
|
/** 获取事件发射器的唯一标识符 */
|
|
258
407
|
get id(): string;
|
|
408
|
+
/**
|
|
409
|
+
* 添加事件监听器到事件树中
|
|
410
|
+
* @param parts - 事件路径数组
|
|
411
|
+
* @param listener - 事件监听器函数
|
|
412
|
+
* @param options - 监听器配置选项
|
|
413
|
+
* @param options.count - 监听器触发次数限制
|
|
414
|
+
* @param options.prepend - 是否将监听器添加到监听器列表开头
|
|
415
|
+
* @returns [节点, 监听器索引] - 返回监听器所在节点和在节点监听器列表中的索引
|
|
416
|
+
* @private
|
|
417
|
+
*/
|
|
259
418
|
private _addListener;
|
|
260
419
|
private _enableDevTools;
|
|
261
420
|
/**
|
|
262
421
|
*
|
|
263
|
-
* 根据parts
|
|
422
|
+
* 根据parts路径遍历监听器树,并在最后的节点上执行回调函数
|
|
264
423
|
*
|
|
265
424
|
* @param parts
|
|
266
425
|
* @param callback
|
|
@@ -354,7 +513,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
354
513
|
* 此方法供子类继承
|
|
355
514
|
*
|
|
356
515
|
*/
|
|
357
|
-
onMessage(message: FastEventMessage): void;
|
|
516
|
+
onMessage(message: FastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
|
|
358
517
|
off(listener: FastEventListener<any, any, any>): void;
|
|
359
518
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
360
519
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
@@ -368,7 +527,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
368
527
|
* - 如果没有提供prefix,则清除所有监听器
|
|
369
528
|
* - 同时会清空保留的事件(_retainedEvents)
|
|
370
529
|
* - 重置监听器对象为空
|
|
371
|
-
|
|
530
|
+
|
|
372
531
|
* @example
|
|
373
532
|
*
|
|
374
533
|
* ```ts
|
|
@@ -386,7 +545,19 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
386
545
|
*/
|
|
387
546
|
private _removeRetainedEvents;
|
|
388
547
|
clear(prefix?: string): void;
|
|
389
|
-
|
|
548
|
+
/**
|
|
549
|
+
* 发送最后一次事件的消息给对应的监听器
|
|
550
|
+
*
|
|
551
|
+
* @param type - 事件类型,支持通配符(*)匹配
|
|
552
|
+
* @private
|
|
553
|
+
*
|
|
554
|
+
* 处理流程:
|
|
555
|
+
* 1. 如果事件类型包含通配符,则遍历所有保留的消息,匹配符合模式的事件
|
|
556
|
+
* 2. 如果是普通事件类型,则直接获取对应的保留消息
|
|
557
|
+
* 3. 遍历匹配到的消息,查找对应路径的监听器节点
|
|
558
|
+
* 4. 执行所有匹配到的监听器
|
|
559
|
+
*/
|
|
560
|
+
private _emitRetainMessage;
|
|
390
561
|
/**
|
|
391
562
|
* 遍历监听器节点树
|
|
392
563
|
* @param node 当前遍历的监听器节点
|
|
@@ -408,6 +579,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
408
579
|
* 执行单个监听器函数
|
|
409
580
|
* @param listener - 要执行的监听器函数或包装过的监听器对象
|
|
410
581
|
* @param message - 事件消息对象,包含type、payload和meta
|
|
582
|
+
* @param args - 监听器参数
|
|
583
|
+
* @param catchErrors - 是否捕获并处理执行过程中的错误
|
|
411
584
|
* @returns 监听器的执行结果或错误对象(如果配置了ignoreErrors)
|
|
412
585
|
* @private
|
|
413
586
|
*
|
|
@@ -424,23 +597,18 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
424
597
|
private _executeListener;
|
|
425
598
|
private _getListenerExecutor;
|
|
426
599
|
/**
|
|
427
|
-
*
|
|
428
|
-
* @param
|
|
429
|
-
* @param
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
* 执行监听器节点中的所有监听函数
|
|
434
|
-
* @param node - FastListenerNode类型的监听器节点
|
|
435
|
-
* @param payload - 事件携带的数据
|
|
436
|
-
* @param type - 事件类型
|
|
437
|
-
* @returns 返回所有监听函数的执行结果数组
|
|
600
|
+
* 执行监听器节点列表中的所有监听器函数
|
|
601
|
+
* @param nodes 监听器节点列表
|
|
602
|
+
* @param message 事件消息对象
|
|
603
|
+
* @param args 监听器参数
|
|
604
|
+
* @param args 监听器参数
|
|
605
|
+
* @returns 所有监听器函数的执行结果数组
|
|
438
606
|
* @private
|
|
439
607
|
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
608
|
+
* 执行流程:
|
|
609
|
+
* 1. 将所有节点中的监听器函数展平为一维数组
|
|
610
|
+
* 2. 通过执行器执行所有监听器函数
|
|
611
|
+
* 3. 更新监听器的执行次数,并移除达到执行次数限制的监听器
|
|
444
612
|
*/
|
|
445
613
|
private _executeListeners;
|
|
446
614
|
/**
|
|
@@ -495,8 +663,14 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
495
663
|
* payload: { userId: 123 },
|
|
496
664
|
* meta: { time: Date.now() }
|
|
497
665
|
* }, true);
|
|
666
|
+
*
|
|
667
|
+
* // 清除保留事件
|
|
668
|
+
* emitter.emit("user/login")
|
|
669
|
+
*
|
|
498
670
|
* ```
|
|
499
671
|
*/
|
|
672
|
+
emit<T extends Types = Types>(type: T, directive: symbol): any[];
|
|
673
|
+
emit(type: string, directive: symbol): any[];
|
|
500
674
|
emit<R = any, T extends Types = Types>(type: T, payload?: AllEvents[T], retain?: boolean): R[];
|
|
501
675
|
emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? AllEvents[Types] : any, retain?: boolean): R[];
|
|
502
676
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
@@ -631,90 +805,27 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
631
805
|
* userEvents.offAll(); // 清理 'user' 前缀下的所有事件
|
|
632
806
|
* ```
|
|
633
807
|
*/
|
|
634
|
-
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: FastEventScopeOptions<M, C
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
interface FastEventListenerDecorators {
|
|
638
|
-
queue: string;
|
|
639
|
-
}
|
|
640
|
-
type FastQueuePriority = 'none';
|
|
641
|
-
type FastQueueOverflows = "drop" | "expand" | 'slide' | 'throw';
|
|
642
|
-
type QueueListenerPipeOptions = {
|
|
643
|
-
size?: number;
|
|
644
|
-
maxExpandSize?: number;
|
|
645
|
-
expandOverflow?: Omit<FastQueueOverflows, 'expand'>;
|
|
646
|
-
overflow?: FastQueueOverflows;
|
|
647
|
-
lifetime?: number;
|
|
648
|
-
onEnter?: (newMessage: FastEventMessage, messages: [FastEventMessage, number][]) => void;
|
|
649
|
-
onDrop?: (message: FastEventMessage) => void;
|
|
650
|
-
};
|
|
651
|
-
declare const queue: (options?: QueueListenerPipeOptions) => FastListenerPipe;
|
|
652
|
-
declare const dropping: (size?: number) => FastListenerPipe;
|
|
653
|
-
declare const sliding: (size?: number) => FastListenerPipe;
|
|
654
|
-
declare const expanding: (options?: Omit<QueueListenerPipeOptions, "overflow">) => FastListenerPipe;
|
|
655
|
-
|
|
656
|
-
interface RetryListenerPipeOptions {
|
|
657
|
-
interval?: number;
|
|
658
|
-
drop?: (message: FastEventMessage, error: Error) => void;
|
|
659
|
-
}
|
|
660
|
-
/**
|
|
661
|
-
* 创建一个重试管道,在监听器执行失败时自动重试
|
|
662
|
-
* @param count 最大重试次数
|
|
663
|
-
* @param options 配置选项
|
|
664
|
-
* @returns FastListenerPipe
|
|
665
|
-
*/
|
|
666
|
-
declare const retry: (count: number, options?: RetryListenerPipeOptions) => FastListenerPipe;
|
|
667
|
-
|
|
668
|
-
/**
|
|
669
|
-
* 创建一个超时装饰器,限制监听器函数的执行时间
|
|
670
|
-
* @param ms 超时时间(毫秒)
|
|
671
|
-
* @param defaultValue 可选的默认返回值,如果提供则超时时返回此值,否则抛出TimeoutError
|
|
672
|
-
* @returns 装饰器函数
|
|
673
|
-
*/
|
|
674
|
-
declare const timeout: <T = any>(ms: number, defaultValue?: T) => FastListenerPipe;
|
|
675
|
-
|
|
676
|
-
interface DebounceOptions {
|
|
677
|
-
/**
|
|
678
|
-
* 当消息被丢弃时的回调函数
|
|
679
|
-
*/
|
|
680
|
-
drop?: (message: FastEventMessage) => void;
|
|
808
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>;
|
|
809
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeObject extends FastEventScope<any, any, any> = FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): ScopeObject;
|
|
681
810
|
}
|
|
682
|
-
/**
|
|
683
|
-
* 创建一个防抖动装饰器,限制监听器函数的执行频率
|
|
684
|
-
* @param ms 防抖动时间(毫秒)
|
|
685
|
-
* @param options 可选的配置项
|
|
686
|
-
* @returns 装饰器函数
|
|
687
|
-
*/
|
|
688
|
-
declare const debounce: (ms: number, options?: DebounceOptions) => FastListenerPipe;
|
|
689
|
-
|
|
690
|
-
interface ThrottleOptions {
|
|
691
|
-
/**
|
|
692
|
-
* 当消息被丢弃时的回调函数
|
|
693
|
-
*/
|
|
694
|
-
drop?: (message: FastEventMessage) => void;
|
|
695
|
-
}
|
|
696
|
-
/**
|
|
697
|
-
* 创建一个节流装饰器,限制监听器函数的执行频率
|
|
698
|
-
* @param interval 节流时间间隔(毫秒)
|
|
699
|
-
* @param options 可选的配置项
|
|
700
|
-
* @returns 装饰器函数
|
|
701
|
-
*/
|
|
702
|
-
declare const throttle: (interval: number, options?: ThrottleOptions) => FastListenerPipe;
|
|
703
|
-
|
|
704
|
-
/**
|
|
705
|
-
* 创建一个memorize pipe,用于缓存上一次的返回值
|
|
706
|
-
* @param predicate 可选的判断函数,用于决定是否使用缓存
|
|
707
|
-
* @returns FastListenerPipe
|
|
708
|
-
*/
|
|
709
|
-
declare function memorize(predicate?: (message: FastEventMessage, args: FastEventListenerArgs) => boolean | Promise<boolean>): FastListenerPipe;
|
|
710
811
|
|
|
812
|
+
declare const __FastEvent__: unique symbol;
|
|
813
|
+
declare const __FastEventScope__: unique symbol;
|
|
711
814
|
declare class FastEventError extends Error {
|
|
815
|
+
constructor(message?: string);
|
|
712
816
|
}
|
|
713
817
|
declare class TimeoutError extends FastEventError {
|
|
714
818
|
}
|
|
819
|
+
declare class UnboundError extends FastEventError {
|
|
820
|
+
}
|
|
715
821
|
declare class AbortError extends FastEventError {
|
|
716
822
|
}
|
|
823
|
+
declare class CancelError extends FastEventError {
|
|
824
|
+
}
|
|
717
825
|
declare class QueueOverflowError extends FastEventError {
|
|
718
826
|
}
|
|
827
|
+
declare const FastEventDirectives: {
|
|
828
|
+
clearRetain: symbol;
|
|
829
|
+
};
|
|
719
830
|
|
|
720
|
-
export { AbortError, type
|
|
831
|
+
export { AbortError, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, type FaseEventMessageExtends, FastEvent, type FastEventAnyListener, FastEventDirectives, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventMessage, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerMeta, type FastListenerNode, type FastListeners, type Merge, type ObjectKeys, type OptionalItems, type Overloads, type OverrideOptions, type PickScopeEvents, QueueOverflowError, type RequiredItems, type ScopeEvents, TimeoutError, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
|