From 5533b1b961eacc48ae8717c09a17ad8c5de63a15 Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Thu, 13 Jun 2019 18:34:47 +0200 Subject: [PATCH] Add .off method to ApiBase (#999) * Add .off method to ApiBase * Update CHANGELOG --- CHANGELOG.md | 1 + packages/api/src/Base.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f6454d5fa..8b8450b598 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Update `ContractInfo` to match substrate master - Cater for `Vec`, `Option`, `Result`, tuples and fixed vectors for contracts ABIs - Additional examples for subscriptions using multi +- Add `off` method to `ApiBase` to remove an event listener # 0.81.1 diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index ecbad47d0b..05fb3782b3 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -315,6 +315,33 @@ export default abstract class ApiBase implement return this; } + /** + * @description Remove the given eventemitter handler + * + * @param type The type of event the callback was attached to. Available events are `connected`, `disconnected`, `ready` and `error` + * @param handler The callback to unregister. + * + * @example + *
+ * + * ```javascript + * const handler = () => { + * console.log('Connected !); + * }; + * + * // Start listening + * api.on('connected', handler); + * + * // Stop listening + * api.off('connected', handler); + * ``` + */ + off (type: ApiInterface$Events, handler: (...args: Array) => any): this { + this._eventemitter.removeListener(type, handler); + + return this; + } + /** * @description Attach an one-time eventemitter handler to listen to a specific event *