From 01baabc7cb70b5c4db79104a21f594b27a85a4b4 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Wed, 3 Apr 2019 17:43:49 +0400 Subject: [PATCH] add loadFromStorage funC --- src/logic/safe/utils/safeStorage.js | 6 +++--- src/logic/tokens/utils/tokensStorage.js | 6 +++--- src/routes/load/container/Load.jsx | 4 ++-- src/routes/safe/store/reducer/safe.js | 4 ++-- src/utils/storage/index.js | 14 ++++++++++++-- src/utils/storage/signatures.js | 6 +++--- src/utils/storage/transactions.js | 6 +++--- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/logic/safe/utils/safeStorage.js b/src/logic/safe/utils/safeStorage.js index 62a403ac..561c824d 100644 --- a/src/logic/safe/utils/safeStorage.js +++ b/src/logic/safe/utils/safeStorage.js @@ -1,14 +1,14 @@ // @flow import { type Owner } from '~/routes/safe/store/model/owner' import { List, Map } from 'immutable' -import { storage, load } from '~/utils/storage' +import { storage, loadFromStorage } from '~/utils/storage' export const SAFES_KEY = 'SAFES' export const TX_KEY = 'TX' export const OWNERS_KEY = 'OWNERS' export const getSafeName = async (safeAddress: string) => { - const safes = await load(SAFES_KEY) + const safes = await loadFromStorage(SAFES_KEY) if (!safes) { return undefined } @@ -39,7 +39,7 @@ export const setOwners = async (safeAddress: string, owners: List) => { } export const getOwners = async (safeAddress: string): Map => { - const data: Object = await load(`${OWNERS_KEY}-${safeAddress}`) + const data: Object = await loadFromStorage(`${OWNERS_KEY}-${safeAddress}`) return data ? Map(data) : Map() } \ No newline at end of file diff --git a/src/logic/tokens/utils/tokensStorage.js b/src/logic/tokens/utils/tokensStorage.js index 9f1263a1..dfb3e6b2 100644 --- a/src/logic/tokens/utils/tokensStorage.js +++ b/src/logic/tokens/utils/tokensStorage.js @@ -1,7 +1,7 @@ // @flow import { List } from 'immutable' import { type Token, type TokenProps } from '~/logic/tokens/store/model/token' -import { storage, load } from '~/utils/storage' +import { storage, loadFromStorage } from '~/utils/storage' export const ACTIVE_TOKENS_KEY = 'ACTIVE_TOKENS' export const TOKENS_KEY = 'TOKENS' @@ -22,14 +22,14 @@ export const setActiveTokens = async (safeAddress: string, tokens: List> => { const key = getActiveTokensKey(safeAddress) - const data = await load(key) + const data = await loadFromStorage(key) return data ? List(data) : List() } export const getTokens = async (safeAddress: string): Promise> => { const key = getTokensKey(safeAddress) - const data = await load(key) + const data = await loadFromStorage(key) return data ? List(data) : List() } diff --git a/src/routes/load/container/Load.jsx b/src/routes/load/container/Load.jsx index d850da39..4dc1cd7b 100644 --- a/src/routes/load/container/Load.jsx +++ b/src/routes/load/container/Load.jsx @@ -4,7 +4,7 @@ import { connect } from 'react-redux' import Page from '~/components/layout/Page' import { buildSafe } from '~/routes/safe/store/actions/fetchSafe' import { SAFES_KEY, saveSafes } from '~/logic/safe/utils' -import { load } from '~/utils/storage' +import { loadFromStorage } from '~/utils/storage' import { SAFELIST_ADDRESS } from '~/routes/routes' import { history } from '~/store' import selector, { type SelectorProps } from './selector' @@ -19,7 +19,7 @@ export const loadSafe = async (safeName: string, safeAddress: string, updateSafe await updateSafe(safeRecord) - const storedSafes = await load(SAFES_KEY) || {} + const storedSafes = await loadFromStorage(SAFES_KEY) || {} storedSafes[safeAddress] = safeRecord.toJSON() saveSafes(storedSafes) diff --git a/src/routes/safe/store/reducer/safe.js b/src/routes/safe/store/reducer/safe.js index c28d8218..a9ff7eb3 100644 --- a/src/routes/safe/store/reducer/safe.js +++ b/src/routes/safe/store/reducer/safe.js @@ -4,7 +4,7 @@ import { handleActions, type ActionType } from 'redux-actions' import { ADD_SAFE, buildOwnersFrom } from '~/routes/safe/store/actions/addSafe' import { type Safe, type SafeProps, makeSafe } from '~/routes/safe/store/model/safe' import { type OwnerProps } from '~/routes/safe/store/model/owner' -import { load } from '~/utils/storage' +import { loadFromStorage } from '~/utils/storage' import { SAFES_KEY } from '~/logic/safe/utils' import { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe' @@ -46,7 +46,7 @@ const buildSafesFrom = (loadedSafes: Object): Map => { } export const safesInitialState = (): State => { - const storedSafes = load(SAFES_KEY) + const storedSafes = loadFromStorage(SAFES_KEY) const safes = storedSafes ? buildSafesFrom(storedSafes) : Map() return safes diff --git a/src/utils/storage/index.js b/src/utils/storage/index.js index 815ffbeb..f8619a7b 100644 --- a/src/utils/storage/index.js +++ b/src/utils/storage/index.js @@ -6,9 +6,11 @@ import { const stores = [CookieStore, IndexedDbStore, LocalStorageStore, SessionStorageStore] export const storage = new ImmortalStorage(stores) -export const load = async (key: string): Promise<*> => { +const PREFIX = 'v1' + +export const loadFromStorage = async (key: string): Promise<*> => { try { - const serializedState = await storage.get(key) + const serializedState = await storage.get(`${PREFIX}__${key}`) if (serializedState === null || serializedState === undefined) { return undefined } @@ -18,3 +20,11 @@ export const load = async (key: string): Promise<*> => { return undefined } } + +export const saveInStorage = async (key: string, value: string): Promise<*> => { + try { + await storage.set(`${PREFIX}__${key}`, value) + } catch (err) { + console.error(`Failed to save ${key} in the storage:`, err) + } +} diff --git a/src/utils/storage/signatures.js b/src/utils/storage/signatures.js index 1b7e77f2..824cff6f 100644 --- a/src/utils/storage/signatures.js +++ b/src/utils/storage/signatures.js @@ -1,12 +1,12 @@ // @flow import { Map } from 'immutable' -import { load } from '~/utils/storage' +import { loadFromStorage } from '~/utils/storage' const getSignaturesKeyFrom = (safeAddress: string) => `TXS-SIGNATURES-${safeAddress}` export const storeSignature = async (safeAddress: string, nonce: number, signature: string) => { const signaturesKey = getSignaturesKeyFrom(safeAddress) - const subjects = Map(await load(signaturesKey)) || Map() + const subjects = Map(await loadFromStorage(signaturesKey)) || Map() try { const key = `${nonce}` @@ -23,7 +23,7 @@ export const storeSignature = async (safeAddress: string, nonce: number, signatu export const getSignaturesFrom = (safeAddress: string, nonce: number) => { const key = getSignaturesKeyFrom(safeAddress) - const data: any = load(key) + const data: any = loadFromStorage(key) const signatures = data ? Map(data) : Map() const txSigs = signatures.get(String(nonce)) || '' diff --git a/src/utils/storage/transactions.js b/src/utils/storage/transactions.js index 19fad841..e924381c 100644 --- a/src/utils/storage/transactions.js +++ b/src/utils/storage/transactions.js @@ -1,12 +1,12 @@ // @flow import { Map } from 'immutable' -import { load } from '~/utils/storage' +import { loadFromStorage } from '~/utils/storage' const getSubjectKeyFrom = (safeAddress: string) => `TXS-SUBJECTS-${safeAddress}` export const storeSubject = async (safeAddress: string, nonce: number, subject: string) => { const key = getSubjectKeyFrom(safeAddress) - const subjects = Map(await load(key)) || Map() + const subjects = Map(await loadFromStorage(key)) || Map() try { const updatedSubjects = subjects.set(nonce, subject) @@ -20,7 +20,7 @@ export const storeSubject = async (safeAddress: string, nonce: number, subject: export const loadSafeSubjects = (safeAddress: string): Map => { const key = getSubjectKeyFrom(safeAddress) - const data: any = load(key) + const data: any = loadFromStorage(key) return data ? Map(data) : Map() }