Merge pull request #789 from bucko13/indexer-listeners

add ability to track listeners and remove them on close for indexer
This commit is contained in:
Braydon Fuller 2019-07-03 09:26:39 -07:00
commit fed7f2997b
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
2 changed files with 33 additions and 6 deletions

View File

@ -52,6 +52,7 @@ class Indexer extends EventEmitter {
this.db = null;
this.batch = null;
this.bound = [];
this.syncing = false;
this.height = 0;
}
@ -122,12 +123,17 @@ class Indexer extends EventEmitter {
}
/**
* Close the indexer, wait for the database to close.
* Close the indexer, wait for the database to close,
* unbind all events.
* @returns {Promise}
*/
async close() {
return this.db.close();
await this.db.close();
for (const [event, listener] of this.bound)
this.chain.removeListener(event, listener);
this.bound.length = 0;
}
/**
@ -187,7 +193,7 @@ class Indexer extends EventEmitter {
}
/**
* Bind to chain events.
* Bind to chain events and save listeners for removal on close
* @private
*/
@ -202,9 +208,10 @@ class Indexer extends EventEmitter {
}
};
this.chain.on('connect', listener);
this.chain.on('disconnect', listener);
this.chain.on('reset', listener);
for (const event of ['connect', 'disconnect', 'reset']) {
this.bound.push([event, listener]);
this.chain.on(event, listener);
}
}
/**

View File

@ -4,6 +4,7 @@
'use strict';
const assert = require('bsert');
const EventEmitter = require('events');
const reorg = require('./util/reorg');
const Script = require('../lib/script/script');
const Opcode = require('../lib/script/opcode');
@ -320,6 +321,25 @@ describe('Indexer', function() {
message: 'Limit above max of 10.'
});
});
it('should track bound chain events and remove on close', async () => {
const indexer = new AddrIndexer({
blocks: {},
chain: new EventEmitter()
});
const events = ['connect', 'disconnect', 'reset'];
await indexer.open();
for (const event of events)
assert.equal(indexer.chain.listeners(event).length, 1);
await indexer.close();
for (const event of events)
assert.equal(indexer.chain.listeners(event).length, 0);
});
});
describe('Index 10 blocks', function() {