-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add part of the event responder system for experimental event API (#1…
…5179) * Add part of the event responder system
- Loading branch information
Showing
15 changed files
with
593 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
import invariant from 'shared/invariant'; | ||
import {rethrowCaughtError} from 'shared/ReactErrorUtils'; | ||
|
||
import type {ReactSyntheticEvent} from './ReactSyntheticEventType'; | ||
import accumulateInto from './accumulateInto'; | ||
import forEachAccumulated from './forEachAccumulated'; | ||
import {executeDispatchesInOrder} from './EventPluginUtils'; | ||
|
||
/** | ||
* Internal queue of events that have accumulated their dispatches and are | ||
* waiting to have their dispatches executed. | ||
*/ | ||
let eventQueue: ?(Array<ReactSyntheticEvent> | ReactSyntheticEvent) = null; | ||
|
||
/** | ||
* Dispatches an event and releases it back into the pool, unless persistent. | ||
* | ||
* @param {?object} event Synthetic event to be dispatched. | ||
* @private | ||
*/ | ||
const executeDispatchesAndRelease = function(event: ReactSyntheticEvent) { | ||
if (event) { | ||
executeDispatchesInOrder(event); | ||
|
||
if (!event.isPersistent()) { | ||
event.constructor.release(event); | ||
} | ||
} | ||
}; | ||
const executeDispatchesAndReleaseTopLevel = function(e) { | ||
return executeDispatchesAndRelease(e); | ||
}; | ||
|
||
export function runEventsInBatch( | ||
events: Array<ReactSyntheticEvent> | ReactSyntheticEvent | null, | ||
) { | ||
if (events !== null) { | ||
eventQueue = accumulateInto(eventQueue, events); | ||
} | ||
|
||
// Set `eventQueue` to null before processing it so that we can tell if more | ||
// events get enqueued while processing. | ||
const processingEventQueue = eventQueue; | ||
eventQueue = null; | ||
|
||
if (!processingEventQueue) { | ||
return; | ||
} | ||
|
||
forEachAccumulated(processingEventQueue, executeDispatchesAndReleaseTopLevel); | ||
invariant( | ||
!eventQueue, | ||
'processEventQueue(): Additional events were enqueued while processing ' + | ||
'an event queue. Support for this has not yet been implemented.', | ||
); | ||
// This would be a good time to rethrow if any of the event handlers threw. | ||
rethrowCaughtError(); | ||
} |
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
Oops, something went wrong.