Skip to content

Commit 453d235

Browse files
committed
Fixed lingering events in provider after removal.
1 parent 741bd05 commit 453d235

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src.ts/contract/contract.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ class PreparedTopicFilter implements DeferredTopicFilter {
8080
const resolver = canResolve(runner) ? runner: null;
8181
this.#filter = (async function() {
8282
const resolvedArgs = await Promise.all(fragment.inputs.map((param, index) => {
83+
const arg = args[index];
84+
if (arg == null) { return null; }
85+
8386
return param.walkAsync(args[index], (type, value) => {
84-
if (type === "address") { return resolveAddress(value, resolver); }
87+
if (type === "address") {
88+
return resolveAddress(value, resolver);
89+
}
8590
return value;
8691
});
8792
}));
@@ -443,17 +448,21 @@ async function getSub(contract: BaseContract, operation: string, event: Contract
443448
}
444449
};
445450

446-
let started = false;
451+
let starting: Array<Promise<any>> = [ ];
447452
const start = () => {
448-
if (started) { return; }
449-
provider.on(filter, listener);
450-
started = true;
453+
if (starting.length) { return; }
454+
starting.push(provider.on(filter, listener));
451455
};
452-
const stop = () => {
453-
if (!started) { return; }
456+
457+
const stop = async () => {
458+
if (starting.length == 0) { return; }
459+
460+
let started = starting;
461+
starting = [ ];
462+
await Promise.all(started);
454463
provider.off(filter, listener);
455-
started = false;
456464
};
465+
457466
sub = { tag, listeners: [ ], start, stop };
458467
subs.set(tag, sub);
459468
}

src.ts/providers/subscriber-filterid.ts

+7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ export class FilterIdSubscriber implements Subscriber {
2828

2929
#network: null | Network;
3030

31+
#hault: boolean;
32+
3133
constructor(provider: JsonRpcApiProvider) {
3234
this.#provider = provider;
3335

3436
this.#filterIdPromise = null;
3537
this.#poller = this.#poll.bind(this);
3638

3739
this.#network = null;
40+
41+
this.#hault = false;
3842
}
3943

4044
_subscribe(provider: JsonRpcApiProvider): Promise<string> {
@@ -68,6 +72,8 @@ export class FilterIdSubscriber implements Subscriber {
6872
throw new Error("chaid changed");
6973
}
7074

75+
if (this.#hault) { return; }
76+
7177
const result = await this.#provider.send("eth_getFilterChanges", [ filterId ]);
7278
await this._emitResults(this.#provider, result);
7379
} catch (error) { console.log("@TODO", error); }
@@ -88,6 +94,7 @@ export class FilterIdSubscriber implements Subscriber {
8894
start(): void { this.#poll(-2); }
8995

9096
stop(): void {
97+
this.#hault = true;
9198
this.#teardown();
9299
this.#provider.off("block", this.#poller);
93100
}

0 commit comments

Comments
 (0)