From e1cdb1b76805ad8ba8f359f11bd3594d2df2c2d2 Mon Sep 17 00:00:00 2001 From: Jack Lam Date: Sat, 16 Dec 2017 18:52:20 +0800 Subject: [PATCH 1/2] Enhancement for "store" make it more robust --- src/store/index.js | 79 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index b21fda7..91dfb1d 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,3 +1,6 @@ +// @flow +/* eslint no-underscore-dangle: 0 */ // --> OFF + import { AsyncStorage } from 'react-native'; import { createStore, combineReducers, compose, applyMiddleware } from 'redux'; // eslint-disable-next-line @@ -7,27 +10,40 @@ import api from './api'; import firebase from './firebase'; import reducers from '../reducers'; -const middlewares = [ - api, - firebase, -]; - -if (__DEV__ === true) { - middlewares.push(createLogger({ - duration: true, - timestamp: true, - diff: true, - })); -} +// wrapper for redux store +export class AppStore { + constructor() { + this.store = null; + } -const appStore = { - setStore(store) { - appStore.store = store; - }, - getStore() { - return appStore.store; - }, - createStore(onComplete: ?() => void) { + get store(): Object { + if (!this.__store__) { + throw Error('store not yet created, must call createStore() before accessing store'); + } + return this.__store__; + } + + set store(store: Object): void { + this.__store__ = store; + } + + get loaded(): boolean { + return !!this.__store__; + } + + createStore(onComplete: ?() => void): Object { + const middlewares = [ + api, + firebase, + ]; + + if (__DEV__) { + middlewares.push(createLogger({ + duration: true, + timestamp: true, + diff: true, + })); + } const store = createStore( combineReducers(reducers), compose( @@ -37,9 +53,24 @@ const appStore = { ); persistStore(store, { storage: AsyncStorage }, onComplete); - appStore.setStore(store); + this.store = store; return store; - }, -}; + } +} -export default appStore; +// a proxy for appStore api to provide some magic. +// caller can access redux store's properties directly. +// for example: appStore.getState() +export default new Proxy(new AppStore(), { + get(target, key) { + const disallowedProps = ['store', '__store__']; + if (disallowedProps.includes(key)) { + return undefined; + } else if (target[key]) { + return target[key]; + } else if (target.store[key]) { + return target.store[key]; + } + return undefined; + }, +}); From 10696f0fd402793522ac2979d64bbd014245b6f8 Mon Sep 17 00:00:00 2001 From: Jack Lam Date: Sat, 16 Dec 2017 19:03:31 +0800 Subject: [PATCH 2/2] remove unnecessary condition --- src/store/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 91dfb1d..60a8f3a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -68,9 +68,7 @@ export default new Proxy(new AppStore(), { return undefined; } else if (target[key]) { return target[key]; - } else if (target.store[key]) { - return target.store[key]; } - return undefined; + return target.store[key]; }, });