Browse Source

Export drizzleMWs, add account subscribing support

develop
Ezerous 4 years ago
parent
commit
dd8333a657
  1. 11
      src/accounts/accountsActions.js
  2. 22
      src/accounts/accountsMiddleware.js
  3. 52
      src/accounts/accountsSaga.js
  4. 3
      src/accounts/constants.js
  5. 9
      src/drizzleStatus/drizzleStatusSaga.js
  6. 22
      src/index.js

11
src/accounts/accountsActions.js

@ -3,20 +3,25 @@ import * as AccountsActions from './constants'
export function accountsFetching (results) { export function accountsFetching (results) {
return { return {
type: AccountsActions.ACCOUNTS_FETCHING, type: AccountsActions.ACCOUNTS_FETCHING,
payload: results
} }
} }
export function accountsFetched (results) { export function accountsFetched (results) {
return { return {
type: AccountsActions.ACCOUNTS_FETCHED, type: AccountsActions.ACCOUNTS_FETCHED,
payload: results accounts: results
}
}
export function accountsListening (results) {
return {
type: AccountsActions.ACCOUNTS_LISTENING,
} }
} }
export function accountsFailed (error) { export function accountsFailed (error) {
return { return {
type: AccountsActions.ACCOUNTS_FAILED, type: AccountsActions.ACCOUNTS_FAILED,
payload: error error
} }
} }

22
src/accounts/accountsMiddleware.js

@ -0,0 +1,22 @@
import { WEB3_INITIALIZED } from '../web3/constants'
import { accountsFetched, accountsListening } from './accountsActions'
export const accountsMiddleware = () => store => next => action => {
const { type } = action
if (type === WEB3_INITIALIZED) {
if(!window.ethereum)
console.warn('No Metamask detected, not subscribed to account changes!')
else {
store.dispatch(accountsListening());
window.ethereum.on('accountsChanged', function (accounts) {
console.debug("Account changed")
store.dispatch(accountsFetched(accounts));
});
}
}
return next(action)
}
const initializedMiddleware = accountsMiddleware(undefined)
export default initializedMiddleware

52
src/accounts/accountsSaga.js

