| 203 | /** notification with an event name, an event string <TInput>, and some data<TData> */ |
| 204 | protected newNotification<TText extends string, TData>(eventName: string, options?: EventOptions): (text: TText, data: TData) => void; |
| 205 | protected newNotification<TText extends string, TData>(eventName: string, options?: EventOptions): (text: TText, data: TData) => void { |
| 206 | eventName = smash(eventName); |
| 207 | this.#knownEvents.add(eventName); |
| 208 | const descriptors = options?.descriptors ? new Descriptors(this.descriptors, options.descriptors) : this.descriptors; |
| 209 | // is it an immediate event? |
| 210 | if (options?.now) { |
| 211 | return (input?: TText, data?: TData): void => { |
| 212 | switch (options.once) { |
| 213 | case false: |
| 214 | // already triggered this one time event. |
| 215 | return; |
| 216 | |
| 217 | case true: |
| 218 | options.once = false; |
| 219 | } |
| 220 | // trigger the event |
| 221 | return (data !== undefined) ? |
| 222 | notifyNow(eventName, descriptors, this, input || '', data as any) : // text and data |
| 223 | notifyNow(eventName, descriptors, this, input || '', input as any); // text or data (or neither) |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | // otherwise queue it |
| 228 | |
| 229 | return (input?: TText, data?: TData): void => { |
| 230 | switch (options?.once) { |
| 231 | case false: |
| 232 | // already triggered this one time event. |
| 233 | return; |
| 234 | |
| 235 | case true: |
| 236 | options.once = false; |
| 237 | } |
| 238 | // trigger the event |
| 239 | return (data !== undefined) ? |
| 240 | notify(eventName, descriptors, this, input || '', data as any) : // text and data |
| 241 | notify(eventName, descriptors, this, input || '', input as any); // text or data (or neither) |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | /** subscribe to events (assumes 'this' modifier, and handler is filtered to the instance) */ |
| 246 | on(eventExpression: string, callback: Callback) { |