UNPKG

@bsv/wallet-toolbox

Version:

BRC100 conforming wallet, wallet storage and wallet signer components

55 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LiveIngestorWhatsOnChainPoll = void 0; const utilityHelpers_1 = require("../../../../utility/utilityHelpers"); const LiveIngestorBase_1 = require("./LiveIngestorBase"); const WhatsOnChainServices_1 = require("./WhatsOnChainServices"); /** * Reports new headers by polling periodically. */ class LiveIngestorWhatsOnChainPoll extends LiveIngestorBase_1.LiveIngestorBase { static createLiveIngestorWhatsOnChainOptions(chain) { const options = { ...WhatsOnChainServices_1.WhatsOnChainServices.createWhatsOnChainServicesOptions(chain), ...LiveIngestorBase_1.LiveIngestorBase.createLiveIngestorBaseOptions(chain), idleWait: 100000 }; return options; } constructor(options) { super(options); this.done = false; this.idleWait = options.idleWait || 100000; this.woc = new WhatsOnChainServices_1.WhatsOnChainServices(options); } async getHeaderByHash(hash) { const header = await this.woc.getHeaderByHash(hash); return header; } async startListening(liveHeaders) { this.done = false; let lastHeaders = []; for (; !this.done;) { const headers = await this.woc.getHeaders(); const newHeaders = headers.filter(h => !lastHeaders.some(lh => lh.hash === h.hash)); for (const h of newHeaders) { const bh = (0, WhatsOnChainServices_1.wocGetHeadersHeaderToBlockHeader)(h); liveHeaders.unshift(bh); } lastHeaders = headers; for (let sec = 0; sec < 60 && !this.done; sec++) { // Only wait a second at a time so we notice `done` sooner... await (0, utilityHelpers_1.wait)(1000); } } this.log(`LiveIngestorWhatsOnChainPoll stopped`); } stopListening() { this.done = true; } async shutdown() { this.stopListening(); } } exports.LiveIngestorWhatsOnChainPoll = LiveIngestorWhatsOnChainPoll; //# sourceMappingURL=LiveIngestorWhatsOnChainPoll.js.map