From 7c37182d03e407175eefab4509dfde43f247c699 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 2 Feb 2023 04:03:11 -0500 Subject: [PATCH] Fixed async emits overrunning each other causing no-running-subscriber errors. --- src.ts/providers/abstract-provider.ts | 6 ++++-- src.ts/providers/subscriber-polling.ts | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src.ts/providers/abstract-provider.ts b/src.ts/providers/abstract-provider.ts index 071228476..1478badf7 100644 --- a/src.ts/providers/abstract-provider.ts +++ b/src.ts/providers/abstract-provider.ts @@ -375,7 +375,7 @@ export class AbstractProvider implements Provider { this.#plugins = new Map(); this.#pausedState = null; - this.#nextTimer = 0; + this.#nextTimer = 1; this.#timers = new Map(); this.#disableCcipRead = false; @@ -1161,7 +1161,9 @@ export class AbstractProvider implements Provider { async emit(event: ProviderEvent, ...args: Array): Promise { const sub = await this.#hasSub(event, args); - if (!sub) { return false; }; + // If there is not subscription or if a recent emit removed + // the last of them (which also deleted the sub) do nothing + if (!sub || sub.listeners.length === 0) { return false; }; const count = sub.listeners.length; sub.listeners = sub.listeners.filter(({ listener, once }) => { diff --git a/src.ts/providers/subscriber-polling.ts b/src.ts/providers/subscriber-polling.ts index 4f6fa362b..c50a79121 100644 --- a/src.ts/providers/subscriber-polling.ts +++ b/src.ts/providers/subscriber-polling.ts @@ -28,7 +28,7 @@ export function getPollingSubscriber(provider: AbstractProvider, event: Provider * * @_docloc: api/providers/abstract-provider */ -export class PollingBlockSubscriber implements Subscriber{ +export class PollingBlockSubscriber implements Subscriber { #provider: AbstractProvider; #poller: null | number; @@ -56,6 +56,9 @@ export class PollingBlockSubscriber implements Subscriber{ return; } + // We have been stopped + if (this.#poller == null) { return; } + // @TODO: Put a cap on the maximum number of events per loop? if (blockNumber !== this.#blockNumber) { @@ -71,8 +74,8 @@ export class PollingBlockSubscriber implements Subscriber{ start(): void { if (this.#poller) { throw new Error("subscriber already running"); } - this.#poll(); this.#poller = this.#provider._setTimeout(this.#poll.bind(this), this.#interval); + this.#poll(); } stop(): void {