From 331446d4722942f19158120b6d37a2aa23b656ac Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 9 Sep 2020 17:05:11 +0300 Subject: [PATCH] Redux improvements --- package.json | 2 +- src/accounts/accountsMiddleware.js | 7 ++++--- src/accounts/accountsReducer.js | 6 +----- src/drizzle/drizzleStatusReducer.js | 25 +++++++++++++++++++++++-- src/index.js | 4 +++- src/web3/web3Actions.js | 7 +++++++ src/web3/web3Middleware.js | 8 +++++--- src/web3/web3Reducer.js | 16 +++++++++++++--- src/web3/web3Saga.js | 1 + 9 files changed, 58 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index d21bc50..4cb3642 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ezerous/drizzle", - "version": "0.3.0", + "version": "0.4.0", "description": "A reactive data-store for web3 and smart contracts.", "license": "MIT", "author": "Ezerous ", diff --git a/src/accounts/accountsMiddleware.js b/src/accounts/accountsMiddleware.js index 2e5b3fa..20103e3 100644 --- a/src/accounts/accountsMiddleware.js +++ b/src/accounts/accountsMiddleware.js @@ -1,5 +1,5 @@ -import { networkIdChanged, WEB3_INITIALIZED } from '../web3/web3Actions' -import { accountsChanged, accountsFetched, accountsListening } from './accountsActions' +import { WEB3_INITIALIZED } from '../web3/web3Actions' +import { accountsChanged, accountsListening } from './accountsActions' export const accountsMiddleware = web3 => store => next => action => { const { type } = action @@ -15,7 +15,8 @@ export const accountsMiddleware = web3 => store => next => action => { patchedAccounts.forEach((account, i) => patchedAccounts[i] = web3.utils.toChecksumAddress(account)); const storedAccounts = store.getState().accounts; - if(storedAccounts[0] && (patchedAccounts[0] !== storedAccounts[0])) + const accountsFailed = store.getState().web3.accountsFailed; + if((storedAccounts[0] || accountsFailed) && (patchedAccounts[0] !== storedAccounts[0])) store.dispatch(accountsChanged(patchedAccounts)); }); store.dispatch(accountsListening()); diff --git a/src/accounts/accountsReducer.js b/src/accounts/accountsReducer.js index d292885..ef4aa5a 100644 --- a/src/accounts/accountsReducer.js +++ b/src/accounts/accountsReducer.js @@ -1,12 +1,8 @@ -import { ACCOUNTS_CHANGED, ACCOUNTS_FETCHED, ACCOUNTS_FETCHING } from './accountsActions' +import { ACCOUNTS_CHANGED, ACCOUNTS_FETCHED } from './accountsActions' const initialState = {} const accountsReducer = (state = initialState, action) => { - if (action.type === ACCOUNTS_FETCHING) { - return state - } - if (action.type === ACCOUNTS_FETCHED || action.type === ACCOUNTS_CHANGED) { return Object.assign({}, state, action.accounts) } diff --git a/src/drizzle/drizzleStatusReducer.js b/src/drizzle/drizzleStatusReducer.js index f30a768..fc75588 100644 --- a/src/drizzle/drizzleStatusReducer.js +++ b/src/drizzle/drizzleStatusReducer.js @@ -1,7 +1,9 @@ import * as DrizzleActions from './drizzleActions' const initialState = { - initialized: false + initializing: true, + initialized: false, + failed: false } const drizzleStatusReducer = (state = initialState, action) => { @@ -9,12 +11,31 @@ const drizzleStatusReducer = (state = initialState, action) => { * Drizzle Status */ + if (action.type === DrizzleActions.DRIZZLE_INITIALIZING) { + return { + ...state, + ...initialState + } + } + if (action.type === DrizzleActions.DRIZZLE_INITIALIZED) { return { ...state, - initialized: true + initializing: false, + initialized: true, + failed: false } } + + if (action.type === DrizzleActions.DRIZZLE_FAILED) { + return { + ...state, + initializing: false, + initialized: false, + failed: true + } + } + return state } diff --git a/src/index.js b/src/index.js index 06f3d56..069a8ad 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,9 @@ import { generateStore } from './generateStore' import { generateContractsInitialState } from './contractStateUtils' // Actions -import * as EventActions from './contracts/constants' import * as AccountActions from './accounts/accountsActions' +import * as DrizzleActions from './drizzle/drizzleActions' +import * as EventActions from './contracts/constants' // Reducers import drizzleReducers from './root/rootReducer' @@ -17,6 +18,7 @@ import drizzleSagas from './root/rootSaga' const drizzleActions = { account: AccountActions, + drizzle: DrizzleActions, event: EventActions } diff --git a/src/web3/web3Actions.js b/src/web3/web3Actions.js index c56259f..6f3a0c6 100644 --- a/src/web3/web3Actions.js +++ b/src/web3/web3Actions.js @@ -7,6 +7,7 @@ export const WEB3_NETWORK_FETCHING = 'WEB3_NETWORK_FETCHING' export const WEB3_NETWORK_FETCHED = 'WEB3_NETWORK_FETCHED' export const WEB3_NETWORK_CHANGED = 'WEB3_NETWORK_CHANGED' export const WEB3_NETWORK_FAILED = 'WEB3_NETWORK_FAILED' +export const WEB3_NETWORK_LISTENING = 'WEB3_NETWORK_LISTENING' export const WEB3_NETWORK_MISMATCH = 'WEB3_NETWORK_MISMATCH' export const NETWORK_IDS = { @@ -30,3 +31,9 @@ export function networkChanged () { type: WEB3_NETWORK_CHANGED } } + +export function networkListening () { + return { + type: WEB3_NETWORK_LISTENING + } +} diff --git a/src/web3/web3Middleware.js b/src/web3/web3Middleware.js index abb2636..694cda2 100644 --- a/src/web3/web3Middleware.js +++ b/src/web3/web3Middleware.js @@ -1,4 +1,4 @@ -import { networkChanged, networkInfoFetching, WEB3_INITIALIZED } from './web3Actions' +import { networkChanged, networkInfoFetching, networkListening, WEB3_INITIALIZED } from './web3Actions' export const web3Middleware = web3 => store => next => action => { const { type } = action @@ -9,12 +9,14 @@ export const web3Middleware = web3 => store => next => action => { else { web3 = action.web3; window.ethereum.on('chainChanged', (chainId) => { - const storedNetworkId = store.getState().web3.chainId; - if(storedNetworkId && (chainId !== storedNetworkId)){ + const storedChainId = store.getState().web3.chainId; + const networkFailed = store.getState().web3.networkFailed; + if((storedChainId || networkFailed) && (chainId !== storedChainId)){ store.dispatch(networkChanged()); store.dispatch(networkInfoFetching(web3)); } }); + store.dispatch(networkListening()); } } return next(action) diff --git a/src/web3/web3Reducer.js b/src/web3/web3Reducer.js index c0fc39f..9344cfa 100644 --- a/src/web3/web3Reducer.js +++ b/src/web3/web3Reducer.js @@ -1,7 +1,8 @@ import * as Action from './web3Actions' +import { ACCOUNTS_FAILED } from '../accounts/accountsActions' const initialState = { - status: '' + status: 'initializing' } const web3Reducer = (state = initialState, action) => { @@ -38,14 +39,16 @@ const web3Reducer = (state = initialState, action) => { ...state, networkId: action.networkInfo.networkId, chainId: action.networkInfo.chainId, - nodeInfo: action.networkInfo.nodeInfo + nodeInfo: action.networkInfo.nodeInfo, + networkFailed: false, + networkMismatch: false } } if (action.type === Action.WEB3_NETWORK_FAILED) { return { ...state, - networkId: action.networkId + networkFailed: true } } if (action.type === Action.WEB3_NETWORK_MISMATCH) { @@ -55,6 +58,13 @@ const web3Reducer = (state = initialState, action) => { } } + if (action.type === ACCOUNTS_FAILED) { + return { + ...state, + accountsFailed: true + } + } + return state } diff --git a/src/web3/web3Saga.js b/src/web3/web3Saga.js index c9ffe5a..8f54fb9 100644 --- a/src/web3/web3Saga.js +++ b/src/web3/web3Saga.js @@ -8,6 +8,7 @@ const Web3 = require('web3'); */ export function * initializeWeb3 (options) { try { + yield put({ type: Action.WEB3_INITIALIZING }); let web3 = {} if (window.ethereum) {