PaulBernier / factomjs

Javascript library to build applications on the Factom blockchain.
MIT License
22 stars 7 forks source link

Add FactomEventEmitter class #9

Closed OrmEmbaar closed 5 years ago

OrmEmbaar commented 5 years ago

This PR adds the FactomEventEmitter class to the FactomJS library. The FactomEventEmitter class allows users to attach event listeners to various Factom network events. Each instance of the FactomEventEmitter class will handle when to start and stop polling the blockchain. The user interface is as follows:

 const emitter = new FactomEventEmitter(cli);

emitter.on('directoryBlock', console.log);
emitter.on('factoidBlock', console.log);
emitter.on('adminBlock', console.log);
emitter.on('entryCreditBlock', console.log);
emitter.on('newEntryChain', console.log); // emits first entry block for any new chain
emitter.on('FA29eyMVJaZ2tbGqJ3M49gANaXMXCjgfKcJGe5mx8p4iQFCvFDAC', console.log); // emits new transaction to or from the given address
emitter.on('23411b35d7666bdc41cf1446bf2156e20584f5c748dbabf5ca8f0fd9bd00efb1', console.log); // emits any new entry block for the given chain.

Changes to the README will follow if this PR is accepted.

OrmEmbaar commented 5 years ago

I agree that your implementation is much cleaner. There are a couple of things that I can see which would not work, but those are minor details. I will refactor the code and see if there is anything more fundamental that we are overlooking.

OrmEmbaar commented 5 years ago

I have refactored the code as you suggested, @PaulBernier.

OrmEmbaar commented 5 years ago

This is what i have come up with for the docs.

/**
 * Listen for new Factom Events.
 * @class
 * @param {FactomCli} cli - FactomCli instance to be used by the FactomEventEmitter instance to fetch blockchain data.
 * @param {object=} opts - Options to set on the FactomEventEmitter instance
 * @param {number} [opts.interval=7500] - Interval (ms) at which the FactomEventEmtitter instance should poll the blockchain to check for a new block.
 * @param {number} [opts.concurrency=10] - Number of concurrent requests that entry chain-type event emitter functions can make to factomd.
 * @event directoryBlock - Listener receives newest directory block.
 * @event factoidBlock - Listener receives newest factoid block.
 * @event adminBlock - Listener receives newest admin block.
 * @event entryCreditBlock - Listener receives newest directory block.
 * @event newEntryChain - Listener receives first entry block of a new entry chain.
 * @event FA29eyMVJaZ2tbGqJ3M49gANaXMXCjgfKcJGe5mx8p4iQFCvFDAC - Listener receives new factoid transaction to or from factoid address.
 * @event 4060c0192a421ca121ffff935889ef55a64574a6ef0e69b2b4f8a0ab919b2ca4 - Listener receives new entry block for entry chain ID.
 * @example
 * // logs all new entry blocks for given chain ID.
 * const cli = new FactomCli();
 * const emitter = new FactomEventEmitter(cli, { concurrency: 15, interval: 10000 })
 * emitter.on('4060c0192a421ca121ffff935889ef55a64574a6ef0e69b2b4f8a0ab919b2ca4', console.log)
 */

Thoughts?

PaulBernier commented 5 years ago

Merged. Thanks for this great contribution @afenrir!