From 92ba6757dac24b11b1d78bede4243fff46104aeb Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 1 Jan 2015 18:51:20 +0100 Subject: [PATCH] Make root instance provider injectable. Fixes #53 --- DefaultRootInstanceProvider.js | 8 ++++++++ Injection.js | 7 +++++++ RootInstanceProvider.js | 21 +++++++++++++++++++++ getRootInstances.js | 8 -------- index.js | 8 ++++---- 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 DefaultRootInstanceProvider.js create mode 100644 Injection.js create mode 100644 RootInstanceProvider.js delete mode 100644 getRootInstances.js diff --git a/DefaultRootInstanceProvider.js b/DefaultRootInstanceProvider.js new file mode 100644 index 000000000..e9c427a10 --- /dev/null +++ b/DefaultRootInstanceProvider.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = { + getRootInstances: function () { + var ReactMount = require('react/lib/ReactMount'); + return ReactMount._instancesByReactRootID || ReactMount._instancesByContainerID; + } +}; \ No newline at end of file diff --git a/Injection.js b/Injection.js new file mode 100644 index 000000000..5420af594 --- /dev/null +++ b/Injection.js @@ -0,0 +1,7 @@ +'use strict'; + +var RootInstanceProvider = require('./RootInstanceProvider'); + +module.exports = { + RootInstanceProvider: RootInstanceProvider.injection +}; \ No newline at end of file diff --git a/RootInstanceProvider.js b/RootInstanceProvider.js new file mode 100644 index 000000000..0759616cf --- /dev/null +++ b/RootInstanceProvider.js @@ -0,0 +1,21 @@ +'use strict'; + +var DefaultRootInstanceProvider = require('./DefaultRootInstanceProvider'); + +var injectedProvider = null; + +var RootInstanceProvider = { + injection: { + injectProvider: function (provider) { + injectedProvider = provider; + } + }, + + getRootInstances: function () { + return injectedProvider.getRootInstances(); + } +}; + +RootInstanceProvider.injection.injectProvider(DefaultRootInstanceProvider); + +module.exports = RootInstanceProvider; \ No newline at end of file diff --git a/getRootInstances.js b/getRootInstances.js deleted file mode 100644 index 61835515d..000000000 --- a/getRootInstances.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -function getRootInstances() { - var ReactMount = require('react/lib/ReactMount'); - return ReactMount._instancesByReactRootID || ReactMount._instancesByContainerID; -} - -module.exports = getRootInstances; \ No newline at end of file diff --git a/index.js b/index.js index 60e163e34..5e4f93fee 100644 --- a/index.js +++ b/index.js @@ -29,11 +29,11 @@ module.exports = function (source, map) { '/* REACT HOT LOADER */', 'if (module.hot) {', '(function () {', - 'var reactHotApi = require(' + JSON.stringify(require.resolve('react-hot-api')) + '),', - ' getRootInstances = require(' + JSON.stringify(require.resolve('./getRootInstances')) + ');', + 'var ReactHotAPI = require(' + JSON.stringify(require.resolve('react-hot-api')) + '),', + ' RootInstanceProvider = require(' + JSON.stringify(require.resolve('./RootInstanceProvider')) + ');', '', - 'if (typeof reactHotApi === "function" && typeof getRootInstances === "function") {', - 'module.makeHot = module.hot.data ? module.hot.data.makeHot : reactHotApi(getRootInstances);', + 'if (typeof ReactHotAPI === "function" && typeof RootInstanceProvider === "object") {', + 'module.makeHot = module.hot.data ? module.hot.data.makeHot : ReactHotAPI(RootInstanceProvider.getRootInstances);', '}', '})();', '}'