-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathobservable.js
161 lines (148 loc) · 3.59 KB
/
observable.js
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
/**
* Observable class prototype.
*
* @module observable
*/
import * as map from './map.js'
import * as set from './set.js'
import * as array from './array.js'
/**
* Handles named events.
* @experimental
*
* This is basically a (better typed) duplicate of Observable, which will replace Observable in the
* next release.
*
* @template {{[key in keyof EVENTS]: function(...any):void}} EVENTS
*/
export class ObservableV2 {
constructor () {
/**
* Some desc.
* @type {Map<string, Set<any>>}
*/
this._observers = map.create()
}
/**
* @template {keyof EVENTS & string} NAME
* @param {NAME} name
* @param {EVENTS[NAME]} f
*/
on (name, f) {
map.setIfUndefined(this._observers, /** @type {string} */ (name), set.create).add(f)
return f
}
/**
* @template {keyof EVENTS & string} NAME
* @param {NAME} name
* @param {EVENTS[NAME]} f
*/
once (name, f) {
/**
* @param {...any} args
*/
const _f = (...args) => {
this.off(name, /** @type {any} */ (_f))
f(...args)
}
this.on(name, /** @type {any} */ (_f))
}
/**
* @template {keyof EVENTS & string} NAME
* @param {NAME} name
* @param {EVENTS[NAME]} f
*/
off (name, f) {
const observers = this._observers.get(name)
if (observers !== undefined) {
observers.delete(f)
if (observers.size === 0) {
this._observers.delete(name)
}
}
}
/**
* Emit a named event. All registered event listeners that listen to the
* specified name will receive the event.
*
* @todo This should catch exceptions
*
* @template {keyof EVENTS & string} NAME
* @param {NAME} name The event name.
* @param {Parameters<EVENTS[NAME]>} args The arguments that are applied to the event listener.
*/
emit (name, args) {
// copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.
return array.from((this._observers.get(name) || map.create()).values()).forEach(f => f(...args))
}
destroy () {
this._observers = map.create()
}
}
/* c8 ignore start */
/**
* Handles named events.
*
* @deprecated
* @template N
*/
export class Observable {
constructor () {
/**
* Some desc.
* @type {Map<N, any>}
*/
this._observers = map.create()
}
/**
* @param {N} name
* @param {function} f
*/
on (name, f) {
map.setIfUndefined(this._observers, name, set.create).add(f)
}
/**
* @param {N} name
* @param {function} f
*/
once (name, f) {
/**
* @param {...any} args
*/
const _f = (...args) => {
this.off(name, _f)
f(...args)
}
this.on(name, _f)
}
/**
* @param {N} name
* @param {function} f
*/
off (name, f) {
const observers = this._observers.get(name)
if (observers !== undefined) {
observers.delete(f)
if (observers.size === 0) {
this._observers.delete(name)
}
}
}
/**
* Emit a named event. All registered event listeners that listen to the
* specified name will receive the event.
*
* @todo This should catch exceptions
*
* @param {N} name The event name.
* @param {Array<any>} args The arguments that are applied to the event listener.
*/
emit (name, args) {
// copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.
return array.from((this._observers.get(name) || map.create()).values()).forEach(f => f(...args))
}
destroy () {
this._observers = map.create()
}
}
/* c8 ignore end */