UNPKG

@node-lightning/wire

Version:
85 lines 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GossipSyncWatcher = exports.GossipSyncWatcherState = void 0; var GossipSyncWatcherState; (function (GossipSyncWatcherState) { GossipSyncWatcherState[GossipSyncWatcherState["Idle"] = 0] = "Idle"; GossipSyncWatcherState[GossipSyncWatcherState["Watching"] = 1] = "Watching"; GossipSyncWatcherState[GossipSyncWatcherState["Complete"] = 2] = "Complete"; GossipSyncWatcherState[GossipSyncWatcherState["Canceled"] = 3] = "Canceled"; })(GossipSyncWatcherState = exports.GossipSyncWatcherState || (exports.GossipSyncWatcherState = {})); /** * GossipSyncWatcher monitors the progress of a GossipSync operation for the * completion of the synchronization and signals once the sync has completed * or timed out. */ class GossipSyncWatcher { constructor(logger) { this.logger = logger; this.completeAfterMs = 5000; this._state = GossipSyncWatcherState.Idle; this._messageCounter = 0; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment this._onTimeout = this._onTimeout.bind(this); } /** * Gets the state of the Watcher: IDLE, WATCHING, COMPLETE, FAILED */ get state() { return this._state; } /** * Gets the number of messages that have been seen while watching */ get messageCounter() { return this._messageCounter; } /** * Watches gossip traffic until a completion event occurs or watching is * cancelled. */ watch() { return new Promise((resolve, reject) => { this._state = GossipSyncWatcherState.Watching; this._setTimeout(); this._resolve = resolve; this._reject = reject; }); } /** * Process a message and debounce when it is a gossip message * @param msg */ onGossipMessage( // eslint-disable-next-line @typescript-eslint/no-unused-vars msg) { if (this._state === GossipSyncWatcherState.Watching) { this._messageCounter += 1; this._clearTimeout(); this._setTimeout(); } } /** * Cancels watching and sends a failure signal. */ cancel() { if (this._state === GossipSyncWatcherState.Watching) { this._clearTimeout(); this._resolve(); } this._state = GossipSyncWatcherState.Canceled; } _clearTimeout() { clearTimeout(this._timeoutHandle); } _setTimeout() { // eslint-disable-next-line @typescript-eslint/unbound-method this._timeoutHandle = setTimeout(this._onTimeout, this.completeAfterMs); } _onTimeout() { this._state = GossipSyncWatcherState.Complete; this._resolve(); } } exports.GossipSyncWatcher = GossipSyncWatcher; //# sourceMappingURL=GossipSyncWatcher.js.map