EventHolder.js
3.29 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
/**
* 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 EventHolder
* @flow
*/
'use strict';
const invariant = require('fbjs/lib/invariant');
class EventHolder {
_heldEvents: Object;
_currentEventKey: ?Object;
constructor() {
this._heldEvents = {};
this._currentEventKey = null;
}
/**
* Holds a given event for processing later.
*
* TODO: Annotate return type better. The structural type of the return here
* is pretty obvious.
*
* @param {string} eventType - Name of the event to hold and later emit
* @param {...*} Arbitrary arguments to be passed to each registered listener
* @return {object} Token that can be used to release the held event
*
* @example
*
* holder.holdEvent({someEvent: 'abc'});
*
* holder.emitToHandler({
* someEvent: function(data, event) {
* console.log(data);
* }
* }); //logs 'abc'
*
*/
holdEvent(eventType: string, ...args: any) {
this._heldEvents[eventType] = this._heldEvents[eventType] || [];
const eventsOfType = this._heldEvents[eventType];
const key = {
eventType: eventType,
index: eventsOfType.length
};
eventsOfType.push(args);
return key;
}
/**
* Emits the held events of the specified type to the given listener.
*
* @param {?string} eventType - Optional name of the events to replay
* @param {function} listener - The listener to which to dispatch the event
* @param {?object} context - Optional context object to use when invoking
* the listener
*/
emitToListener(eventType: ?string , listener: Function, context: ?Object) {
const eventsOfType = this._heldEvents[eventType];
if (!eventsOfType) {
return;
}
const origEventKey = this._currentEventKey;
eventsOfType.forEach((/*?array*/ eventHeld, /*number*/ index) => {
if (!eventHeld) {
return;
}
this._currentEventKey = {
eventType: eventType,
index: index
};
listener.apply(context, eventHeld);
});
this._currentEventKey = origEventKey;
}
/**
* Provides an API that can be called during an eventing cycle to release
* the last event that was invoked, so that it is no longer "held".
*
* If it is called when not inside of an emitting cycle it will throw.
*
* @throws {Error} When called not during an eventing cycle
*/
releaseCurrentEvent() {
invariant(
this._currentEventKey !== null,
'Not in an emitting cycle; there is no current event'
);
this._currentEventKey && this.releaseEvent(this._currentEventKey);
}
/**
* Releases the event corresponding to the handle that was returned when the
* event was first held.
*
* @param {object} token - The token returned from holdEvent
*/
releaseEvent(token: Object) {
delete this._heldEvents[token.eventType][token.index];
}
/**
* Releases all events of a certain type.
*
* @param {string} type
*/
releaseEventType(type: string) {
this._heldEvents[type] = [];
}
}
module.exports = EventHolder;