-
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.
Move sync task queue to its own module
The sync task queue is React-specific and doesn't really have anything to do with Scheduler. We'd keep using it even once `postTask` exists. By separating that part out, `SchedulerWithReactIntegration` is now just a module that re-exports the Scheduler API. So I unforked it. When we switch to ES Modules, we can remove this re-exporting module.
- Loading branch information
Showing
10 changed files
with
84 additions
and
112 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
67 changes: 67 additions & 0 deletions
67
packages/react-reconciler/src/ReactFiberSyncTaskQueue.new.js
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,67 @@ | ||
/** | ||
* 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 type {SchedulerCallback} from './Scheduler'; | ||
|
||
import { | ||
DiscreteEventPriority, | ||
getCurrentUpdatePriority, | ||
setCurrentUpdatePriority, | ||
} from './ReactEventPriorities.new'; | ||
import {ImmediatePriority, scheduleCallback} from './Scheduler'; | ||
|
||
let syncQueue: Array<SchedulerCallback> | null = null; | ||
let isFlushingSyncQueue: boolean = false; | ||
|
||
export function scheduleSyncCallback(callback: SchedulerCallback) { | ||
// Push this callback into an internal queue. We'll flush these either in | ||
// the next tick, or earlier if something calls `flushSyncCallbackQueue`. | ||
if (syncQueue === null) { | ||
syncQueue = [callback]; | ||
} else { | ||
// Push onto existing queue. Don't need to schedule a callback because | ||
// we already scheduled one when we created the queue. | ||
syncQueue.push(callback); | ||
} | ||
} | ||
|
||
export function flushSyncCallbackQueue() { | ||
if (!isFlushingSyncQueue && syncQueue !== null) { | ||
// Prevent re-entrancy. | ||
isFlushingSyncQueue = true; | ||
let i = 0; | ||
const previousUpdatePriority = getCurrentUpdatePriority(); | ||
try { | ||
const isSync = true; | ||
const queue = syncQueue; | ||
// TODO: Is this necessary anymore? The only user code that runs in this | ||
// queue is in the render or commit phases. | ||
setCurrentUpdatePriority(DiscreteEventPriority); | ||
for (; i < queue.length; i++) { | ||
let callback = queue[i]; | ||
do { | ||
callback = callback(isSync); | ||
} while (callback !== null); | ||
} | ||
syncQueue = null; | ||
} catch (error) { | ||
// If something throws, leave the remaining callbacks on the queue. | ||
if (syncQueue !== null) { | ||
syncQueue = syncQueue.slice(i + 1); | ||
} | ||
// Resume flushing in the next tick | ||
scheduleCallback(ImmediatePriority, flushSyncCallbackQueue); | ||
throw error; | ||
} finally { | ||
setCurrentUpdatePriority(previousUpdatePriority); | ||
isFlushingSyncQueue = false; | ||
} | ||
} | ||
return null; | ||
} |
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