@@ -101,6 +101,27 @@ class Emitter
101101
102102 new Disposable (@off .bind (this , eventName, handler))
103103
104+ # Public: Register the given handler function to be invoked only once whenever
105+ # the next by the given name are emitted via {::emit}. After being invoked,
106+ # will automatically be unsubscribed.
107+ #
108+ # * `eventName` {String} naming the event that you want to invoke the handler
109+ # when emitted.
110+ # * `handler` {Function} to invoke when {::emit} is called with the given
111+ # event name.
112+ #
113+ # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
114+ once : (eventName , handler , unshift = false ) ->
115+ if typeof handler isnt ' function'
116+ throw new Error (" Handler must be a function" )
117+
118+ subscription = null
119+ wrappedHandler = (value ) ->
120+ handler (value)
121+ subscription .dispose ()
122+
123+ subscription = @ on (eventName, wrappedHandler, unshift)
124+
104125 # Public: Register the given handler function to be invoked *before* all
105126 # other handlers existing at the time of subscription whenever events by the
106127 # given name are emitted via {::emit}.
@@ -121,6 +142,27 @@ class Emitter
121142 preempt : (eventName , handler ) ->
122143 @ on (eventName, handler, true )
123144
145+ # Public: Register the given handler function to be invoked *before* all
146+ # other handlers existing at the time of subscription only once the next time
147+ # events by the given name are emitted via {::emit}. After being invoked, it
148+ # will automatically be unsubscribed.
149+ #
150+ # Use this method when you need to be the first to handle a given event. This
151+ # could be required when a data structure in a parent object needs to be
152+ # updated before third-party event handlers registered on a child object via a
153+ # public API are invoked. Your handler could itself be preempted via
154+ # subsequent calls to this method, but this can be controlled by keeping
155+ # methods based on `::preempt` private.
156+ #
157+ # * `eventName` {String} naming the event that you want to invoke the handler
158+ # when emitted.
159+ # * `handler` {Function} to invoke when {::emit} is called with the given
160+ # event name.
161+ #
162+ # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
163+ preemptOnce : (eventName , handler ) ->
164+ @ once (eventName, handler, true )
165+
124166 # Private: Used by the disposable.
125167 off : (eventName , handlerToRemove ) ->
126168 return if @disposed
0 commit comments