save active tokens via immortaldb

This commit is contained in:
Mikhail Mikheev 2019-03-27 19:30:32 +04:00
parent 7e150a2ff3
commit 633f8738b6
6 changed files with 42 additions and 24 deletions

View File

@ -3,14 +3,14 @@ import { Map, List } from 'immutable'
import { createAction } from 'redux-actions'
import type { Dispatch as ReduxDispatch } from 'redux'
import { type Token } from '~/logic/tokens/store/model/token'
import { ensureOnce } from '~/utils/singleton'
import { ensureOnceAsync } from '~/utils/singleton'
import { type GlobalState } from '~/store/index'
import { setActiveTokenAddresses } from '~/logic/tokens/utils/tokensStorage'
import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelpers'
export const ADD_TOKENS = 'ADD_TOKENS'
const setTokensOnce = ensureOnce(setActiveTokenAddresses)
const setTokensOnce = ensureOnceAsync(setActiveTokenAddresses)
type TokenProps = {
safeAddress: string,
@ -25,11 +25,13 @@ const addTokens = createAction(
}),
)
const saveTokens = (safeAddress: string, tokens: Map<string, Token>) => (dispatch: ReduxDispatch<GlobalState>) => {
const saveTokens = (safeAddress: string, tokens: Map<string, Token>) => async (
dispatch: ReduxDispatch<GlobalState>,
) => {
dispatch(addTokens(safeAddress, tokens))
const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
setTokensOnce(safeAddress, activeAddresses)
await setTokensOnce(safeAddress, activeAddresses)
}
export default saveTokens

View File

@ -7,19 +7,16 @@ import { removeFromActiveTokens } from '~/logic/tokens/utils/tokensStorage'
export const DISABLE_TOKEN = 'DISABLE_TOKEN'
const disableToken = createAction(
DISABLE_TOKEN,
(safeAddress: string, tokenAddress: string) => ({
const disableToken = createAction(DISABLE_TOKEN, (safeAddress: string, tokenAddress: string) => ({
safeAddress,
tokenAddress,
}),
)
}))
const hideToken = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
const hideToken = (safeAddress: string, token: Token) => async (dispatch: ReduxDispatch<GlobalState>) => {
const { address } = token
dispatch(disableToken(safeAddress, address))
removeFromActiveTokens(safeAddress, address)
await removeFromActiveTokens(safeAddress, address)
}
export default hideToken

View File

@ -12,12 +12,12 @@ const enableToken = createAction(ENABLE_TOKEN, (safeAddress: string, tokenAddres
tokenAddress,
}))
const setTokenEnabled = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
const setTokenEnabled = (safeAddress: string, token: Token) => async (dispatch: ReduxDispatch<GlobalState>) => {
const { address } = token
dispatch(enableToken(safeAddress, address))
const activeTokens = getActiveTokenAddresses(safeAddress)
setActiveTokenAddresses(safeAddress, activeTokens.push(address))
const activeTokens = await getActiveTokenAddresses(safeAddress)
await setActiveTokenAddresses(safeAddress, activeTokens.push(address))
}
export default setTokenEnabled

View File

@ -20,12 +20,12 @@ const removeToken = createAction(
}),
)
const deleteToken = (safeAddress: string, token: Token) => (dispatch: ReduxDispatch<GlobalState>) => {
const deleteToken = (safeAddress: string, token: Token) => async (dispatch: ReduxDispatch<GlobalState>) => {
dispatch(removeToken(safeAddress, token))
const tokenAddress = token.get('address')
removeFromActiveTokens(safeAddress, tokenAddress)
removeTokenFromStorage(safeAddress, token)
await removeFromActiveTokens(safeAddress, tokenAddress)
await removeTokenFromStorage(safeAddress, token)
}
export default deleteToken

View File

@ -24,14 +24,14 @@ export const getActiveTokenAddresses = async (safeAddress: string): Promise<List
const key = getActiveTokensKey(safeAddress)
const data = await ImmortalDB.get(key)
return data ? List(data) : List()
return data ? List(JSON.parse(data)) : List()
}
export const getTokens = async (safeAddress: string): Promise<List<TokenProps>> => {
const key = getTokensKey(safeAddress)
const data = await ImmortalDB.get(key)
return data ? List(data) : List()
return data ? List(JSON.parse(data)) : List()
}
export const setToken = async (safeAddress: string, token: Token) => {
@ -40,7 +40,7 @@ export const setToken = async (safeAddress: string, token: Token) => {
try {
const serializedState = JSON.stringify(data.push(token))
const key = getTokensKey(safeAddress)
ImmortalDB.set(key, serializedState)
await ImmortalDB.set(key, serializedState)
} catch (err) {
// eslint-disable-next-line
console.log('Error adding token in localstorage')
@ -64,5 +64,5 @@ export const removeTokenFromStorage = async (safeAddress: string, token: Token)
export const removeFromActiveTokens = async (safeAddress: string, tokenAddress: string) => {
const activeTokens = await getActiveTokenAddresses(safeAddress)
const index = activeTokens.indexOf(tokenAddress)
setActiveTokenAddresses(safeAddress, activeTokens.delete(index))
await setActiveTokenAddresses(safeAddress, activeTokens.delete(index))
}

View File

@ -4,7 +4,9 @@ export const ensureOnce = (fn: Function): Function => {
let response
return (...args) => {
if (executed) { return response }
if (executed) {
return response
}
executed = true
// eslint-disable-next-line
@ -13,3 +15,20 @@ export const ensureOnce = (fn: Function): Function => {
return response
}
}
export const ensureOnceAsync = (fn: Function): Function => {
let executed = false
let response
return async (...args) => {
if (executed) {
return response
}
executed = true
// eslint-disable-next-line
response = await fn.apply(undefined, args)
return response
}
}