Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 43x 57x 1x 1x 3x 3x 1x 56x 32x 56x 56x 56x 56x 56x 5x 5x 5x 5x 33x 32x 31x 31x 31x 1x 1x 2x 2x 1x 88x 88x 88x 2x 4x 88x 3x 88x 88x 86x 2x 2x 2x 3x 1x | import { castArray } from 'lodash-uni' export type EventBusListenerTag = string | number export type EventBusListener< TCallback extends (...args: any[]) => any = (...args: any[]) => any, > = TCallback & { __EVENT_BUS_TAG__?: EventBusListenerTag } export type EventBusOffListener = () => any export type EventBusListeners = Record<string, EventBusListener> export interface EventBusListenerDescriptor< TListenerName extends keyof EventBusListeners, > { name: TListenerName context?: any tag?: EventBusListenerTag filter?: ( callback: EventBusListener, index: number, callbacks: EventBusListener[], ) => boolean } export type EventBusBeforeOn<TListeners extends EventBusListeners> = { [TListenerName in keyof TListeners]?: ( this: EventBus<TListeners>, callback: EventBusListener<TListeners[TListenerName]>, ) => TListeners[TListenerName] } export type EventBusBeforeEmit<TListeners extends EventBusListeners> = { [TListenerName in keyof TListeners]?: ( this: EventBus<TListeners>, context: any, ) => any } export interface EventBusOptions<TListeners extends EventBusListeners> { beforeOn?: EventBusBeforeOn<TListeners> beforeEmit?: EventBusBeforeEmit<TListeners> } /** * 事件巴士,管理事件的发布与订阅。 * * @template TListeners 事件名称及其对应的回调描述 * @example * ```typescript * const bus = new EventBus<{ * success: (payload: { message: string }) => any * }>() * bus.on('success', ({ message }) => console.log(message)) * bus.emit('success', { message: '提交成功' }) * // => 控制台输出: 提交成功 * ``` */ export class EventBus<TListeners extends EventBusListeners> { /** * 构造函数。 */ constructor(private options?: EventBusOptions<TListeners>) {} /** * 回调列表。 */ private callbacks: { [TListenerName in keyof TListeners]: Array<TListeners[TListenerName]> } = Object.create(null) /** * 订阅多个事件。 * * @param eventNames 事件名称 * @param callback 事件触发回调 * @returns 返回取消订阅的函数 */ on<TListenerName extends keyof TListeners>( eventNames: TListenerName[], callback: ( firstPayload: { [K in TListenerName]: Parameters<TListeners[K]>[0] }, payload: { [K in TListenerName]: Parameters<TListeners[K]> }, ) => any, ): EventBusOffListener /** * 订阅单个事件。 * * @param eventName 事件名称 * @param callback 事件触发回调 * @returns 返回取消订阅的函数 */ on<TListenerName extends keyof TListeners>( eventName: TListenerName, callback: TListeners[TListenerName], ): EventBusOffListener on<TListenerName extends keyof TListeners>( eventNames: TListenerName | TListenerName[], callback: any, ): EventBusOffListener { if (Array.isArray(eventNames)) { const offs: EventBusOffListener[] = [] for (const eventName of eventNames) { offs.push( // @ts-ignore this.on(eventName, (...args: any[]) => callback( { [eventName]: args[0] }, { [eventName]: args, }, ), ), ) } return () => offs.forEach(off => off()) } if (!this.callbacks[eventNames]) { this.callbacks[eventNames] = [] } callback = this.options?.beforeOn?.[eventNames]?.call(this, callback) ?? callback const index = this.callbacks[eventNames].indexOf(callback) if (index === -1) { this.callbacks[eventNames].push(callback) } return () => this.off(eventNames, callback) } /** * 订阅事件,但只订阅一次即取消订阅。 * * @param eventName 事件名称 * @param callback 事件触发回调 * @returns 返回取消订阅的函数 */ once<TListenerName extends keyof TListeners>( eventName: TListenerName, callback: TListeners[TListenerName], ): EventBusOffListener { const off = this.on(eventName, ((...args) => { off() callback(...args) }) as TListeners[TListenerName]) return off } /** * 取消订阅事件,若没有指定回调,则取消所有回调。 * * @param eventName 事件名称 * @param callback 事件触发回调 */ off<TListenerName extends keyof TListeners>( eventName: TListenerName, callbackOrTag?: TListeners[TListenerName] | string | number, ): void { if (this.callbacks[eventName] && callbackOrTag) { if (typeof callbackOrTag === 'function') { const index = this.callbacks[eventName].indexOf(callbackOrTag) if (index > -1) { this.callbacks[eventName].splice(index, 1) } } else { let index = this.callbacks[eventName].length while (index--) { if ( this.callbacks[eventName][index].__EVENT_BUS_TAG__ != null && this.callbacks[eventName][index].__EVENT_BUS_TAG__ === callbackOrTag ) { this.callbacks[eventName].splice(index, 1) } } } } else { delete this.callbacks[eventName] } } /** * 发布事件。 * * @param eventNameAndContext 事件名称和上下文 * @param args 传给事件回调的参数 * @returns 返回各事件回调的返回结果组成的数组 */ emit<TListenerName extends keyof TListeners>( // @ts-ignore eventName: TListenerName | EventBusListenerDescriptor<TListenerName>, ...args: Parameters<TListeners[TListenerName]> ): Array<ReturnType<TListeners[TListenerName]>> { const { name, context, tag, filter, }: { name: TListenerName context?: any tag?: string | number filter?: ( callback: EventBusListener, index: number, callbacks: EventBusListener[], ) => boolean } = typeof eventName === 'object' ? eventName : { name: eventName, } let callbacks = (this.callbacks[name] || []).slice() if (tag != null) { callbacks = callbacks.filter( callback => callback.__EVENT_BUS_TAG__ != null && tag === callback.__EVENT_BUS_TAG__, ) } if (typeof filter === 'function') { callbacks = callbacks.filter(filter) } this.options?.beforeEmit?.[name]?.call(this, context) return callbacks.map(callback => { return callback.call(context, ...args) }) } /** * 局部执行。 * * @param fn 执行函数 * @returns 可以返回一个或多个取消订阅的函数 */ run( fn: (bus: this) => void | EventBusOffListener | EventBusOffListener[], ): EventBusOffListener { const res = fn(this) return () => { if (res) { castArray(res).forEach(off => off()) } } } /** * 清空事件订阅。 */ clear() { this.callbacks = Object.create(null) } /** * 销毁。 */ destroy() { // @ts-ignore this.callbacks = null } } |