UNPKG

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