Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React fiber compatibility #5

Merged
merged 4 commits into from
Aug 8, 2017
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "iojs"
- "6"
script:
- npm test
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
"devDependencies": {
"babel": "^5.5.8",
"babel-core": "^5.6.18",
"babel-eslint": "^3.1.15",
"babel-eslint": "^4.1.8",
"eslint": "^0.23",
"eslint-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0",
"expect": "^1.6.0",
"jsdom": "^7.2.2",
"mocha": "^2.2.5",
"mocha-jsdom": "^1.0.0",
"mocha-jsdom": "^1.1.0",
"react": "^0.14.0-rc1",
"react-addons-test-utils": "^0.14.0-rc1",
"rimraf": "^2.3.4"
Expand Down
52 changes: 49 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

// Constant to identify a React Component. It's been extracted from ReactTypeOfWork
// (https://github.com/facebook/react/blob/master/src/shared/ReactTypeOfWork.js#L20)
const ReactClassComponent = 2;

function traverseRenderedChildren(internalInstance, callback, argument) {
callback(internalInstance, argument);

Expand Down Expand Up @@ -33,10 +38,51 @@ function forceUpdateIfPending(internalInstance, React) {
}
}

function deepForceUpdateStack(instance, React) {
const internalInstance = instance._reactInternalInstance;
traverseRenderedChildren(internalInstance, setPendingForceUpdate);
traverseRenderedChildren(internalInstance, forceUpdateIfPending, React);
}

function deepForceUpdate(instance, React) {
const root = instance._reactInternalInstance;
if (typeof root.tag !== 'number') {
// Traverse stack-based React tree.
return deepForceUpdateStack(instance, React);
}

let node = root;
while (true) {
if (node.tag === ReactClassComponent) {
const publicInstance = node.stateNode;
const { updater } = publicInstance;
if (typeof publicInstance.forceUpdate === 'function') {
publicInstance.forceUpdate();
} else if (updater && typeof updater.enqueueForceUpdate === 'function') {
updater.enqueueForceUpdate(publicInstance);
}
}
if (node.child) {
node.child.return = node;
node = node.child;
continue;
}
if (node === root) {
return undefined;
}
while (!node.sibling) {
if (!node.return || node.return === root) {
return undefined;
}
node = node.return;
}
node.sibling.return = node.return;
node = node.sibling;
}
}

export default function getForceUpdate(React) {
return instance => {
const internalInstance = instance._reactInternalInstance;
traverseRenderedChildren(internalInstance, setPendingForceUpdate);
traverseRenderedChildren(internalInstance, forceUpdateIfPending, React);
deepForceUpdate(instance, React);
};
}