test-pkg-ngn
Version:
A library containing helper functions that facilitate scripting for keepers of the Keep3r Network
82 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockListener = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const rxjs_1 = require("rxjs");
/**
* Class in charge of managing the fetching of blocks and how they are provided acoss the app.
*
*/
class BlockListener {
/**
* @param provider - JsonRpc provider that has the methods needed to fetch and listen for new blocks.
*/
constructor(provider) {
this.provider = provider;
// Amount of live subscriptions to block$ observable.
this.count = 0;
// Observable in charge of emitting and providing new Blocks.
this.block$ = new rxjs_1.Subject();
// Array of generated internal subscriptions. Used mainly to be able to unsubscribe from them when needed.
this.subs = [];
}
/**
* This function is able to provide a listener for new incoming blocks with all their data.
* Returns and observable that emits an event every time a new block arrives.
*
* @dev
* Block listener is initialized only if the subscriptions account is zero. Otherwise it will skip the initialization
* and just return the block$ observable where the new blocks are being pushed.
* For the fetching part we need to combine two different functions to fetch and deliver new blocks:
* - One that fetches the current block just once when this function is called and push it to block$ observable.
* - One that hooks to the 'block' event of the provider that returns just the number of the new block, and then use
* provider.getBlock(blockNumber) method to fetch all the data of that block and push it to block$ observable.
*
* @param debugId - Optional id to help with debugging.
* @returns An observable that emits blocks
*/
stream(debugId) {
if (this.count++ === 0) {
this.provider.getBlock('latest').then((block) => {
console.info(`${chalk_1.default.bgGray('\nblock arrived:', block.number)}\n`);
this.block$.next(block);
});
console.info(chalk_1.default.redBright('\n------ START BLOCK LISTENING -----'));
const onBlockNumber$ = rxjs_1.fromEvent(this.provider, 'block');
const sub = onBlockNumber$.pipe(rxjs_1.mergeMap((blockNumber) => this.provider.getBlock(blockNumber))).subscribe((block) => {
console.info(`${chalk_1.default.bgGray('\nblock arrived:', block.number)}\n`);
this.block$.next(block);
});
this.subs.push(sub);
}
if (debugId)
console.debug(`\nOpen BlockListener subscriptions count: ${chalk_1.default.redBright(this.count)} corresponded to ${chalk_1.default.green(debugId)}`);
else
console.debug('\nOpen BlockListener subscriptions count:', chalk_1.default.redBright(this.count));
return this.block$;
}
/**
* Stops block fetching and remove all the internal subscriptions to blockNumber observable.
*
* @dev
* This will only stop block fetching and subscription to blockNumber IF the amount of subscriptions to block$ observable
* is zero. If amount is zero this means that theres no part of the code actually listening to the block$ observable
* so theres no need for us to keep listening for new blocks incoming.
*
* @param debugId - Optional id to help with debugging.
*/
stop(debugId) {
if (--this.count === 0) {
console.info(chalk_1.default.redBright('\n------ STOP BLOCK LISTENING -----'));
this.subs.forEach((sub) => sub.unsubscribe());
this.provider.removeAllListeners('block');
}
if (debugId)
console.debug(`\nOpen BlockListener subscriptions count: ${chalk_1.default.redBright(this.count)} corresponded to ${chalk_1.default.green(debugId)}`);
else
console.debug('\nOpen BlockListener subscriptions count:', chalk_1.default.redBright(this.count));
}
}
exports.BlockListener = BlockListener;
//# sourceMappingURL=blocks.js.map