Skip to content

Commit

Permalink
React Native Compatibility
Browse files Browse the repository at this point in the history
Summary:
**This PR depends on an unreleased version of `fbjs`, so DO NOT MERGE.**

When merged along with facebook/react-native#5084, facebook/fbjs#95, and whatever PR fixes facebook/react-native#4062 (which I will update this issue with when I push it), this fixes #26.

The changes to Relay itself are super minor here:

1. Remove the reliance on ReactDOM. The only use of ReactDOM is `unstable_batchedupdates`. So to fix, I abstracted the reference to `unstable_batchedupdates` to it's own module, and then took advantage of the "react-native" `package.json` option, supported by the React Native packager, to load the correct version of this function given the execution context.
2. Removed `react-dom` from peerDependencies (but kept it in devDependencies, for use in tests), and also upgrade the `fbjs` dependency to a (yet unreleased) version that provides better compatibility with React Native.
Closes #713

Reviewed By: yungsters

Differential Revision: D2872129

fb-gh-sync-id: f6ba6d06cfdde8ad8cbb0c7cd9d645f44f65e437
  • Loading branch information
skevy authored and facebook-github-bot-7 committed Jan 28, 2016
1 parent 482ee7c commit 60c8a76
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var babelOpts = {
_moduleMap: objectAssign({}, require('fbjs/module-map'), {
'React': 'react',
'ReactDOM': 'react-dom',
'ReactNative': 'react-native',
'StaticContainer.react': 'react-static-container',
}),
};
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
},
"dependencies": {
"babel-runtime": "5.8.24",
"fbjs": "^0.5.1",
"fbjs": "^0.7.0",
"react-static-container": "^1.0.0-alpha.1"
},
"peerDependencies": {
"babel-relay-plugin": "0.6.3",
"react": "^0.14.0",
"react-dom": "^0.14.0"
"react": "^0.14.0"
},
"devDependencies": {
"babel": "^5.8.25",
Expand All @@ -55,15 +54,15 @@
"gulp-util": "^3.0.6",
"jest-cli": "^0.8.1",
"object-assign": "^3.0.0",
"react": "^0.14.0-rc",
"react-dom": "^0.14.0-rc",
"react": "^0.14.0",
"react-dom": "^0.14.0",
"run-sequence": "^1.1.2",
"webpack": "1.11.0",
"webpack-stream": "^2.1.0"
},
"devEngines": {
"node": "4.x",
"npm": "2.x"
"node": ">=4.x",
"npm": ">=2.x"
},
"jest": {
"rootDir": "",
Expand All @@ -73,6 +72,7 @@
"persistModuleRegistryBetweenSpecs": true,
"modulePathIgnorePatterns": [
"<rootDir>/lib/",
"<rootDir>/src/(.*).native.js",
"<rootDir>/node_modules/(?!(fbjs/lib/|react/lib/|fbjs-scripts/jest))"
],
"preprocessorIgnorePatterns": [
Expand Down
2 changes: 1 addition & 1 deletion src/.flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.*/react-static-container/node_modules/.*

[include]
../node_modules/fbjs/flow/include
../node_modules/fbjs/lib
../node_modules/fbjs/node_modules/promise
../node_modules/react
../node_modules/react-static-container/lib
Expand Down
6 changes: 3 additions & 3 deletions src/container/RelayContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const ErrorUtils = require('ErrorUtils');
const GraphQLFragmentPointer = require('GraphQLFragmentPointer');
const GraphQLStoreQueryResolver = require('GraphQLStoreQueryResolver');
const React = require('React');
const ReactDOM = require('ReactDOM');
const RelayContainerComparators = require('RelayContainerComparators');
const RelayContainerProxy = require('RelayContainerProxy');
const RelayFragmentReference = require('RelayFragmentReference');
Expand Down Expand Up @@ -47,6 +46,7 @@ const forEachObject = require('forEachObject');
const invariant = require('invariant');
const nullthrows = require('nullthrows');
const prepareRelayContainerProps = require('prepareRelayContainerProps');
const relayUnstableBatchedUpdates = require('relayUnstableBatchedUpdates');
const shallowEqual = require('shallowEqual');
const warning = require('warning');
const isReactComponent = require('isReactComponent');
Expand Down Expand Up @@ -80,7 +80,7 @@ var containerContextTypes = {
var storeData = RelayStoreData.getDefaultInstance();

storeData.getChangeEmitter().injectBatchingStrategy(
ReactDOM.unstable_batchedUpdates
relayUnstableBatchedUpdates
);

/**
Expand Down Expand Up @@ -286,7 +286,7 @@ function createContainerComponent(
var mounted = this.mounted;
if (mounted) {
var updateProfiler = RelayProfiler.profile('RelayContainer.update');
ReactDOM.unstable_batchedUpdates(() => {
relayUnstableBatchedUpdates(() => {
this.setState(partialState, () => {
updateProfiler.stop();
if (isComplete) {
Expand Down
12 changes: 12 additions & 0 deletions src/tools/__mocks__/relayUnstableBatchedUpdates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

module.exports = require.requireActual('relayUnstableBatchedUpdates');

This comment has been minimized.

Copy link
@josephsavona

josephsavona Jan 28, 2016

Contributor

@skevy for reference, this is the alternative to adding a module to the jest config - it's a bit easier for us since there are multiple jest configs (FB, GitHub) that would have to be updated.

This comment has been minimized.

Copy link
@skevy

skevy Jan 28, 2016

Author Contributor

Thanks! Now I know. :)

18 changes: 18 additions & 0 deletions src/tools/relayUnstableBatchedUpdates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule relayUnstableBatchedUpdates
* @flow
* @typechecks
*/

'use strict';

const ReactDOM = require('ReactDOM');

module.exports = ReactDOM.unstable_batchedUpdates;
16 changes: 16 additions & 0 deletions src/tools/relayUnstableBatchedUpdates.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule relayUnstableBatchedUpdates
*/

'use strict';

const ReactNative = require('ReactNative');

module.exports = ReactNative.unstable_batchedUpdates;

This comment has been minimized.

Copy link
@josephsavona

josephsavona Jan 28, 2016

Contributor

since this exports a function, i renamed the module to lowercase to match our convention. otherwise, this is per the original PR.

This comment has been minimized.

Copy link
@skevy

skevy Jan 28, 2016

Author Contributor

Cool beans. :)

0 comments on commit 60c8a76

Please # to comment.