2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2016-05-16 11:04:37 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2016-05-16 11:04:37 +00:00
|
|
|
* 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.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* @providesModule EventHolder
|
2016-05-16 11:04:37 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-16 11:04:37 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
class EventHolder {
|
2016-05-16 11:04:37 +00:00
|
|
|
|
|
|
|
_heldEvents: Object;
|
|
|
|
_currentEventKey: ?Object;
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
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'
|
|
|
|
*
|
|
|
|
*/
|
2016-05-16 11:04:37 +00:00
|
|
|
holdEvent(eventType: string, ...args: any) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this._heldEvents[eventType] = this._heldEvents[eventType] || [];
|
2016-05-16 11:04:37 +00:00
|
|
|
const eventsOfType = this._heldEvents[eventType];
|
|
|
|
const key = {
|
2015-01-30 01:10:49 +00:00
|
|
|
eventType: eventType,
|
|
|
|
index: eventsOfType.length
|
|
|
|
};
|
2016-05-16 11:04:37 +00:00
|
|
|
eventsOfType.push(args);
|
2015-01-30 01:10:49 +00:00
|
|
|
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
|
|
|
|
*/
|
2016-05-16 11:04:37 +00:00
|
|
|
emitToListener(eventType: ?string , listener: Function, context: ?Object) {
|
|
|
|
const eventsOfType = this._heldEvents[eventType];
|
2015-01-30 01:10:49 +00:00
|
|
|
if (!eventsOfType) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-16 11:04:37 +00:00
|
|
|
const origEventKey = this._currentEventKey;
|
2015-01-30 01:10:49 +00:00
|
|
|
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'
|
|
|
|
);
|
2016-05-16 11:04:37 +00:00
|
|
|
this._currentEventKey && this.releaseEvent(this._currentEventKey);
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-05-16 11:04:37 +00:00
|
|
|
releaseEventType(type: string) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this._heldEvents[type] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EventHolder;
|