/
events.ts
212 lines (183 loc) · 5.3 KB
/
events.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
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
/**
@module @ember/object
*/
import { Meta, meta as metaFor, peekMeta } from '@ember/-internals/meta';
import { setListeners } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
/*
The event system uses a series of nested hashes to store listeners on an
object. When a listener is registered, or when an event arrives, these
hashes are consulted to determine which target and action pair to invoke.
The hashes are stored in the object's meta hash, and look like this:
// Object's meta hash
{
listeners: { // variable name: `listenerSet`
"foo:change": [ // variable name: `actions`
target, method, once
]
}
}
*/
/**
Add an event listener
@method addListener
@static
@for @ember/object/events
@param obj
@param {String} eventName
@param {Object|Function} target A target object or a function
@param {Function|String} method A function or the name of a function to be called on `target`
@param {Boolean} once A flag whether a function should only be called once
@public
*/
export function addListener(
obj: object,
eventName: string,
target: object | Function | null,
method?: Function | string,
once?: boolean
): void {
assert(
'You must pass at least an object and event name to addListener',
Boolean(obj) && Boolean(eventName)
);
if (!method && 'function' === typeof target) {
method = target;
target = null;
}
metaFor(obj).addToListeners(eventName, target, method!, once === true);
}
/**
Remove an event listener
Arguments should match those passed to `addListener`.
@method removeListener
@static
@for @ember/object/events
@param obj
@param {String} eventName
@param {Object|Function} target A target object or a function
@param {Function|String} method A function or the name of a function to be called on `target`
@public
*/
export function removeListener(
obj: object,
eventName: string,
target: object | null,
method?: Function | string
): void {
assert(
'You must pass at least an object and event name to removeListener',
Boolean(obj) && Boolean(eventName)
);
if (!method && 'function' === typeof target) {
method = target;
target = null;
}
let m = metaFor(obj);
if (method === undefined) {
m.removeAllListeners(eventName);
} else {
m.removeFromListeners(eventName, target, method);
}
}
/**
Send an event. The execution of suspended listeners
is skipped, and once listeners are removed. A listener without
a target is executed on the passed object. If an array of actions
is not passed, the actions stored on the passed object are invoked.
@method sendEvent
@static
@for @ember/object/events
@param obj
@param {String} eventName
@param {Array} params Optional parameters for each listener.
@return {Boolean} if the event was delivered to one or more actions
@public
*/
export function sendEvent(
obj: object,
eventName: string,
params: any[],
actions?: any[],
_meta?: Meta | null
) {
if (actions === undefined) {
let meta = _meta === undefined ? peekMeta(obj) : _meta;
actions =
typeof meta === 'object' && meta !== null ? meta.matchingListeners(eventName) : undefined;
}
if (actions === undefined || actions.length === 0) {
return false;
}
for (let i = actions.length - 3; i >= 0; i -= 3) {
// looping in reverse for once listeners
let target = actions[i] as any | null;
let method = actions[i + 1] as string | Function;
let once = actions[i + 2] as boolean;
if (!method) {
continue;
}
if (once) {
removeListener(obj, eventName, target, method);
}
if (!target) {
target = obj;
}
if ('string' === typeof method) {
method = target[method] as Function;
}
method.apply(target, params);
}
return true;
}
/**
@private
@method hasListeners
@static
@for @ember/object/events
@param obj
@param {String} eventName
@return {Boolean} if `obj` has listeners for event `eventName`
*/
export function hasListeners(obj: object, eventName: string): boolean {
let meta = peekMeta(obj);
if (meta === null) {
return false;
}
let matched = meta.matchingListeners(eventName);
return matched !== undefined && matched.length > 0;
}
/**
Define a property as a function that should be executed when
a specified event or events are triggered.
``` javascript
import EmberObject from '@ember/object';
import { on } from '@ember/object/evented';
import { sendEvent } from '@ember/object/events';
let Job = EmberObject.extend({
logCompleted: on('completed', function() {
console.log('Job completed!');
})
});
let job = Job.create();
sendEvent(job, 'completed'); // Logs 'Job completed!'
```
@method on
@static
@for @ember/object/evented
@param {String} eventNames*
@param {Function} func
@return {Function} the listener function, passed as last argument to on(...)
@public
*/
export function on(...args: Array<string | Function>): Function {
let func = args.pop() as Function;
let events = args as string[];
assert('on expects function as last argument', typeof func === 'function');
assert(
'on called without valid event names',
events.length > 0 && events.every(p => typeof p === 'string' && p.length > 0)
);
setListeners(func, events);
return func;
}