Synchronous work
Andrew Clark committed
Oct 28, 2016 at 11:12 UTC
a07f5eede0a3e0f083b37cd28610f67d2ff98f07
1 file changed
+44
-29
src/renderers/shared/fiber/ReactFiberScheduler.js
+44
-29
@@ -373,20 +373,7 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
373
}
374
375
function performDeferredWork(deadline) {
376
- try {
377
- performDeferredWorkUnsafe(deadline);
378
- } catch (error) {
379
- const failedUnitOfWork = nextUnitOfWork;
380
- // Reset because it points to the error boundary:
381
- nextUnitOfWork = null;
382
- if (!failedUnitOfWork) {
383
- // We shouldn't end up here because nextUnitOfWork
384
- // should always be set while work is being performed.
385
- throw error;
386
- }
387
- const trappedError = trapError(failedUnitOfWork, error);
388
- handleErrors([trappedError]);
389
- }
376
+ performAndHandleErrors(performDeferredWorkUnsafe, deadline);
377
}
378
379
function scheduleDeferredWork(root : FiberRoot, priority : PriorityLevel) {
@@ -439,20 +426,7 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
426
}
427
428
function performAnimationWork() {
442
- try {
443
- performAnimationWorkUnsafe();
444
- } catch (error) {
445
- const failedUnitOfWork = nextUnitOfWork;
446
- // Reset because it points to the error boundary:
447
- nextUnitOfWork = null;
448
- if (!failedUnitOfWork) {
449
- // We shouldn't end up here because nextUnitOfWork
450
- // should always be set while work is being performed.
451
- throw error;
452
- }
453
- const trappedError = trapError(failedUnitOfWork, error);
454
- handleErrors([trappedError]);
455
- }
429
+ performAndHandleErrors(performAnimationWorkUnsafe);
430
}
431
432
function scheduleAnimationWork(root: FiberRoot, priorityLevel : PriorityLevel) {
@@ -505,6 +479,46 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
479
return root;
480
}
481
482
+ function performSynchronousWorkUnsafe() {
483
+ // Perform work now
484
+ nextUnitOfWork = findNextUnitOfWork();
485
+ while (nextUnitOfWork &&
486
+ nextPriorityLevel === SynchronousPriority) {
487
+ nextUnitOfWork = performUnitOfWork(nextUnitOfWork, false);
488
+ // If there's no nextUnitForWork, we don't need to search for more
489
+ // because it shouldn't be possible to schedule sync work without
490
+ // immediately performing it
491
+ }
492
+ if (nextUnitOfWork) {
493
+ if (nextPriorityLevel > AnimationPriority) {
494
+ scheduleDeferredCallback(performDeferredWork);
495
+ return;
496
+ }
497
+ scheduleAnimationCallback(performAnimationWork);
498
+ }
499
+ }
500
+
501
+ function performSynchronousWork() {
502
+ performAndHandleErrors(performSynchronousWorkUnsafe);
503
+ }
504
+
505
+ function performAndHandleErrors<A>(fn: (a: A) => void, a: A) {
506
+ try {
507
+ fn(a);
508
+ } catch (error) {
509
+ const failedUnitOfWork = nextUnitOfWork;
510
+ // Reset because it points to the error boundary:
511
+ nextUnitOfWork = null;
512
+ if (!failedUnitOfWork) {
513
+ // We shouldn't end up here because nextUnitOfWork
514
+ // should always be set while work is being performed.
515
+ throw error;
516
+ }
517
+ const trappedError = trapError(failedUnitOfWork, error);
518
+ handleErrors([trappedError]);
519
+ }
520
+ }
521
+
522
function handleErrors(initialTrappedErrors : Array<TrappedError>) : void {
523
let nextTrappedErrors = initialTrappedErrors;
524
let firstUncaughtError = null;
@@ -579,7 +593,8 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
593
594
function scheduleWork(root : FiberRoot) {
595
if (defaultPriority === SynchronousPriority) {
582
- throw new Error('Not implemented yet');
596
+ root.current.pendingWorkPriority = SynchronousPriority;
597
+ performSynchronousWork();
598
}
599
600
if (defaultPriority === NoWork) {