Skip to content

Commit

Permalink
[Flare] Rework the responder dispatching/batching mechanism (#16334)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Aug 19, 2019
1 parent 6ae6a7c commit dce430a
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 565 deletions.
20 changes: 17 additions & 3 deletions packages/legacy-events/ReactGenericBatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from './ReactControlledComponent';
import {enableFlareAPI} from 'shared/ReactFeatureFlags';

import {invokeGuardedCallbackAndCatchFirstError} from 'shared/ReactErrorUtils';

// Used as a way to call batchedUpdates when we don't have a reference to
// the renderer. Such as when we're dispatching events or if third party
// libraries need to call batchedUpdates. Eventually, this API will go away when
Expand All @@ -28,6 +30,7 @@ let flushDiscreteUpdatesImpl = function() {};
let batchedEventUpdatesImpl = batchedUpdatesImpl;

let isInsideEventHandler = false;
let isBatchingEventUpdates = false;

function finishEventHandler() {
// Here we wait until all updates have propagated, which is important
Expand Down Expand Up @@ -60,20 +63,31 @@ export function batchedUpdates(fn, bookkeeping) {
}

export function batchedEventUpdates(fn, a, b) {
if (isInsideEventHandler) {
if (isBatchingEventUpdates) {
// If we are currently inside another batch, we need to wait until it
// fully completes before restoring state.
return fn(a, b);
}
isInsideEventHandler = true;
isBatchingEventUpdates = true;
try {
return batchedEventUpdatesImpl(fn, a, b);
} finally {
isInsideEventHandler = false;
isBatchingEventUpdates = false;
finishEventHandler();
}
}

export function executeUserEventHandler(fn: any => void, value: any) {
const previouslyInEventHandler = isInsideEventHandler;
try {
isInsideEventHandler = true;
const type = typeof value === 'object' && value !== null ? value.type : '';
invokeGuardedCallbackAndCatchFirstError(type, fn, undefined, value);
} finally {
isInsideEventHandler = previouslyInEventHandler;
}
}

export function discreteUpdates(fn, a, b, c) {
const prevIsInsideEventHandler = isInsideEventHandler;
isInsideEventHandler = true;
Expand Down
Loading

0 comments on commit dce430a

Please sign in to comment.