UNPKG

2.76 kBTypeScriptView Raw
1// Last module patch version validated against: 3.0.1
2
3export interface Dispatch<T extends object> {
4 /**
5 * Like `function.apply`, invokes each registered callback for the specified type,
6 * passing the callback the specified arguments, with `that` as the `this` context.
7 *
8 * @param type A specified event type.
9 * @param that The `this` context for the callback.
10 * @param args Additional arguments to be passed to the callback.
11 * @throws "unknown type" on unknown event type.
12 */
13 apply(type: string, that?: T, args?: any[]): void;
14
15 /**
16 * Like `function.call`, invokes each registered callback for the specified type,
17 * passing the callback the specified arguments, with `that` as the `this` context.
18 * See dispatch.apply for more information.
19 *
20 * @param type A specified event type.
21 * @param that The `this` context for the callback.
22 * @param args Additional arguments to be passed to the callback.
23 * @throws "unknown type" on unknown event type.
24 */
25 call(type: string, that?: T, ...args: any[]): void;
26
27 /**
28 * Returns a copy of this dispatch object.
29 * Changes to this dispatch do not affect the returned copy and vice versa.
30 */
31 copy(): Dispatch<T>;
32
33 /**
34 * Returns the callback for the specified typenames, if any.
35 * If multiple typenames are specified, the first matching callback is returned.
36 */
37 on(typenames: string): ((this: T, ...args: any[]) => void) | undefined;
38 /**
39 * Adds or removes the callback for the specified typenames.
40 * If a callback function is specified, it is registered for the specified (fully-qualified) typenames.
41 * If a callback was already registered for the given typenames, the existing callback is removed before the new callback is added.
42 * The specified typenames is a string, such as start or end.foo.
43 * The type may be optionally followed by a period (.) and a name; the optional name allows multiple callbacks to be registered to receive events of the same type, such as start.foo and start.bar.
44 * To specify multiple typenames, separate typenames with spaces, such as start end or start.foo start.bar.
45 * To remove all callbacks for a given name foo, say dispatch.on(".foo", null).
46 */
47 on(typenames: string, callback: null | ((this: T, ...args: any[]) => void)): this;
48}
49
50/**
51 * Creates a new dispatch for the specified event types. Each type is a string, such as "start" or "end".
52 *
53 * @param types The event types.
54 * @throws "illegal type" on empty string or duplicated event types.
55 */
56// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
57export function dispatch<T extends object>(...types: string[]): Dispatch<T>;
58
\No newline at end of file