This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathemitter.coffee
More file actions
185 lines (158 loc) · 5.61 KB
/
emitter.coffee
File metadata and controls
185 lines (158 loc) · 5.61 KB
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
Disposable = require './disposable'
# Essential: Utility class to be used when implementing event-based APIs that
# allows for handlers registered via `::on` to be invoked with calls to
# `::emit`. Instances of this class are intended to be used internally by
# classes that expose an event-based API.
#
# For example:
#
# ```coffee
# class User
# constructor: ->
# @emitter = new Emitter
#
# onDidChangeName: (callback) ->
# @emitter.on 'did-change-name', callback
#
# setName: (name) ->
# if name isnt @name
# @name = name
# @emitter.emit 'did-change-name', name
# @name
# ```
module.exports =
class Emitter
@exceptionHandlers: []
@onEventHandlerException: (exceptionHandler) ->
if @exceptionHandlers.length is 0
@dispatch = @exceptionHandlingDispatch
@exceptionHandlers.push(exceptionHandler)
new Disposable =>
@exceptionHandlers.splice(@exceptionHandlers.indexOf(exceptionHandler), 1)
if @exceptionHandlers.length is 0
@dispatch = @simpleDispatch
@simpleDispatch: (handler, value) ->
handler(value)
@exceptionHandlingDispatch: (handler, value) ->
try
handler(value)
catch exception
for exceptionHandler in @exceptionHandlers
exceptionHandler(exception)
@dispatch: @simpleDispatch
disposed: false
###
Section: Construction and Destruction
###
# Public: Construct an emitter.
#
# ```coffee
# @emitter = new Emitter()
# ```
constructor: ->
@clear()
# Public: Clear out any existing subscribers.
clear: ->
@handlersByEventName = {}
# Public: Unsubscribe all handlers.
dispose: ->
@handlersByEventName = null
@disposed = true
###
Section: Event Subscription
###
# Public: Register the given handler function to be invoked whenever events by
# the given name are emitted via {::emit}.
#
# * `eventName` {String} naming the event that you want to invoke the handler
# when emitted.
# * `handler` {Function} to invoke when {::emit} is called with the given
# event name.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
on: (eventName, handler, unshift=false) ->
if @disposed
throw new Error("Emitter has been disposed")
if typeof handler isnt 'function'
throw new Error("Handler must be a function")
if currentHandlers = @handlersByEventName[eventName]
if unshift
@handlersByEventName[eventName] = [handler].concat(currentHandlers)
else
@handlersByEventName[eventName] = currentHandlers.concat(handler)
else
@handlersByEventName[eventName] = [handler]
new Disposable(@off.bind(this, eventName, handler))
# Public: Register the given handler function to be invoked the next time an
# events with the given name is emitted via {::emit}.
#
# * `eventName` {String} naming the event that you want to invoke the handler
# when emitted.
# * `handler` {Function} to invoke when {::emit} is called with the given
# event name.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
once: (eventName, handler, unshift=false) ->
wrapped = (value) ->
disposable.dispose()
handler(value)
disposable = @on(eventName, wrapped, unshift)
# Public: Register the given handler function to be invoked *before* all
# other handlers existing at the time of subscription whenever events by the
# given name are emitted via {::emit}.
#
# Use this method when you need to be the first to handle a given event. This
# could be required when a data structure in a parent object needs to be
# updated before third-party event handlers registered on a child object via a
# public API are invoked. Your handler could itself be preempted via
# subsequent calls to this method, but this can be controlled by keeping
# methods based on `::preempt` private.
#
# * `eventName` {String} naming the event that you want to invoke the handler
# when emitted.
# * `handler` {Function} to invoke when {::emit} is called with the given
# event name.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
preempt: (eventName, handler) ->
@on(eventName, handler, true)
# Private: Used by the disposable.
off: (eventName, handlerToRemove) ->
return if @disposed
if oldHandlers = @handlersByEventName[eventName]
newHandlers = []
for handler in oldHandlers when handler isnt handlerToRemove
newHandlers.push(handler)
if newHandlers.length > 0
@handlersByEventName[eventName] = newHandlers
else
delete @handlersByEventName[eventName]
return
###
Section: Event Emission
###
# Public: Invoke handlers registered via {::on} for the given event name.
#
# * `eventName` The name of the event to emit. Handlers registered with {::on}
# for the same name will be invoked.
# * `value` Callbacks will be invoked with this value as an argument.
emit: (eventName, value) ->
if handlers = @handlersByEventName?[eventName]
for handler in handlers
@constructor.dispatch(handler, value)
return
emitAsync: (eventName, value) ->
if handlers = @handlersByEventName?[eventName]
promises = for handler in handlers
@constructor.dispatch(handler, value)
return Promise.all(promises).then -> return
return Promise.resolve()
getEventNames: ->
Object.keys(@handlersByEventName)
listenerCountForEventName: (eventName) ->
@handlersByEventName[eventName]?.length ? 0
getTotalListenerCount: ->
result = 0
for eventName in Object.keys(@handlersByEventName)
result += @handlersByEventName[eventName].length
result