Added unsubscribe return function to wsProvider.on() (#1780)

* Added unsubscribe return function to wsProvider.on()

* Updated mocks, added noop to http provider
This commit is contained in:
Jakub Pánik
2020-01-30 16:48:11 +01:00
committed by GitHub
parent 65ff5c8f30
commit 59e069f091
4 changed files with 14 additions and 4 deletions
+4 -1
View File
@@ -84,8 +84,11 @@ export default class HttpProvider implements ProviderInterface {
* @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void {
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
l.error('HTTP Provider does not have \'on\' emitters, use WebSockets instead');
return (): void => {
// noop
};
}
/**
+4 -1
View File
@@ -104,8 +104,11 @@ export default class Mock implements ProviderInterface {
return true;
}
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void {
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
this.emitter.on(type, sub);
return (): void => {
this.emitter.removeListener(type, sub);
};
}
// eslint-disable-next-line @typescript-eslint/require-await
+1 -1
View File
@@ -47,7 +47,7 @@ export interface ProviderInterface {
clone (): ProviderInterface;
disconnect (): void;
isConnected (): boolean;
on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void;
on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
send (method: string, params: any[]): Promise<any>;
subscribe (type: string, method: string, params: any[], cb: ProviderInterfaceCallback): Promise<number>;
unsubscribe (type: string, method: string, id: number): Promise<boolean>;
+5 -1
View File
@@ -167,9 +167,13 @@ export default class WsProvider implements WSProviderInterface {
* @summary Listens on events after having subscribed using the [[subscribe]] function.
* @param {ProviderInterfaceEmitted} type Event
* @param {ProviderInterfaceEmitCb} sub Callback
* @return unsubscribe function
*/
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): void {
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
this._eventemitter.on(type, sub);
return (): void => {
this._eventemitter.removeListener(type, sub);
};
}
/**