From c8156325343c35895c0c6060437a1c72f58d43f9 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Fri, 16 Nov 2018 11:20:51 +0100 Subject: [PATCH 1/2] fix: Watch mobile command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c989b78ec..b4411d8bc 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "build:browser:min": "yarn run webpack --env.production", "build:mobile:min": "yarn run webpack --env.production --env.target=mobile", "watch": "yarn run webpack --watch --display-chunks", - "watch:mobile": "yarn run watch:common --env.target=mobile", + "watch:mobile": "yarn run watch --env.target=mobile", "clean": "rm -rf ./dist/*", "lint": "eslint 'config/**/*.{js,jsx}, src/**/*.{js,jsx}'", "prebuild": "npm-run-all lint clean tx", From 0b71760e061939cdf9e0ac740b2fead59e2fcf07 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Fri, 16 Nov 2018 15:32:03 +0100 Subject: [PATCH 2/2] fix: Only 1 store --- src/index.jsx | 4 ++-- src/lib/store/index.js | 15 +++++++++++---- test/store/index.spec.js | 10 +++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index 40a3614d0..063a9ead4 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -178,8 +178,8 @@ const init = async ({ isPublic = true } // store - const createReduxStore = require('lib/store').default - const reduxStore = createReduxStore() + const getOrCreateStore = require('lib/store').default + const reduxStore = getOrCreateStore() reduxStore.dispatch(setInfos(appName, appNamePrefix, appSlug)) stack.init({ diff --git a/src/lib/store/index.js b/src/lib/store/index.js index 2ffc36e27..d8a7bb765 100644 --- a/src/lib/store/index.js +++ b/src/lib/store/index.js @@ -1,6 +1,6 @@ /* global __DEVELOPMENT__ */ -import { createStore, applyMiddleware } from 'redux' +import { createStore as createReduxStore, applyMiddleware } from 'redux' import appsI18nMiddleware from '../middlewares/appsI18n' import thunkMiddleware from 'redux-thunk' import { persistStore, persistCombineReducers } from 'redux-persist' @@ -22,11 +22,18 @@ const middlewares = [appsI18nMiddleware, thunkMiddleware] if (__DEVELOPMENT__) middlewares.push(loggerMiddleware) -const createReduxStore = () => { - let store = createStore(reducer, applyMiddleware.apply(null, middlewares)) +export const createStore = () => { + store = createReduxStore(reducer, applyMiddleware.apply(null, middlewares)) persistStore(store) + return store +} +let store +const getOrCreateStore = () => { + if (!store) { + store = createStore() + } return store } -export default createReduxStore +export default getOrCreateStore diff --git a/test/store/index.spec.js b/test/store/index.spec.js index e2a28ef1e..28922a254 100644 --- a/test/store/index.spec.js +++ b/test/store/index.spec.js @@ -1,4 +1,4 @@ -import createStore from 'lib/store' +import getOrCreateStore, { createStore } from 'lib/store' import { isFetchingApps, getApps, hasFetched } from 'lib/reducers' describe('store', () => { @@ -31,3 +31,11 @@ describe('store', () => { expect(hasFetched(getState())).toEqual(true) }) }) + +describe('singleton', () => { + it('should work as a singleton', () => { + const store = getOrCreateStore() + const store2 = getOrCreateStore() + expect(store).toEqual(store2) + }) +})