/
AEventDispatcher.ts
137 lines (121 loc) · 3.94 KB
/
AEventDispatcher.ts
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
import {dispatch, Dispatch} from 'd3-dispatch';
/** @internal */
export function suffix(suffix: string, ...prefix: string[]) {
return prefix.map((p) => `${p}${suffix}`);
}
export interface IEventContext {
/**
* who is sending this event
*/
readonly source: AEventDispatcher;
readonly origin: AEventDispatcher;
/**
* the event type
*/
readonly type: string;
/**
* in case of multi propagation the 'main' event type
*/
readonly primaryType: string;
/**
* the arguments to the listener
*/
readonly args: any[];
}
export interface IEventListener {
(this: IEventContext, ...args: any[]): any;
}
export interface IEventHandler {
on(type: string | string[], listener: IEventListener | null): this;
}
declare const __DEBUG__: boolean;
/**
* base class for event dispatching using d3 event mechanism
*/
export default class AEventDispatcher implements IEventHandler {
private readonly listeners: Dispatch<any>;
private readonly listenerEvents: Set<string>;
private readonly forwarder: (...args: any[]) => void;
constructor() {
const events = this.createEventList();
this.listenerEvents = new Set(events);
this.listeners = dispatch(...events);
const that = this;
this.forwarder = function (this: IEventContext, ...args: any[]) {
that.fireImpl(this.type, this.primaryType, this.origin, ...args);
};
}
on(type: string | string[], listener: IEventListener | null): this {
if (Array.isArray(type)) {
type.forEach((d) => {
if (this.listenerEvents.has(d.split('.')[0])) {
this.listenersChanged(d, Boolean(listener!));
this.listeners.on(d, listener!);
} else if (__DEBUG__ && !d.includes('.')) {
console.warn(this, 'invalid event type', d);
}
});
} else if (this.listenerEvents.has((<string>type).split('.')[0])) {
this.listenersChanged(<string>type, Boolean(listener!));
this.listeners.on(<string>type, listener!);
} else if (__DEBUG__ && !type.includes('.')) {
console.warn(this, 'invalid event type', type);
}
return this;
}
protected listenersChanged(_type: string, _active: boolean) {
// hook
}
/**
* return the list of events to be able to dispatch
* @return {Array} by default no events
*/
protected createEventList(): string[] {
return [];
}
protected fire(type: string | string[], ...args: any[]) {
const primaryType = Array.isArray(type) ? type[0] : type;
this.fireImpl(type, primaryType, this, ...args);
}
private fireImpl(type: string | string[], primaryType: string, origin: AEventDispatcher, ...args: any[]) {
const fireImpl = (t: string) => {
if (!this.listenerEvents.has(t)) {
if (__DEBUG__) {
console.warn(this, 'invalid event type', t);
}
return;
}
//local context per event, set a this argument
const context: IEventContext = {
source: this, //who is sending this event,
origin,
type: t, //the event type
primaryType, //in case of multi propagation the 'main' event type
args //the arguments to the listener
};
this.listeners.apply(t, context, args);
};
if (Array.isArray(type)) {
type.forEach(fireImpl.bind(this));
} else {
fireImpl(<string>type);
}
}
/**
* forwards one or more events from a given dispatcher to the current one
* i.e. when one of the given events is fired in 'from' it will be forwarded to all my listeners
* @param {IEventHandler} from the event dispatcher to forward from
* @param {string[]} types the event types to forward
*/
protected forward(from: IEventHandler, ...types: string[]) {
from.on(types, this.forwarder);
}
/**
* removes the forwarding declarations
* @param {IEventHandler} from the originated dispatcher
* @param {string[]} types event types to forward
*/
protected unforward(from: IEventHandler, ...types: string[]) {
from.on(types, null);
}
}