Fixed async emits overrunning each other causing no-running-subscriber errors.

This commit is contained in:
Richard Moore 2023-02-02 04:03:11 -05:00
parent 97b041f1a0
commit 7c37182d03
2 changed files with 9 additions and 4 deletions

@ -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<any>): Promise<boolean> {
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 }) => {

@ -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 {