From 34ca4a756335941e3afb15e85783b1daa522a4a7 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 29 Aug 2019 15:33:58 -0400 Subject: [PATCH] feat: use a single subscription/interval for calls --- src/eventSyncer.js | 55 +++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/eventSyncer.js b/src/eventSyncer.js index 69368c7..7c8c1be 100644 --- a/src/eventSyncer.js +++ b/src/eventSyncer.js @@ -9,14 +9,17 @@ const Web3Eth = require('web3-eth'); class EventSyncer { - constructor(provider, options, ) { + constructor(provider, options = {}) { this.events = new Events(); this.web3 = new Web3Eth(provider); - this.intervalTracker = []; this.options = {}; this.options.callInterval = options.callInterval || 0; this.options.dbFilename = options.dbFilename || 'phoenix.db'; + + this.newBlocksSubscription = null; + this.intervalTracker = null; + this.callMethods = []; } init() { @@ -84,8 +87,35 @@ class EventSyncer { return sub; } + _initNewBlocksSubscription() { + if(this.newBlocksSubscription != null || this.options.callInterval !== 0) return; + + this.newBlocksSubscription = this.web3.subscribe('newBlockHeaders', (err, result) => { + if(err) { + sub.error(err); + return; + } + + this.callMethods.forEach(fn => { + fn(); + }); + }); + } + + _initCallInterval() { + if(this.intervalTracker != null || this.options.callInterval === 0) return; + + this.intervalTracker = setInterval(() => { + this.callMethods.forEach(fn => { + fn(); + }); + }, this.options.callInterval); + + } + // TODO: should save value in database trackProperty(contractInstance, propName, methodArgs = [], callArgs = {}) { + const sub = new ReplaySubject(); const method = contractInstance.methods[propName].apply(contractInstance.methods[propName], methodArgs) @@ -101,24 +131,19 @@ class EventSyncer { callContractMethod(); - if(this.options.callInterval > 0){ - this.intervalTracker.push(setInterval(callContractMethod, this.options.callInterval)); - } else { - this.web3.subscribe('newBlockHeaders', (err, result) => { - if(err) { - sub.error(err); - return; - } - callContractMethod(); - }) - } + this._initNewBlocksSubscription(); + this._initCallInterval(); + this.callMethods.push(callContractMethod); + return sub.pipe(distinctUntilChanged((a, b) => equal(a, b))); } clear(){ - this.intervalTracker.forEach(clearInterval); - this.intervalTracker = []; + clearInterval(this.intervalTracker); + this.newBlocksSubscription.unsubscribe(); + this.intervalTracker = null; + this.callMethods = []; } }