From 6786ae4d57889ce5678af6f3035985a8bdb35c59 Mon Sep 17 00:00:00 2001 From: Ivan Kalinin Date: Fri, 15 Jul 2022 16:15:59 +0200 Subject: [PATCH] Add autobinding to subscriptions and address --- package.json | 2 +- src/index.ts | 12 ++++++------ src/stream.ts | 4 +++- src/utils.ts | 4 +++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index d0cd1a99..4d21b14e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "everscale-inpage-provider", - "version": "0.3.27", + "version": "0.3.28", "description": "Web3-like interface to the Everscale blockchain", "repository": "https://github.com/broxus/everscale-inpage-provider", "main": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index 826ed314..2e049f43 100644 --- a/src/index.ts +++ b/src/index.ts @@ -365,7 +365,7 @@ export class ProviderRpcClient { return this; } - async subscribe(): Promise { + subscribe = async (): Promise => { if (this._subscribed) { return; } @@ -375,9 +375,9 @@ export class ProviderRpcClient { for (const handler of this._listeners['subscribed']) { handler(); } - } + }; - async unsubscribe(): Promise { + unsubscribe = async (): Promise => { if (!this._subscribed) { return; } @@ -387,7 +387,7 @@ export class ProviderRpcClient { for (const handler of this._listeners['unsubscribed']) { handler(); } - } + }; notify(data: ProviderEventData) { for (const handler of this._listeners['data']) { @@ -923,12 +923,12 @@ export interface Subscription { /** * Can be used to re-subscribe with the same parameters. */ - subscribe(): Promise; + subscribe: () => Promise; /** * Unsubscribes the subscription. */ - unsubscribe(): Promise; + unsubscribe: () => Promise; } type SubscriptionEvent = 'data' | 'subscribed' | 'unsubscribed'; diff --git a/src/stream.ts b/src/stream.ts index 7d980cf1..c4cf802d 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -113,7 +113,9 @@ export class Subscriber { return this._addSubscription('contractStateChanged', address, false); } - public async unsubscribe(): Promise { + public unsubscribe = async (): Promise => this._unsubscribe(); + + private async _unsubscribe(): Promise { const subscriptions = Object.assign({}, this.subscriptions); for (const address of Object.keys(this.subscriptions)) { delete this.subscriptions[address]; diff --git a/src/utils.ts b/src/utils.ts index 34d1afff..41c09163 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,7 +39,9 @@ export class Address { return this._address; } - public equals(other: Address | string): boolean { + public equals = (other: Address | string): boolean => this._equals(other); + + private _equals(other: Address | string): boolean { if (other instanceof Address) { return this._address == other._address; } else {