mirror of
https://github.com/trezor/trezor-wallet
synced 2025-01-29 17:31:00 +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 {
|
export function loadTokensFromJSON(): AsyncAction {
|
||||||
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||||
|
|
||||||
|
if (typeof window.localStorage === 'undefined') return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const config: Config = await httpRequest('data/appConfig.json', 'json');
|
const config: Config = await httpRequest('data/appConfig.json', 'json');
|
||||||
const ERC20Abi = await httpRequest('data/ERC20Abi.json', 'json');
|
const ERC20Abi = await httpRequest('data/ERC20Abi.json', 'json');
|
||||||
|
|
||||||
|
window.addEventListener('storage', event => {
|
||||||
|
dispatch( update(event) );
|
||||||
|
})
|
||||||
|
|
||||||
// load tokens
|
// load tokens
|
||||||
const tokens = await config.coins.reduce(async (promise: Promise<TokensCollection>, coin: Coin): Promise<TokensCollection> => {
|
const tokens = await config.coins.reduce(async (promise: Promise<TokensCollection>, coin: Coin): Promise<TokensCollection> => {
|
||||||
const collection: TokensCollection = await promise;
|
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 => {
|
export const save = (key: string, value: string): ThunkAction => {
|
||||||
return (dispatch: Dispatch, getState: GetState): void => {
|
return (dispatch: Dispatch, getState: GetState): void => {
|
||||||
@ -165,12 +183,13 @@ export const save = (key: string, value: string): ThunkAction => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const get = (key: string): ?string => {
|
export const get = (key: string): ?string => {
|
||||||
if (typeof window.localStorage !== 'undefined') {
|
|
||||||
try {
|
|
||||||
return window.localStorage.getItem(key);
|
try {
|
||||||
} catch (error) {
|
return window.localStorage.getItem(key);
|
||||||
// available = false;
|
} catch (error) {
|
||||||
return null;
|
// available = false;
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user