Skip to content

Commit 59e069f

Browse files
authored
Added unsubscribe return function to wsProvider.on() (#1780)
* Added unsubscribe return function to wsProvider.on() * Updated mocks, added noop to http provider
1 parent 65ff5c8 commit 59e069f

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

packages/rpc-provider/src/http/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ export default class HttpProvider implements ProviderInterface {
8484
* @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
8585
*/
8686
// eslint-disable-next-line @typescript-eslint/no-unused-vars
87-
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void {
87+
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
8888
l.error('HTTP Provider does not have \'on\' emitters, use WebSockets instead');
89+
return (): void => {
90+
// noop
91+
};
8992
}
9093

9194
/**

packages/rpc-provider/src/mock/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@ export default class Mock implements ProviderInterface {
104104
return true;
105105
}
106106

107-
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void {
107+
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
108108
this.emitter.on(type, sub);
109+
return (): void => {
110+
this.emitter.removeListener(type, sub);
111+
};
109112
}
110113

111114
// eslint-disable-next-line @typescript-eslint/require-await

packages/rpc-provider/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface ProviderInterface {
4747
clone (): ProviderInterface;
4848
disconnect (): void;
4949
isConnected (): boolean;
50-
on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void;
50+
on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
5151
send (method: string, params: any[]): Promise<any>;
5252
subscribe (type: string, method: string, params: any[], cb: ProviderInterfaceCallback): Promise<number>;
5353
unsubscribe (type: string, method: string, id: number): Promise<boolean>;

packages/rpc-provider/src/ws/Provider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ export default class WsProvider implements WSProviderInterface {
167167
* @summary Listens on events after having subscribed using the [[subscribe]] function.
168168
* @param {ProviderInterfaceEmitted} type Event
169169
* @param {ProviderInterfaceEmitCb} sub Callback
170+
* @return unsubscribe function
170171
*/
171-
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void {
172+
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
172173
this._eventemitter.on(type, sub);
174+
return (): void => {
175+
this._eventemitter.removeListener(type, sub);
176+
};
173177
}
174178

175179
/**

0 commit comments

Comments
 (0)