Fixed lingering events in provider after removal.

This commit is contained in:
Richard Moore 2023-01-15 07:54:07 -05:00
parent 741bd05830
commit 453d2357b1
2 changed files with 24 additions and 8 deletions

@ -80,8 +80,13 @@ class PreparedTopicFilter implements DeferredTopicFilter {
const resolver = canResolve(runner) ? runner: null; const resolver = canResolve(runner) ? runner: null;
this.#filter = (async function() { this.#filter = (async function() {
const resolvedArgs = await Promise.all(fragment.inputs.map((param, index) => { const resolvedArgs = await Promise.all(fragment.inputs.map((param, index) => {
const arg = args[index];
if (arg == null) { return null; }
return param.walkAsync(args[index], (type, value) => { return param.walkAsync(args[index], (type, value) => {
if (type === "address") { return resolveAddress(value, resolver); } if (type === "address") {
return resolveAddress(value, resolver);
}
return value; return value;
}); });
})); }));
@ -443,17 +448,21 @@ async function getSub(contract: BaseContract, operation: string, event: Contract
} }
}; };
let started = false; let starting: Array<Promise<any>> = [ ];
const start = () => { const start = () => {
if (started) { return; } if (starting.length) { return; }
provider.on(filter, listener); starting.push(provider.on(filter, listener));
started = true;
}; };
const stop = () => {
if (!started) { return; } const stop = async () => {
if (starting.length == 0) { return; }
let started = starting;
starting = [ ];
await Promise.all(started);
provider.off(filter, listener); provider.off(filter, listener);
started = false;
}; };
sub = { tag, listeners: [ ], start, stop }; sub = { tag, listeners: [ ], start, stop };
subs.set(tag, sub); subs.set(tag, sub);
} }

@ -28,6 +28,8 @@ export class FilterIdSubscriber implements Subscriber {
#network: null | Network; #network: null | Network;
#hault: boolean;
constructor(provider: JsonRpcApiProvider) { constructor(provider: JsonRpcApiProvider) {
this.#provider = provider; this.#provider = provider;
@ -35,6 +37,8 @@ export class FilterIdSubscriber implements Subscriber {
this.#poller = this.#poll.bind(this); this.#poller = this.#poll.bind(this);
this.#network = null; this.#network = null;
this.#hault = false;
} }
_subscribe(provider: JsonRpcApiProvider): Promise<string> { _subscribe(provider: JsonRpcApiProvider): Promise<string> {
@ -68,6 +72,8 @@ export class FilterIdSubscriber implements Subscriber {
throw new Error("chaid changed"); throw new Error("chaid changed");
} }
if (this.#hault) { return; }
const result = await this.#provider.send("eth_getFilterChanges", [ filterId ]); const result = await this.#provider.send("eth_getFilterChanges", [ filterId ]);
await this._emitResults(this.#provider, result); await this._emitResults(this.#provider, result);
} catch (error) { console.log("@TODO", error); } } catch (error) { console.log("@TODO", error); }
@ -88,6 +94,7 @@ export class FilterIdSubscriber implements Subscriber {
start(): void { this.#poll(-2); } start(): void { this.#poll(-2); }
stop(): void { stop(): void {
this.#hault = true;
this.#teardown(); this.#teardown();
this.#provider.off("block", this.#poller); this.#provider.off("block", this.#poller);
} }