-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate Package for
@react-native/event-emitter
- Loading branch information
Showing
15 changed files
with
2,168 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-flow"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# @react-native/event-emitter | ||
|
||
`@react-native/event-emitter` is an event library used by React Native that is | ||
published as an independent package so it can also be used by applications. | ||
|
||
Consult the included type definitions for usage instructions. | ||
|
||
## License | ||
|
||
`idx` is [MIT licensed](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
*/ | ||
|
||
export interface EventSubscription { | ||
remove(): void; | ||
} | ||
|
||
export interface IEventEmitter<TEventToArgsMap extends {[eventType: string]: unknown[]}> { | ||
addListener<TEvent extends keyof TEventToArgsMap>( | ||
eventType: TEvent, | ||
listener: (...args: TEventToArgsMap[TEvent]) => void, | ||
context?: unknown, | ||
): EventSubscription; | ||
|
||
emit<TEvent extends keyof TEventToArgsMap>( | ||
eventType: TEvent, | ||
...args: TEventToArgsMap[TEvent] | ||
): void; | ||
|
||
removeAllListeners<TEvent extends keyof TEventToArgsMap>(eventType?: TEvent | null): void; | ||
|
||
listenerCount<TEvent extends keyof TEventToArgsMap>(eventType: TEvent): number; | ||
} | ||
|
||
/** | ||
* EventEmitter manages listeners and publishes events to them. | ||
* | ||
* EventEmitter accepts a single type parameter that defines the valid events | ||
* and associated listener argument(s). | ||
* | ||
* @example | ||
* | ||
* const emitter = new EventEmitter<{ | ||
* success: [number, string], | ||
* error: [Error], | ||
* }>(); | ||
* | ||
* emitter.on('success', (statusCode, responseText) => {...}); | ||
* emitter.emit('success', 200, '...'); | ||
* | ||
* emitter.on('error', error => {...}); | ||
* emitter.emit('error', new Error('Resource not found')); | ||
* | ||
*/ | ||
export default class EventEmitter<TEventToArgsMap extends {[eventType: string]: unknown[]}> implements IEventEmitter<TEventToArgsMap> { | ||
/** | ||
* Registers a listener that is called when the supplied event is emitted. | ||
* Returns a subscription that has a `remove` method to undo registration. | ||
*/ | ||
addListener<TEvent extends keyof TEventToArgsMap>( | ||
eventType: TEvent, | ||
listener: (...args: TEventToArgsMap[TEvent]) => void, | ||
context?: unknown, | ||
): EventSubscription; | ||
|
||
/** | ||
* Emits the supplied event. Additional arguments supplied to `emit` will be | ||
* passed through to each of the registered listeners. | ||
* | ||
* If a listener modifies the listeners registered for the same event, those | ||
* changes will not be reflected in the current invocation of `emit`. | ||
*/ | ||
emit<TEvent extends keyof TEventToArgsMap>( | ||
eventType: TEvent, | ||
...args: TEventToArgsMap[TEvent] | ||
): void; | ||
|
||
/** | ||
* Removes all registered listeners. | ||
*/ | ||
removeAllListeners<TEvent extends keyof TEventToArgsMap>(eventType?: TEvent | null): void; | ||
|
||
/** | ||
* Returns the number of registered listeners for the supplied event. | ||
*/ | ||
listenerCount<TEvent extends keyof TEventToArgsMap>(eventType: TEvent): number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* | ||
* @format | ||
*/ | ||
|
||
/** | ||
* EventEmitter manages listeners and publishes events to them. | ||
* | ||
* EventEmitter accepts a single type parameter that defines the valid events | ||
* and associated listener argument(s). | ||
* | ||
* @example | ||
* | ||
* const emitter = new EventEmitter<{ | ||
* success: [number, string], | ||
* error: [Error], | ||
* }>(); | ||
* | ||
* emitter.on('success', (statusCode, responseText) => {...}); | ||
* emitter.emit('success', 200, '...'); | ||
* | ||
* emitter.on('error', error => {...}); | ||
* emitter.emit('error', new Error('Resource not found')); | ||
* | ||
*/ | ||
export default class EventEmitter { | ||
_registry = {}; | ||
/** | ||
* Registers a listener that is called when the supplied event is emitted. | ||
* Returns a subscription that has a `remove` method to undo registration. | ||
*/ | ||
|
||
addListener(eventType, listener, context) { | ||
const registrations = allocate(this._registry, eventType); | ||
const registration = { | ||
context, | ||
listener, | ||
|
||
remove() { | ||
registrations.delete(registration); | ||
}, | ||
}; | ||
registrations.add(registration); | ||
return registration; | ||
} | ||
/** | ||
* Emits the supplied event. Additional arguments supplied to `emit` will be | ||
* passed through to each of the registered listeners. | ||
* | ||
* If a listener modifies the listeners registered for the same event, those | ||
* changes will not be reflected in the current invocation of `emit`. | ||
*/ | ||
|
||
emit(eventType, ...args) { | ||
const registrations = this._registry[eventType]; | ||
|
||
if (registrations != null) { | ||
for (const registration of [...registrations]) { | ||
registration.listener.apply(registration.context, args); | ||
} | ||
} | ||
} | ||
/** | ||
* Removes all registered listeners. | ||
*/ | ||
|
||
removeAllListeners(eventType) { | ||
if (eventType == null) { | ||
this._registry = {}; | ||
} else { | ||
delete this._registry[eventType]; | ||
} | ||
} | ||
/** | ||
* Returns the number of registered listeners for the supplied event. | ||
*/ | ||
|
||
listenerCount(eventType) { | ||
const registrations = this._registry[eventType]; | ||
return registrations == null ? 0 : registrations.size; | ||
} | ||
} | ||
|
||
function allocate(registry, eventType) { | ||
let registrations = registry[eventType]; | ||
|
||
if (registrations == null) { | ||
registrations = new Set(); | ||
registry[eventType] = registrations; | ||
} | ||
|
||
return registrations; | ||
} |
Oops, something went wrong.