-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventEmitterWithHolding.js
165 lines (148 loc) · 4.91 KB
/
EventEmitterWithHolding.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
162
163
164
165
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule EventEmitterWithHolding
* @flow
*/
'use strict';
import type EmitterSubscription from 'EmitterSubscription';
import type EventEmitter from 'EventEmitter';
import type EventHolder from 'EventHolder';
/**
* @class EventEmitterWithHolding
* @description
* An EventEmitterWithHolding decorates an event emitter and enables one to
* "hold" or cache events and then have a handler register later to actually
* handle them.
*
* This is separated into its own decorator so that only those who want to use
* the holding functionality have to and others can just use an emitter. Since
* it implements the emitter interface it can also be combined with anything
* that uses an emitter.
*/
class EventEmitterWithHolding {
_emitter: EventEmitter;
_eventHolder: EventHolder;
_currentEventToken: ?Object;
_emittingHeldEvents: boolean;
/**
* @constructor
* @param {object} emitter - The object responsible for emitting the actual
* events.
* @param {object} holder - The event holder that is responsible for holding
* and then emitting held events.
*/
constructor(emitter: EventEmitter, holder: EventHolder) {
this._emitter = emitter;
this._eventHolder = holder;
this._currentEventToken = null;
this._emittingHeldEvents = false;
}
/**
* @see EventEmitter#addListener
*/
addListener(eventType: string, listener: Function, context: ?Object) {
return this._emitter.addListener(eventType, listener, context);
}
/**
* @see EventEmitter#once
*/
once(eventType: string, listener: Function, context: ?Object) {
return this._emitter.once(eventType, listener, context);
}
/**
* Adds a listener to be invoked when events of the specified type are
* emitted. An optional calling context may be provided. The data arguments
* emitted will be passed to the listener function. In addition to subscribing
* to all subsequent events, this method will also handle any events that have
* already been emitted, held, and not released.
*
* @param {string} eventType - Name of the event to listen to
* @param {function} listener - Function to invoke when the specified event is
* emitted
* @param {*} context - Optional context object to use when invoking the
* listener
*
* @example
* emitter.emitAndHold('someEvent', 'abc');
*
* emitter.addRetroactiveListener('someEvent', function(message) {
* console.log(message);
* }); // logs 'abc'
*/
addRetroactiveListener(
eventType: string, listener: Function, context: ?Object): EmitterSubscription {
const subscription = this._emitter.addListener(eventType, listener, context);
this._emittingHeldEvents = true;
this._eventHolder.emitToListener(eventType, listener, context);
this._emittingHeldEvents = false;
return subscription;
}
/**
* @see EventEmitter#removeAllListeners
*/
removeAllListeners(eventType: string) {
this._emitter.removeAllListeners(eventType);
}
/**
* @see EventEmitter#removeCurrentListener
*/
removeCurrentListener() {
this._emitter.removeCurrentListener();
}
/**
* @see EventEmitter#listeners
*/
listeners(eventType: string) /* TODO: Annotate return type here */ {
return this._emitter.listeners(eventType);
}
/**
* @see EventEmitter#emit
*/
emit(eventType: string, ...args: any) {
this._emitter.emit(eventType, ...args);
}
/**
* Emits an event of the given type with the given data, and holds that event
* in order to be able to dispatch it to a later subscriber when they say they
* want to handle held events.
*
* @param {string} eventType - Name of the event to emit
* @param {...*} Arbitrary arguments to be passed to each registered listener
*
* @example
* emitter.emitAndHold('someEvent', 'abc');
*
* emitter.addRetroactiveListener('someEvent', function(message) {
* console.log(message);
* }); // logs 'abc'
*/
emitAndHold(eventType: string, ...args: any) {
this._currentEventToken = this._eventHolder.holdEvent(eventType, ...args);
this._emitter.emit(eventType, ...args);
this._currentEventToken = null;
}
/**
* @see EventHolder#releaseCurrentEvent
*/
releaseCurrentEvent() {
if (this._currentEventToken) {
this._eventHolder.releaseEvent(this._currentEventToken);
} else if (this._emittingHeldEvents) {
this._eventHolder.releaseCurrentEvent();
}
}
/**
* @see EventHolder#releaseEventType
* @param {string} eventType
*/
releaseHeldEventType(eventType: string) {
this._eventHolder.releaseEventType(eventType);
}
}
module.exports = EventEmitterWithHolding;