Browse Source

Reload on network change

develop
Ezerous 4 years ago
parent
commit
96893ae3a5
  1. 2
      package.json
  2. 6
      src/Drizzle.js
  3. 9
      src/drizzle-middleware.js
  4. 2
      src/web3/web3Actions.js
  5. 11
      src/web3/web3Middleware.js
  6. 2
      src/web3/web3Saga.js

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@ezerous/drizzle", "name": "@ezerous/drizzle",
"version": "0.2.0", "version": "0.2.1",
"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>",

6
src/Drizzle.js

@ -81,6 +81,12 @@ class Drizzle {
events events
) )
if (this.contracts[drizzleContract.contractName]) {
throw new Error(
`Contract already exists: ${drizzleContract.contractName}`
)
}
this.store.dispatch({ type: ContractActions.CONTRACT_INITIALIZING, contractConfig }) this.store.dispatch({ type: ContractActions.CONTRACT_INITIALIZING, contractConfig })
this.contracts[drizzleContract.contractName] = drizzleContract this.contracts[drizzleContract.contractName] = drizzleContract

9
src/drizzle-middleware.js

@ -1,7 +1,6 @@
import * as DrizzleActions from './drizzleStatus/drizzleActions' import * as DrizzleActions from './drizzleStatus/drizzleActions'
import * as ContractActions from './contracts/constants' import * as ContractActions from './contracts/constants'
import { ACCOUNTS_FETCHED } from './accounts/accountsActions' import { ACCOUNTS_FETCHED } from './accounts/accountsActions'
import { NETWORK_ID_CHANGED } from './web3/web3Actions'
export const drizzleMiddleware = drizzleInstance => store => next => action => { export const drizzleMiddleware = drizzleInstance => store => next => action => {
const { type } = action const { type } = action
@ -10,14 +9,6 @@ export const drizzleMiddleware = drizzleInstance => store => next => action => {
drizzleInstance = action.drizzle drizzleInstance = action.drizzle
} }
if (type === NETWORK_ID_CHANGED) {
store.dispatch({
type: DrizzleActions.DRIZZLE_INITIALIZING,
drizzle: drizzleInstance,
options: drizzleInstance.options
})
}
if ( if (
type === ACCOUNTS_FETCHED && type === ACCOUNTS_FETCHED &&
drizzleInstance && drizzleInstance &&

2
src/web3/web3Actions.js

@ -3,7 +3,6 @@ export const WEB3_INITIALIZED = 'WEB3_INITIALIZED'
export const WEB3_FAILED = 'WEB3_FAILED' export const WEB3_FAILED = 'WEB3_FAILED'
export const WEB3_USER_DENIED = 'WEB3_USER_DENIED' export const WEB3_USER_DENIED = 'WEB3_USER_DENIED'
export const NETWORK_ID_FETCHING = 'NETWORK_ID_FETCHING'
export const NETWORK_ID_FETCHED = 'NETWORK_ID_FETCHED' export const NETWORK_ID_FETCHED = 'NETWORK_ID_FETCHED'
export const NETWORK_ID_CHANGED = 'NETWORK_ID_CHANGED' export const NETWORK_ID_CHANGED = 'NETWORK_ID_CHANGED'
export const NETWORK_ID_FAILED = 'NETWORK_ID_FAILED' export const NETWORK_ID_FAILED = 'NETWORK_ID_FAILED'
@ -21,7 +20,6 @@ export const NETWORK_IDS = {
export function networkIdChanged (web3, networkId) { export function networkIdChanged (web3, networkId) {
return { return {
type: NETWORK_ID_CHANGED, type: NETWORK_ID_CHANGED,
web3,
networkId networkId
} }
} }

11
src/web3/web3Middleware.js

@ -1,23 +1,24 @@
import { networkIdChanged, WEB3_INITIALIZED } from './web3Actions' import { networkIdChanged, WEB3_INITIALIZED } from './web3Actions'
export const web3Middleware = web3 => store => next => action => { export const web3Middleware = () => store => next => action => {
const { type } = action const { type } = action
if (type === WEB3_INITIALIZED) { if (type === WEB3_INITIALIZED) {
if(!window.ethereum) if(!window.ethereum)
console.warn('No Metamask detected, not subscribed to network changes!') console.warn('No Metamask detected, not subscribed to network changes!')
else { else {
web3 = action.web3;
window.ethereum.on('networkChanged', (networkId) => { window.ethereum.on('networkChanged', (networkId) => {
// Warning: 'networkChanged' is deprecated (EIP-1193) // Warning: 'networkChanged' is deprecated (EIP-1193)
const storedNetworkId = store.getState().web3.networkId; const storedNetworkId = store.getState().web3.networkId;
if(storedNetworkId && networkId !== storedNetworkId) if(storedNetworkId && (networkId !== storedNetworkId)){
store.dispatch(networkIdChanged(web3, networkId)); store.dispatch(networkIdChanged(networkId)); // Just to be typical
window.location.reload();
}
}); });
} }
} }
return next(action) return next(action)
} }
const initializedMiddleware = web3Middleware(undefined) const initializedMiddleware = web3Middleware()
export default initializedMiddleware export default initializedMiddleware

2
src/web3/web3Saga.js

@ -14,7 +14,7 @@ export function * initializeWeb3 (options) {
const { ethereum } = window const { ethereum } = window
web3 = options.customProvider || new Web3(ethereum); web3 = options.customProvider || new Web3(ethereum);
try { try {
yield call([ethereum, 'request'], { method: 'eth_requestAccounts' }) yield call([ethereum, 'request'], { method: 'eth_requestAccounts' });
yield put({ type: Action.WEB3_INITIALIZED, web3 }) yield put({ type: Action.WEB3_INITIALIZED, web3 })

Loading…
Cancel
Save