-
Notifications
You must be signed in to change notification settings - Fork 47.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When dispatching an event, if a listener throws- the event emitter should still call other listeners before re-throwing. If we don't do this, certain stores (e.g. ProfilerStore) may miss events and be left in an unknown state.
- Loading branch information
Brian Vaughn
committed
Mar 24, 2020
1 parent
a600408
commit 7788cec
Showing
7 changed files
with
70 additions
and
21 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
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,65 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export default class EventEmitter<Events: Object> { | ||
listenersMap: Map<string, Set<Function>> = new Map(); | ||
|
||
addListener<Event: $Keys<Events>>( | ||
event: Event, | ||
listener: (...$ElementType<Events, Event>) => any, | ||
): void { | ||
let listeners = this.listenersMap.get(event); | ||
if (listeners === undefined) { | ||
listeners = new Set(); | ||
|
||
this.listenersMap.set(event, listeners); | ||
} | ||
|
||
listeners.add(listener); | ||
} | ||
|
||
emit<Event: $Keys<Events>>( | ||
event: Event, | ||
...args: $ElementType<Events, Event> | ||
): void { | ||
const listeners = this.listenersMap.get(event); | ||
if (listeners !== undefined) { | ||
let caughtError = null; | ||
|
||
listeners.forEach(listener => { | ||
try { | ||
listener.apply(null, args); | ||
} catch (error) { | ||
if (caughtError === null) { | ||
caughtError = error; | ||
} | ||
} | ||
}); | ||
|
||
if (caughtError !== null) { | ||
throw caughtError; | ||
} | ||
} | ||
} | ||
|
||
removeAllListeners(event?: $Keys<Events>): void { | ||
if (event != null) { | ||
this.listenersMap.delete(event); | ||
} else { | ||
this.listenersMap.clear(); | ||
} | ||
} | ||
|
||
removeListener(event: $Keys<Events>, listener: Function): void { | ||
const listeners = this.listenersMap.get(event); | ||
if (listeners !== undefined) { | ||
listeners.delete(listener); | ||
} | ||
} | ||
} |
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