Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/isomorphic/classic/class/ReactClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@ var ReactClassMixin = {
* type signature and the only use case for this, is to avoid that.
*/
replaceState: function(newState, callback) {
if (this.updater.isFiberUpdater) {
this.updater.enqueueReplaceState(this, newState, callback);
return;
}

this.updater.enqueueReplaceState(this, newState);
if (callback) {
this.updater.enqueueCallback(this, callback, 'replaceState');
Expand Down
11 changes: 11 additions & 0 deletions src/isomorphic/modern/class/ReactComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ ReactComponent.prototype.setState = function(partialState, callback) {
'setState(...): takes an object of state variables to update or a ' +
'function which returns an object of state variables.'
);

if (this.updater.isFiberUpdater) {
this.updater.enqueueSetState(this, partialState, callback);
return;
}

this.updater.enqueueSetState(this, partialState);
if (callback) {
this.updater.enqueueCallback(this, callback, 'setState');
Expand All @@ -86,6 +92,11 @@ ReactComponent.prototype.setState = function(partialState, callback) {
* @protected
*/
ReactComponent.prototype.forceUpdate = function(callback) {
if (this.updater.isFiberUpdater) {
this.updater.enqueueForceUpdate(this, callback);
return;
}

this.updater.enqueueForceUpdate(this);
if (callback) {
this.updater.enqueueCallback(this, callback, 'forceUpdate');
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/dom/fiber/ReactDOMFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ var DOMRenderer = ReactFiberReconciler({

scheduleDeferredCallback: window.requestIdleCallback,

useSyncScheduling: true,

});

var warned = false;
Expand Down Expand Up @@ -163,6 +165,10 @@ var ReactDOM = {
return DOMRenderer.findHostInstance(component);
},

unstable_batchedUpdates<A>(fn : () => A) : A {
return DOMRenderer.batchedUpdates(fn);
},

};

module.exports = ReactDOM;
2 changes: 1 addition & 1 deletion src/renderers/shared/fiber/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var ReactFiberClassComponent = require('ReactFiberClassComponent');

module.exports = function<T, P, I, TI, C>(
config : HostConfig<T, P, I, TI, C>,
scheduleUpdate : (fiber: Fiber, priorityLevel : PriorityLevel) => void
scheduleUpdate : (fiber: Fiber, priorityLevel : ?PriorityLevel) => void
) {

const {
Expand Down
44 changes: 21 additions & 23 deletions src/renderers/shared/fiber/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type { Fiber } from 'ReactFiber';
import type { PriorityLevel } from 'ReactPriorityLevel';
import type { UpdateQueue } from 'ReactFiberUpdateQueue';

var { LowPriority } = require('ReactPriorityLevel');
var {
createUpdateQueue,
addToQueue,
Expand All @@ -27,51 +26,50 @@ var { isMounted } = require('ReactFiberTreeReflection');
var ReactInstanceMap = require('ReactInstanceMap');
var shallowEqual = require('shallowEqual');

module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : PriorityLevel) => void) {
module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : ?PriorityLevel) => void) {

function scheduleUpdateQueue(fiber: Fiber, updateQueue: UpdateQueue, priorityLevel : PriorityLevel) {
function scheduleUpdateQueue(fiber: Fiber, updateQueue: UpdateQueue) {
fiber.updateQueue = updateQueue;
// Schedule update on the alternate as well, since we don't know which tree
// is current.
if (fiber.alternate) {
fiber.alternate.updateQueue = updateQueue;
}
scheduleUpdate(fiber, priorityLevel);
scheduleUpdate(fiber);
}

// Class component state updater
const updater = {
isMounted,
enqueueSetState(instance, partialState) {
enqueueSetState(instance, partialState, callback) {
const fiber = ReactInstanceMap.get(instance);
const updateQueue = fiber.updateQueue ?
addToQueue(fiber.updateQueue, partialState) :
createUpdateQueue(partialState);
scheduleUpdateQueue(fiber, updateQueue, LowPriority);
if (callback) {
addCallbackToQueue(updateQueue, callback);
}
scheduleUpdateQueue(fiber, updateQueue);
},
enqueueReplaceState(instance, state) {
enqueueReplaceState(instance, state, callback) {
const fiber = ReactInstanceMap.get(instance);
const updateQueue = createUpdateQueue(state);
updateQueue.isReplace = true;
scheduleUpdateQueue(fiber, updateQueue, LowPriority);
if (callback) {
addCallbackToQueue(updateQueue, callback);
}
scheduleUpdateQueue(fiber, updateQueue);
},
enqueueForceUpdate(instance) {
enqueueForceUpdate(instance, callback) {
const fiber = ReactInstanceMap.get(instance);
const updateQueue = fiber.updateQueue || createUpdateQueue(null);
updateQueue.isForced = true;
scheduleUpdateQueue(fiber, updateQueue, LowPriority);
},
enqueueCallback(instance, callback) {
const fiber = ReactInstanceMap.get(instance);
let updateQueue = fiber.updateQueue ?
fiber.updateQueue :
createUpdateQueue(null);
addCallbackToQueue(updateQueue, callback);
fiber.updateQueue = updateQueue;
if (fiber.alternate) {
fiber.alternate.updateQueue = updateQueue;
if (callback) {
addCallbackToQueue(updateQueue, callback);
}
scheduleUpdateQueue(fiber, updateQueue);
},
isFiberUpdater: true,
};

function checkShouldComponentUpdate(workInProgress, oldProps, newProps, newState) {
Expand Down Expand Up @@ -131,7 +129,7 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
// process them now.
const updateQueue = workInProgress.updateQueue;
if (updateQueue) {
instance.state = mergeUpdateQueue(updateQueue, state, props);
instance.state = mergeUpdateQueue(updateQueue, instance, state, props);
}
}
}
Expand Down Expand Up @@ -175,7 +173,7 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
// process them now.
const newUpdateQueue = workInProgress.updateQueue;
if (newUpdateQueue) {
newInstance.state = mergeUpdateQueue(newUpdateQueue, newState, newProps);
newInstance.state = mergeUpdateQueue(newUpdateQueue, newInstance, newState, newProps);
}
}
return true;
Expand Down Expand Up @@ -212,7 +210,7 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
// TODO: Previous state can be null.
let newState;
if (updateQueue) {
newState = mergeUpdateQueue(updateQueue, previousState, newProps);
newState = mergeUpdateQueue(updateQueue, instance, previousState, newProps);
} else {
newState = previousState;
}
Expand Down
18 changes: 18 additions & 0 deletions src/renderers/shared/fiber/ReactFiberErrorBoundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
'use strict';

import type { Fiber } from 'ReactFiber';
import type { FiberRoot } from 'ReactFiberRoot';

var {
ClassComponent,
HostContainer,
} = require('ReactTypeOfWork');

export type TrappedError = {
boundary: Fiber | null,
error: any,
root: FiberRoot,
};

function findClosestErrorBoundary(fiber : Fiber): Fiber | null {
Expand All @@ -37,9 +40,24 @@ function findClosestErrorBoundary(fiber : Fiber): Fiber | null {
return null;
}

function findRoot(fiber : Fiber) : FiberRoot {
while (fiber) {
if (!fiber.return) {
if (fiber.tag === HostContainer) {
return ((fiber.stateNode : any) : FiberRoot);
} else {
throw new Error('Invalid root');
}
}
fiber = fiber.return;
}
throw new Error('Could not find a root.');
}

function trapError(fiber : Fiber, error : any) : TrappedError {
return {
boundary: findClosestErrorBoundary(fiber),
root: findRoot(fiber),
error,
};
}
Expand Down
17 changes: 14 additions & 3 deletions src/renderers/shared/fiber/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (__DEV__) {

var { findCurrentHostFiber } = require('ReactFiberTreeReflection');

type Deadline = {
export type Deadline = {
timeRemaining : () => number
};

Expand Down Expand Up @@ -56,8 +56,9 @@ export type HostConfig<T, P, I, TI, C> = {
removeChild(parentInstance : I, child : I | TI) : void,

scheduleAnimationCallback(callback : () => void) : void,
scheduleDeferredCallback(callback : (deadline : Deadline) => void) : void
scheduleDeferredCallback(callback : (deadline : Deadline) => void) : void,

useSyncScheduling ?: boolean,
};

type OpaqueNode = Fiber;
Expand All @@ -67,6 +68,10 @@ export type Reconciler<C, I, TI> = {
updateContainer(element : ReactElement<any>, container : OpaqueNode) : void,
unmountContainer(container : OpaqueNode) : void,
performWithPriority(priorityLevel : PriorityLevel, fn : Function) : void,
/* eslint-disable no-undef */
// FIXME: ESLint complains about type parameter
batchedUpdates<A>(fn : () => A) : A,
/* eslint-enable no-undef */

// Used to extract the return value from the initial render. Legacy API.
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | TI | I | null),
Expand All @@ -77,7 +82,11 @@ export type Reconciler<C, I, TI> = {

module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) : Reconciler<C, I, TI> {

var { scheduleWork, performWithPriority } = ReactFiberScheduler(config);
var {
scheduleWork,
performWithPriority,
batchedUpdates,
} = ReactFiberScheduler(config);

return {

Expand Down Expand Up @@ -141,6 +150,8 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) :

performWithPriority,

batchedUpdates,

getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | I | TI | null) {
const root : FiberRoot = (container.stateNode : any);
const containerFiber = root.current;
Expand Down
Loading