Browse Source

Redux improvements

develop
Ezerous 4 years ago
parent
commit
331446d472
  1. 2
      package.json
  2. 7
      src/accounts/accountsMiddleware.js
  3. 6
      src/accounts/accountsReducer.js
  4. 25
      src/drizzle/drizzleStatusReducer.js
  5. 4
      src/index.js
  6. 7
      src/web3/web3Actions.js
  7. 8
      src/web3/web3Middleware.js
  8. 16
      src/web3/web3Reducer.js
  9. 1
      src/web3/web3Saga.js

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@ezerous/drizzle", "name": "@ezerous/drizzle",
"version": "0.3.0", "version": "0.4.0",
"description": "A reactive data-store for web3 and smart contracts.", "description": "A reactive data-store for web3 and smart contracts.",
"license": "MIT", "license": "MIT",
"author": "Ezerous <ezerous@gmail.com>", "author": "Ezerous <ezerous@gmail.com>",

7
src/accounts/accountsMiddleware.js

@ -1,5 +1,5 @@
import { networkIdChanged, WEB3_INITIALIZED } from '../web3/web3Actions' import { WEB3_INITIALIZED } from '../web3/web3Actions'
import { accountsChanged, accountsFetched, accountsListening } from './accountsActions' import { accountsChanged, accountsListening } from './accountsActions'
export const accountsMiddleware = web3 => store => next => action => { export const accountsMiddleware = web3 => store => next => action => {
const { type } = 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)); patchedAccounts.forEach((account, i) => patchedAccounts[i] = web3.utils.toChecksumAddress(account));
const storedAccounts = store.getState().accounts; 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(accountsChanged(patchedAccounts));
}); });
store.dispatch(accountsListening()); store.dispatch(accountsListening());

6
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 initialState = {}
const accountsReducer = (state = initialState, action) => { const accountsReducer = (state = initialState, action) => {
if (action.type === ACCOUNTS_FETCHING) {
return state
}
if (action.type === ACCOUNTS_FETCHED || action.type === ACCOUNTS_CHANGED) { if (action.type === ACCOUNTS_FETCHED || action.type === ACCOUNTS_CHANGED) {
return Object.assign({}, state, action.accounts) return Object.assign({}, state, action.accounts)
} }

25
src/drizzle/drizzleStatusReducer.js

@ -1,7 +1,9 @@
import * as DrizzleActions from './drizzleActions' import * as DrizzleActions from './drizzleActions'
const initialState = { const initialState = {
initialized: false initializing: true,
initialized: false,
failed: false
} }
const drizzleStatusReducer = (state = initialState, action) => { const drizzleStatusReducer = (state = initialState, action) => {
@ -9,12 +11,31 @@ const drizzleStatusReducer = (state = initialState, action) => {
* Drizzle Status * Drizzle Status
*/ */
if (action.type === DrizzleActions.DRIZZLE_INITIALIZING) {
return {
...state,
...initialState
}
}
if (action.type === DrizzleActions.DRIZZLE_INITIALIZED) { if (action.type === DrizzleActions.DRIZZLE_INITIALIZED) {
return { return {
...state, ...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 return state
} }

4
src/index.js

@ -3,8 +3,9 @@ import { generateStore } from './generateStore'
import { generateContractsInitialState } from './contractStateUtils' import { generateContractsInitialState } from './contractStateUtils'
// Actions // Actions
import * as EventActions from './contracts/constants'
import * as AccountActions from './accounts/accountsActions' import * as AccountActions from './accounts/accountsActions'
import * as DrizzleActions from './drizzle/drizzleActions'
import * as EventActions from './contracts/constants'
// Reducers // Reducers
import drizzleReducers from './root/rootReducer' import drizzleReducers from './root/rootReducer'
@ -17,6 +18,7 @@ import drizzleSagas from './root/rootSaga'
const drizzleActions = { const drizzleActions = {
account: AccountActions, account: AccountActions,
drizzle: DrizzleActions,
event: EventActions event: EventActions
} }

7
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_FETCHED = 'WEB3_NETWORK_FETCHED'
export const WEB3_NETWORK_CHANGED = 'WEB3_NETWORK_CHANGED' export const WEB3_NETWORK_CHANGED = 'WEB3_NETWORK_CHANGED'
export const WEB3_NETWORK_FAILED = 'WEB3_NETWORK_FAILED' 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 WEB3_NETWORK_MISMATCH = 'WEB3_NETWORK_MISMATCH'
export const NETWORK_IDS = { export const NETWORK_IDS = {
@ -30,3 +31,9 @@ export function networkChanged () {
type: WEB3_NETWORK_CHANGED type: WEB3_NETWORK_CHANGED
} }
} }
export function networkListening () {
return {
type: WEB3_NETWORK_LISTENING
}
}

8
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 => { export const web3Middleware = web3 => store => next => action => {
const { type } = action const { type } = action
@ -9,12 +9,14 @@ export const web3Middleware = web3 => store => next => action => {
else { else {
web3 = action.web3; web3 = action.web3;
window.ethereum.on('chainChanged', (chainId) => { window.ethereum.on('chainChanged', (chainId) => {
const storedNetworkId = store.getState().web3.chainId; const storedChainId = store.getState().web3.chainId;
if(storedNetworkId && (chainId !== storedNetworkId)){ const networkFailed = store.getState().web3.networkFailed;
if((storedChainId || networkFailed) && (chainId !== storedChainId)){
store.dispatch(networkChanged()); store.dispatch(networkChanged());
store.dispatch(networkInfoFetching(web3)); store.dispatch(networkInfoFetching(web3));
} }
}); });
store.dispatch(networkListening());
} }
} }
return next(action) return next(action)

16
src/web3/web3Reducer.js

@ -1,7 +1,8 @@
import * as Action from './web3Actions' import * as Action from './web3Actions'
import { ACCOUNTS_FAILED } from '../accounts/accountsActions'
const initialState = { const initialState = {
status: '' status: 'initializing'
} }
const web3Reducer = (state = initialState, action) => { const web3Reducer = (state = initialState, action) => {
@ -38,14 +39,16 @@ const web3Reducer = (state = initialState, action) => {
...state, ...state,
networkId: action.networkInfo.networkId, networkId: action.networkInfo.networkId,
chainId: action.networkInfo.chainId, chainId: action.networkInfo.chainId,
nodeInfo: action.networkInfo.nodeInfo nodeInfo: action.networkInfo.nodeInfo,
networkFailed: false,
networkMismatch: false
} }
} }
if (action.type === Action.WEB3_NETWORK_FAILED) { if (action.type === Action.WEB3_NETWORK_FAILED) {
return { return {
...state, ...state,
networkId: action.networkId networkFailed: true
} }
} }
if (action.type === Action.WEB3_NETWORK_MISMATCH) { 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 return state
} }

1
src/web3/web3Saga.js

@ -8,6 +8,7 @@ const Web3 = require('web3');
*/ */
export function * initializeWeb3 (options) { export function * initializeWeb3 (options) {
try { try {
yield put({ type: Action.WEB3_INITIALIZING });
let web3 = {} let web3 = {}
if (window.ethereum) { if (window.ethereum) {

Loading…
Cancel
Save