add loadFromStorage funC

This commit is contained in:
Mikhail Mikheev 2019-04-03 17:43:49 +04:00
parent 68e98922c3
commit 01baabc7cb
7 changed files with 28 additions and 18 deletions

View File

@ -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<Owner>) => {
}
export const getOwners = async (safeAddress: string): Map<string, string> => {
const data: Object = await load(`${OWNERS_KEY}-${safeAddress}`)
const data: Object = await loadFromStorage(`${OWNERS_KEY}-${safeAddress}`)
return data ? Map(data) : Map()
}

View File

@ -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<TokenPro
export const getActiveTokens = async (safeAddress: string): Promise<List<TokenProps>> => {
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<List<TokenProps>> => {
const key = getTokensKey(safeAddress)
const data = await load(key)
const data = await loadFromStorage(key)
return data ? List(data) : List()
}

View File

@ -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)

View File

@ -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<string, Safe> => {
}
export const safesInitialState = (): State => {
const storedSafes = load(SAFES_KEY)
const storedSafes = loadFromStorage(SAFES_KEY)
const safes = storedSafes ? buildSafesFrom(storedSafes) : Map()
return safes

View File

@ -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)
}
}

View File

@ -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)) || ''

View File

@ -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<string, string> => {
const key = getSubjectKeyFrom(safeAddress)
const data: any = load(key)
const data: any = loadFromStorage(key)
return data ? Map(data) : Map()
}