Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add destruction function to sub manager middleware #10

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eth-json-rpc-filters",
"version": "4.1.0",
"version": "4.1.1",
"description": "[json-rpc-engine](https://github.com/kumavis/json-rpc-engine) middleware implementing ethereum filter methods. Backed by an [eth-block-tracker](https://github.com/MetaMask/eth-block-tracker) and web3 provider interface (`web3.currentProvider`).",
"main": "index.js",
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions subscriptionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ function createSubscriptionMiddleware({ blockTracker, provider }) {
const subscriptions = {}
const filterManager = createFilterMiddleware({ blockTracker, provider })

// internal flag
let isDestroyed = false

// create subscriptionManager api object
const events = new SafeEventEmitter()
const middleware = createScaffoldMiddleware({
eth_subscribe: createAsyncMiddleware(subscribe),
eth_unsubscribe: createAsyncMiddleware(unsubscribe),
})
middleware.destroy = destroy
return { events, middleware }

async function subscribe(req, res) {

if (isDestroyed) throw new Error(
'SubscriptionManager - attempting to use after destroying'
)

const subscriptionType = req.params[0]
// subId is 16 byte hex string
const subId = unsafeRandomBytes(16)
Expand Down Expand Up @@ -81,6 +90,11 @@ function createSubscriptionMiddleware({ blockTracker, provider }) {
}

async function unsubscribe(req, res) {

if (isDestroyed) throw new Error(
'SubscriptionManager - attempting to use after destroying'
)

const id = req.params[0]
const subscription = subscriptions[id]
// if missing, return "false" to indicate it was not removed
Expand All @@ -105,6 +119,14 @@ function createSubscriptionMiddleware({ blockTracker, provider }) {
})
}

function destroy () {
events.removeAllListeners()
for (const id in subscriptions) {
subscriptions[id].destroy()
delete subscriptions[id]
}
isDestroyed = true
}
}

function normalizeBlock(block) {
Expand Down
6 changes: 0 additions & 6 deletions test/ganache.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const test = require('tape')
const clone = require('deep-clone')
const JsonRpcEngine = require('json-rpc-engine')
const asMiddleware = require('json-rpc-engine/src/asMiddleware')
const createScaffoldMiddleware = require('eth-json-rpc-middleware/scaffold')
const providerFromEngine = require('eth-json-rpc-middleware/providerFromEngine')
const ethUtil = require('ethereumjs-util')
const {
createTestSetup,
createPayload,
asyncTest,
deployLogEchoContract,
} = require('./util')
Expand Down
4 changes: 2 additions & 2 deletions tx-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const { incrementHexInt } = require('./hexUtils')

class TxFilter extends BaseFilter {

constructor ({ provider, params }) {
constructor ({ provider }) {
super()
this.type = 'tx'
this.provider = provider
}

async update ({ oldBlock, newBlock }) {
async update ({ oldBlock }) {
const toBlock = oldBlock
const fromBlock = incrementHexInt(oldBlock)
const blocks = await getBlocksForRange({ provider: this.provider, fromBlock, toBlock })
Expand Down