Skip to content

Commit

Permalink
Changed onPollingComplete to be by networkClientId
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed Sep 27, 2023
1 parent ddf7c70 commit 82255b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
3 changes: 1 addition & 2 deletions packages/polling-controller/src/PollingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,13 @@ describe('PollingController', () => {
PollingCompleteType<typeof name>
>();


const controller = new MyGasFeeController({
messenger: mockMessenger,
metadata: {},
name,
state: { foo: 'bar' },
});
controller.onPollingComplete(pollingComplete);
controller.onPollingComplete('mainnet', pollingComplete);
const pollingToken = controller.start('mainnet');
controller.stop(pollingToken);
expect(pollingComplete).toHaveBeenCalledTimes(1);
Expand Down
22 changes: 17 additions & 5 deletions packages/polling-controller/src/PollingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {

readonly #intervalIds: Record<NetworkClientId, NodeJS.Timeout> = {};

#callbacks: Set<(networkClientId: NetworkClientId) => void> = new Set();
#callbacks: Map<
NetworkClientId,
Set<(networkClientId: NetworkClientId) => void>
> = new Map();

#intervalLength = 1000;

Expand Down Expand Up @@ -91,10 +94,10 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
clearTimeout(this.#intervalIds[networkClientId]);
delete this.#intervalIds[networkClientId];
this.#networkClientIdTokensMap.delete(networkClientId);
this.#callbacks.forEach((callback) => {
this.#callbacks.get(networkClientId)?.forEach((callback) => {
callback(networkClientId);
});
this.#callbacks.clear();
this.#callbacks.get(networkClientId)?.clear();
}
}
});
Expand Down Expand Up @@ -125,8 +128,17 @@ function PollingControllerMixin<TBase extends Constructor>(Base: TBase) {
}, this.#intervalLength);
}

onPollingComplete(callback: (networkClientId: NetworkClientId) => void) {
this.#callbacks.add(callback);
onPollingComplete(
networkClientId: NetworkClientId,
callback: (networkClientId: NetworkClientId) => void,
) {
if (this.#callbacks.has(networkClientId)) {
this.#callbacks.get(networkClientId)?.add(callback);
} else {
const set = new Set<typeof callback>();
set.add(callback);
this.#callbacks.set(networkClientId, set);
}
}
}
return PollingControllerBase;
Expand Down

0 comments on commit 82255b8

Please sign in to comment.