Skip to content

Commit

Permalink
Merge pull request #350 from cozy/fix-only-1-store
Browse files Browse the repository at this point in the history
Fix only 1 store
  • Loading branch information
ptbrowne authored Nov 16, 2018
2 parents f2ee949 + 0b71760 commit d939f46
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
15 changes: 11 additions & 4 deletions src/lib/store/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
10 changes: 9 additions & 1 deletion test/store/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createStore from 'lib/store'
import getOrCreateStore, { createStore } from 'lib/store'
import { isFetchingApps, getApps, hasFetched } from 'lib/reducers'

describe('store', () => {
Expand Down Expand Up @@ -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)
})
})

0 comments on commit d939f46

Please sign in to comment.