Browse Source

Removed legacy block polling

develop
Ezerous 4 years ago
parent
commit
7f5ab530bb
  1. 1
      package.json
  2. 79
      src/blocks/blocksSaga.js
  3. 2
      src/blocks/constants.js
  4. 3
      src/defaultOptions.js
  5. 14
      src/drizzleStatus/drizzleStatusSaga.js

1
package.json

@ -8,7 +8,6 @@
"repository": "github:Ezerous/drizzle", "repository": "github:Ezerous/drizzle",
"dependencies": { "dependencies": {
"deepmerge": "4.2.2", "deepmerge": "4.2.2",
"eth-block-tracker": "4.4.3",
"is-plain-object": "4.1.1", "is-plain-object": "4.1.1",
"redux": "4.0.5", "redux": "4.0.5",
"redux-saga": "1.1.3", "redux-saga": "1.1.3",

79
src/blocks/blocksSaga.js

@ -1,14 +1,11 @@
import { END, eventChannel } from 'redux-saga' import { END, eventChannel } from 'redux-saga'
import { call, put, take, takeEvery, takeLatest, all } from 'redux-saga/effects' import { call, put, take, takeEvery, takeLatest, all } from 'redux-saga/effects'
import PollingBlockTracker from 'eth-block-tracker'
import * as BlocksActions from './constants' import * as BlocksActions from './constants'
import * as ContractActions from '../contracts/constants'
/* /*
* Listen for Blocks * Listen for Blocks
*/ */
export function createBlockChannel ({ drizzle, web3 }) {
export function createBlockChannel ({ drizzle, web3, syncAlways }) {
return eventChannel(emit => { return eventChannel(emit => {
const blockEvents = web3.eth const blockEvents = web3.eth
.subscribe('newBlockHeaders', (error, result) => { .subscribe('newBlockHeaders', (error, result) => {
@ -22,7 +19,7 @@ export function createBlockChannel ({ drizzle, web3, syncAlways }) {
} }
}) })
.on('data', blockHeader => { .on('data', blockHeader => {
emit({ type: BlocksActions.BLOCK_RECEIVED, blockHeader, drizzle, web3, syncAlways }) emit({ type: BlocksActions.BLOCK_RECEIVED, blockHeader, drizzle, web3 })
}) })
.on('error', error => { .on('error', error => {
emit({ type: BlocksActions.BLOCKS_FAILED, error }) emit({ type: BlocksActions.BLOCKS_FAILED, error })
@ -37,71 +34,10 @@ export function createBlockChannel ({ drizzle, web3, syncAlways }) {
}) })
} }
function * callCreateBlockChannel ({ drizzle, web3, syncAlways }) { function * callCreateBlockChannel ({ drizzle, web3 }) {
const blockChannel = yield call(createBlockChannel, { const blockChannel = yield call(createBlockChannel, {
drizzle, drizzle,
web3, web3
syncAlways
})
try {
while (true) {
var event = yield take(blockChannel)
yield put(event)
}
} finally {
blockChannel.close()
}
}
/*
* Poll for Blocks
*/
export function createBlockPollChannel ({
drizzle,
interval,
web3,
syncAlways
}) {
return eventChannel(emit => {
const blockTracker = new PollingBlockTracker({
provider: web3.currentProvider,
pollingInterval: interval
})
blockTracker.on('block', block => {
emit({ type: BlocksActions.BLOCK_FOUND, block, drizzle, web3, syncAlways })
})
blockTracker.start().catch(error => {
emit({ type: BlocksActions.BLOCKS_FAILED, error })
emit(END)
})
const unsubscribe = () => {
blockTracker.stop().catch(_ => {
// PollingBlockTracker assumes there is an outstanding event subscription.
// However for our tests we start and stop a PollingBlockTracker in succession
// that triggers an error.
})
}
return unsubscribe
})
}
function * callCreateBlockPollChannel ({
drizzle,
interval,
web3,
syncAlways
}) {
const blockChannel = yield call(createBlockPollChannel, {
drizzle,
interval,
web3,
syncAlways
}) })
try { try {
@ -117,7 +53,6 @@ function * callCreateBlockPollChannel ({
/* /*
* Process Blocks * Process Blocks
*/ */
function * processBlockHeader ({ blockHeader, drizzle, web3, syncAlways }) { function * processBlockHeader ({ blockHeader, drizzle, web3, syncAlways }) {
const blockNumber = blockHeader.number const blockNumber = blockHeader.number
@ -133,7 +68,7 @@ function * processBlockHeader ({ blockHeader, drizzle, web3, syncAlways }) {
} }
} }
function * processBlock ({ block, drizzle, web3, syncAlways }) { function * processBlock ({ block, drizzle, syncAlways }) {
try { try {
// Emit block for addition to store. // Emit block for addition to store.
// Regardless of syncing success/failure, this is still the latest block. // Regardless of syncing success/failure, this is still the latest block.
@ -182,10 +117,6 @@ function * blocksSaga () {
// Block Subscriptions // Block Subscriptions
yield takeLatest(BlocksActions.BLOCKS_LISTENING, callCreateBlockChannel) yield takeLatest(BlocksActions.BLOCKS_LISTENING, callCreateBlockChannel)
yield takeEvery(BlocksActions.BLOCK_RECEIVED, processBlockHeader) yield takeEvery(BlocksActions.BLOCK_RECEIVED, processBlockHeader)
// Block Polling
yield takeLatest(BlocksActions.BLOCKS_POLLING, callCreateBlockPollChannel)
yield takeEvery(BlocksActions.BLOCK_FOUND, processBlock)
} }
export default blocksSaga export default blocksSaga

2
src/blocks/constants.js

@ -2,6 +2,4 @@ export const BLOCK_PROCESSING = 'BLOCK_PROCESSING'
export const BLOCKS_FAILED = 'BLOCKS_FAILED' export const BLOCKS_FAILED = 'BLOCKS_FAILED'
export const BLOCK_FAILED = 'BLOCK_FAILED' export const BLOCK_FAILED = 'BLOCK_FAILED'
export const BLOCK_RECEIVED = 'BLOCK_RECEIVED' export const BLOCK_RECEIVED = 'BLOCK_RECEIVED'
export const BLOCK_FOUND = 'BLOCK_FOUND'
export const BLOCKS_LISTENING = 'BLOCKS_LISTENING' export const BLOCKS_LISTENING = 'BLOCKS_LISTENING'
export const BLOCKS_POLLING = 'BLOCKS_POLLING'

3
src/defaultOptions.js

@ -7,9 +7,6 @@ const defaultOptions = {
}, },
contracts: [], contracts: [],
events: {}, events: {},
polls: {
blocks: 3000
},
syncAlways: false, syncAlways: false,
networkWhitelist: [] networkWhitelist: []
} }

14
src/drizzleStatus/drizzleStatusSaga.js

@ -48,19 +48,7 @@ export function * initializeDrizzle (action) {
yield call([drizzle, drizzle.addContract], contractConfig, events) yield call([drizzle, drizzle.addContract], contractConfig, events)
} }
const syncAlways = options.syncAlways yield put({ type: BlocksActions.BLOCKS_LISTENING, drizzle, web3 })
// Protect server-side environments by ensuring ethereum access is
// guarded by isMetaMask which should only be in browser environment.
//
if (web3.currentProvider.isMetaMask && !window.ethereum) {
// Using old MetaMask, attempt block polling.
const interval = options.polls.blocks
yield put({ type: BlocksActions.BLOCKS_POLLING, drizzle, interval, web3, syncAlways })
} else {
// Not using old MetaMask, attempt subscription block listening.
yield put({ type: BlocksActions.BLOCKS_LISTENING, drizzle, web3, syncAlways })
}
// Accounts Polling // Accounts Polling
if ('accounts' in options.polls) { if ('accounts' in options.polls) {

Loading…
Cancel
Save