Rubén Norte d5e9e55fa3 Remove @providesModule from all modules
Summary:
This PR removes the need for having the `providesModule` tags in all the modules in the repository.

It configures Flow, Jest and Metro to get the module names from the filenames (`Libraries/Animated/src/nodes/AnimatedInterpolation.js` => `AnimatedInterpolation`)

* Checked the Flow configuration by running flow on the project root (no errors):

```
yarn flow
```

* Checked the Jest configuration by running the tests with a clean cache:

```
yarn jest --clearCache && yarn test
```

* Checked the Metro configuration by starting the server with a clean cache and requesting some bundles:

```
yarn run start --reset-cache
curl 'localhost:8081/IntegrationTests/AccessibilityManagerTest.bundle?platform=android'
curl 'localhost:8081/Libraries/Alert/Alert.bundle?platform=ios'
```

[INTERNAL] [FEATURE] [All] - Removed providesModule from all modules and configured tools.
Closes https://github.com/facebook/react-native/pull/18995

Reviewed By: mjesun

Differential Revision: D7729509

Pulled By: rubennorte

fbshipit-source-id: 892f760a05ce1fddb088ff0cd2e97e521fb8e825
2018-04-25 07:37:10 -07:00

119 lines
3.1 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @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;