From 1edb3933b989ca852eee7d3cfe120ba8d99c4e09 Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Wed, 29 May 2019 17:05:05 +0200 Subject: [PATCH] Apps: use consistent style across WebWorker scripts (#876) --- apps/finance/app/src/script.js | 56 ++++++++++++++-------------- apps/token-manager/app/src/script.js | 15 ++++---- apps/voting/app/src/script.js | 4 +- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/apps/finance/app/src/script.js b/apps/finance/app/src/script.js index 3de173aa80..90e4c1a525 100644 --- a/apps/finance/app/src/script.js +++ b/apps/finance/app/src/script.js @@ -115,43 +115,41 @@ async function initialize(vaultAddress, ethAddress) { async (state, event) => { const { vault } = settings const { address: eventAddress, event: eventName } = event - let nextState = { + const nextState = { ...state, } + if (event === events.SYNC_STATUS_SYNCING) { + return { ...nextState, isSyncing: true } + } else if (event === events.SYNC_STATUS_SYNCED) { + return { ...nextState, isSyncing: false } + } + + // Vault event if (addressesEqual(eventAddress, vault.address)) { - // Vault event - nextState = await vaultLoadBalance(nextState, event, settings) - } else { - // Finance event - switch (eventName) { - case events.SYNC_STATUS_SYNCING: - nextState.isSyncing = true - break - case events.SYNC_STATUS_SYNCED: - nextState.isSyncing = false - break - case 'ChangePeriodDuration': - nextState.periodDuration = marshallDate( - event.returnValues.newDuration - ) - break - case 'NewPeriod': + return vaultLoadBalance(nextState, event, settings) + } + + // Finance event + switch (eventName) { + case 'ChangePeriodDuration': + nextState.periodDuration = marshallDate( + event.returnValues.newDuration + ) + return nextState + case 'NewPeriod': + return { + ...(await newPeriod(nextState, event, settings)), // A new period is always started as part of the Finance app's initialization, // so this is just a handy way to get information about the app we're running // (e.g. its own address) - nextState.proxyAddress = eventAddress - nextState = await newPeriod(nextState, event, settings) - break - case 'NewTransaction': - nextState = await newTransaction(nextState, event, settings) - break - default: - break - } + proxyAddress: eventAddress, + } + case 'NewTransaction': + return newTransaction(nextState, event, settings) + default: + return nextState } - - return nextState }, { init: initializeState(settings), diff --git a/apps/token-manager/app/src/script.js b/apps/token-manager/app/src/script.js index 102dbf5c61..a123371e6f 100644 --- a/apps/token-manager/app/src/script.js +++ b/apps/token-manager/app/src/script.js @@ -47,17 +47,18 @@ retryEvery(retry => { async function initialize(tokenAddress) { const token = app.external(tokenAddress, tokenAbi) - async function reducer(state, { address, event, returnValues }) { - let nextState = { + function reducer(state, { address, event, returnValues }) { + const nextState = { ...state, } if (event === events.SYNC_STATUS_SYNCING) { - nextState.isSyncing = true + return { ...nextState, isSyncing: true } } else if (event === events.SYNC_STATUS_SYNCED) { - nextState.isSyncing = false + return { ...nextState, isSyncing: false } } + // Token event if (addressesEqual(address, tokenAddress)) { switch (event) { case 'ClaimedTokens': @@ -70,11 +71,11 @@ async function initialize(tokenAddress) { default: return nextState } - } else { - // Token Manager event - // TODO: add handlers for the vesting events from token Manager } + // Token Manager event + // TODO: add handlers for the vesting events from token Manager + return nextState } diff --git a/apps/voting/app/src/script.js b/apps/voting/app/src/script.js index 93b738661c..76483f4928 100644 --- a/apps/voting/app/src/script.js +++ b/apps/voting/app/src/script.js @@ -57,8 +57,8 @@ retryEvery(retry => { async function initialize(tokenAddr) { return app.store( - async (state, { event, returnValues }) => { - let nextState = { + (state, { event, returnValues }) => { + const nextState = { ...state, }