mirror of
https://github.com/trezor/trezor-wallet
synced 2025-01-28 00:41:08 +00:00
synchronized localStorage (tokens)
This commit is contained in:
parent
aca4a26372
commit
d7f56db291
@ -81,10 +81,17 @@ export const loadData = (): ThunkAction => {
|
||||
|
||||
export function loadTokensFromJSON(): AsyncAction {
|
||||
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||
|
||||
if (typeof window.localStorage === 'undefined') return;
|
||||
|
||||
try {
|
||||
const config: Config = await httpRequest('data/appConfig.json', 'json');
|
||||
const ERC20Abi = await httpRequest('data/ERC20Abi.json', 'json');
|
||||
|
||||
window.addEventListener('storage', event => {
|
||||
dispatch( update(event) );
|
||||
})
|
||||
|
||||
// load tokens
|
||||
const tokens = await config.coins.reduce(async (promise: Promise<TokensCollection>, coin: Coin): Promise<TokensCollection> => {
|
||||
const collection: TokensCollection = await promise;
|
||||
@ -150,6 +157,17 @@ export function loadTokensFromJSON(): AsyncAction {
|
||||
}
|
||||
}
|
||||
|
||||
export function update(event: StorageEvent): AsyncAction {
|
||||
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||
if (event.key === 'tokens' && event.newValue) {
|
||||
dispatch({
|
||||
type: TOKEN.FROM_STORAGE,
|
||||
payload: JSON.parse(event.newValue)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const save = (key: string, value: string): ThunkAction => {
|
||||
return (dispatch: Dispatch, getState: GetState): void => {
|
||||
@ -165,12 +183,13 @@ export const save = (key: string, value: string): ThunkAction => {
|
||||
}
|
||||
|
||||
export const get = (key: string): ?string => {
|
||||
if (typeof window.localStorage !== 'undefined') {
|
||||
try {
|
||||
return window.localStorage.getItem(key);
|
||||
} catch (error) {
|
||||
// available = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
return window.localStorage.getItem(key);
|
||||
} catch (error) {
|
||||
// available = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user