diff options
author | Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru> | 2019-06-11 02:51:30 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru> | 2019-06-11 02:51:30 +0300 |
commit | f12a3bccd8c726e59e92123a3d8ef1f5a73d7253 (patch) | |
tree | d4b6e126c0f216972562621608a4ba97800a94bf /src/Store | |
parent | b7810c3d6c0164def328c60244bfbd20d7935bc3 (diff) |
Fixes
Diffstat (limited to 'src/Store')
-rw-r--r-- | src/Store/store.ts | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/src/Store/store.ts b/src/Store/store.ts index 581f14b..2fb5a1f 100644 --- a/src/Store/store.ts +++ b/src/Store/store.ts @@ -1,8 +1,10 @@ import { createStore, combineReducers, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import { createReducer } from 'redux-su'; -import { composeWithDevTools } from 'redux-devtools-extension'; +import { AsyncStorage } from 'react-native'; +import { persistStore, persistReducer } from 'redux-persist'; import { Portal, Link, Field } from '../Api/types'; +import { Region } from '../Api/interfaces'; const reducers = { 'auth': createReducer({ @@ -32,8 +34,22 @@ const reducers = { ({ ...store, links: { ...store.links, ...action.links } }), 'fieldsSet': (store: any, action: { fields: { [guid: string]: Field } }) => ({ ...store, fields: { ...store.fields, ...action.fields } }), - 'setLoad': (store: any, action: { queue: string[][] }) => + 'setLoad': (store: any, action: { queue: string[] }) => ({ ...store, loadQueue: [...action.queue] }), + 'portalsGC': (store: any, action: { portals: { [tile: string]: string[] } }) => { + let portalsCache = store.portalsCache + let portals = store.portals + Object.keys(action.portals).forEach(tileId => { + const oldPortals = portalsCache[tileId] + if (oldPortals != undefined) { + oldPortals.forEach((guid: string) => { + delete portals[guid] + }) + } + portalsCache[tileId] = action.portals[tileId] + }) + return ({ ...store, portals: { ...portals }, portalsCache }) + }, 'linksGC': (store: any, action: { links: { [tile: string]: string[] } }) => { let linksCache = store.linksCache let links = store.links @@ -62,28 +78,31 @@ const reducers = { }) return ({ ...store, fields: { ...fields }, fieldsCache }) }, - }, { portals: {}, fields: {}, links: {}, loadQueue: [], linksCache: {}, fieldsCache: {} }), + }, { portals: {}, fields: {}, links: {}, loadQueue: [], linksCache: {}, fieldsCache: {}, portalsCache: {} }), 'settings': createReducer({ 'setLevelFrom': (store: any, action: { level: number }) => ({ ...store, filterLevel: [action.level, store.filterLevel[1]] }), 'setLevelTo': (store: any, action: { level: number }) => ({ ...store, filterLevel: [store.filterLevel[0], action.level] + }), + 'setRegion': (store: any, action: { region: Region }) => ({ + ...store, region: action.region }) - }, { filterLevel: [0, 8] }) + }, { filterLevel: [0, 8], region: null }) } -function extend(obj: any, src: any) { - for (var key in src) { - if (src.hasOwnProperty(key) && !!src[key]) { - obj[key] = src[key]; - } - } - return obj; -} +const rootReducer = combineReducers(reducers) -const store = createStore( - combineReducers(reducers), - composeWithDevTools(applyMiddleware(thunk)) -) -export default store
\ No newline at end of file +const persistConfig = { + key: 'root', + storage: AsyncStorage, + } + + const persistedReducer = persistReducer(persistConfig, rootReducer) + + export default () => { + let store = createStore(persistedReducer, applyMiddleware(thunk)) + let persistor = persistStore(store) + return { store, persistor } + }
\ No newline at end of file |