@ -1,21 +1,18 @@
import { eventChannel } from 'redux-saga' import { call, put, takeLatest } from 'redux-saga/effects'
import { call, put, take, takeLatest } from 'redux-saga/effects'
import { getAccountBalances } from '../accountBalances/accountBalancesSaga'
import * as AccountsActions from './constants' import * as AccountsActions from './constants'
/* /*
* Fetch Accounts List * Fetch Accounts List
*/ */
export function * getAccounts (action) { export function * getAccounts (action) {
const web3 = action.web3 const web3 = action.web3
try { try {
const accounts = yield call(web3.eth.getAccounts) const accounts = yield call(web3.eth.getAccounts)
if (!accounts) { if (!accounts)
throw 'No accounts found!' throw 'No accounts found!'
}
yield put({ type: AccountsActions.ACCOUNTS_FETCHED, accounts }) yield put({ type: AccountsActions.ACCOUNTS_FETCHED, accounts })
} catch (error) { } catch (error) {
@ -25,51 +22,8 @@ export function * getAccounts (action) {
} }
} }
/*
* Poll for Account Changes
*/
function * createAccountsPollChannel ({ interval, web3 }) {
return eventChannel(emit => {
const persistedWeb3 = web3
const accountsPoller = setInterval(() => {
emit({ type: AccountsActions.SYNCING_ACCOUNTS, persistedWeb3 })
}, interval) // options.polls.accounts
const unsubscribe = () => {
clearInterval(accountsPoller)
}
return unsubscribe
})
}
function * callCreateAccountsPollChannel ({ interval, web3 }) {
const accountsChannel = yield call(createAccountsPollChannel, {
interval,
web3
})
try {
while (true) {
var event = yield take(accountsChannel)
if (event.type === AccountsActions.SYNCING_ACCOUNTS) {
yield call(getAccounts, { web3: event.persistedWeb3 })
yield call(getAccountBalances, { web3: event.persistedWeb3 })
}
yield put(event)
}
} finally {
accountsChannel.close()
}
}
function * accountsSaga () { function * accountsSaga () {
yield takeLatest(AccountsActions.ACCOUNTS_FETCHING, getAccounts) yield takeLatest(AccountsActions.ACCOUNTS_FETCHING, getAccounts)
yield takeLatest(AccountsActions.ACCOUNTS_POLLING, callCreateAccountsPollChannel)
} }
export default accountsSaga export default accountsSaga

3
src/accounts/constants.js

@ -1,5 +1,4 @@
export const ACCOUNTS_FETCHING = 'ACCOUNTS_FETCHING' export const ACCOUNTS_FETCHING = 'ACCOUNTS_FETCHING'
export const ACCOUNTS_FETCHED = 'ACCOUNTS_FETCHED' export const ACCOUNTS_FETCHED = 'ACCOUNTS_FETCHED'
export const ACCOUNTS_FAILED = 'ACCOUNTS_FAILED' export const ACCOUNTS_FAILED = 'ACCOUNTS_FAILED'
export const SYNCING_ACCOUNTS = 'SYNCING_ACCOUNTS' export const ACCOUNTS_LISTENING = 'ACCOUNTS_LISTENING'
export const ACCOUNTS_POLLING = 'ACCOUNTS_POLLING'

9
src/drizzleStatus/drizzleStatusSaga.js

@ -49,15 +49,6 @@ export function * initializeDrizzle (action) {
} }
yield put({ type: BlocksActions.BLOCKS_LISTENING, drizzle, web3 }) yield put({ type: BlocksActions.BLOCKS_LISTENING, drizzle, web3 })
// Accounts Polling
if ('accounts' in options.polls) {
yield put({
type: AccountsActions.ACCOUNTS_POLLING,
interval: options.polls.accounts,
web3
})
}
} }
} }
} catch (error) { } catch (error) {

22
src/index.js

@ -15,6 +15,17 @@ import transactionsReducer from './transactions/transactionsReducer'
import transactionStackReducer from './transactions/transactionStackReducer' import transactionStackReducer from './transactions/transactionStackReducer'
import web3Reducer from './web3/web3Reducer' import web3Reducer from './web3/web3Reducer'
// Middleware
import drizzleMiddleware from './drizzle-middleware'
import accountsMiddleware from './accounts/accountsMiddleware'
// Sagas
import accountsSaga from './accounts/accountsSaga'
import accountBalancesSaga from './accountBalances/accountBalancesSaga'
import blocksSaga from './blocks/blocksSaga'
import contractsSaga from './contracts/contractsSaga'
import drizzleStatusSaga from './drizzleStatus/drizzleStatusSaga'
const drizzleReducers = { const drizzleReducers = {
accounts: accountsReducer, accounts: accountsReducer,
accountBalances: accountBalancesReducer, accountBalances: accountBalancesReducer,
@ -26,12 +37,10 @@ const drizzleReducers = {
web3: web3Reducer web3: web3Reducer
} }
// Sagas const drizzleMiddlewares = [
import accountsSaga from './accounts/accountsSaga' drizzleMiddleware,
import accountBalancesSaga from './accountBalances/accountBalancesSaga' accountsMiddleware
import blocksSaga from './blocks/blocksSaga' ]
import contractsSaga from './contracts/contractsSaga'
import drizzleStatusSaga from './drizzleStatus/drizzleStatusSaga'
const drizzleSagas = [ const drizzleSagas = [
accountsSaga, accountsSaga,
@ -46,6 +55,7 @@ export {
generateContractsInitialState, generateContractsInitialState,
generateStore, generateStore,
drizzleReducers, drizzleReducers,
drizzleMiddlewares,
drizzleSagas, drizzleSagas,
EventActions EventActions
} }

Loading…
Cancel
Save