From 319027e2e32e62ec51f0c429fdb8bc01964f012e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 29 Sep 2021 17:27:31 -0700 Subject: [PATCH] Add type annotations to BaseObservable --- src/observable/BaseObservable.ts | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/observable/BaseObservable.ts b/src/observable/BaseObservable.ts index 0f9934c3..16bd3b81 100644 --- a/src/observable/BaseObservable.ts +++ b/src/observable/BaseObservable.ts @@ -14,20 +14,18 @@ See the License for the specific language governing permissions and limitations under the License. */ -export class BaseObservable { - constructor() { - this._handlers = new Set(); - } +export abstract class BaseObservable { + protected _handlers: Set = new Set(); - onSubscribeFirst() { + onSubscribeFirst(): void { } - onUnsubscribeLast() { + onUnsubscribeLast(): void { } - subscribe(handler) { + subscribe(handler: T): () => void { this._handlers.add(handler); if (this._handlers.size === 1) { this.onSubscribeFirst(); @@ -37,7 +35,7 @@ export class BaseObservable { }; } - unsubscribe(handler) { + unsubscribe(handler: T | null): null { if (handler) { this._handlers.delete(handler); if (this._handlers.size === 0) { @@ -48,14 +46,14 @@ export class BaseObservable { return null; } - unsubscribeAll() { + unsubscribeAll(): void { if (this._handlers.size !== 0) { this._handlers.clear(); this.onUnsubscribeLast(); } } - get hasSubscriptions() { + get hasSubscriptions(): boolean { return this._handlers.size !== 0; } @@ -63,13 +61,11 @@ export class BaseObservable { } export function tests() { - class Collection extends BaseObservable { - constructor() { - super(); - this.firstSubscribeCalls = 0; - this.firstUnsubscribeCalls = 0; - } - onSubscribeFirst() { this.firstSubscribeCalls += 1; } + class Collection extends BaseObservable<{}> { + firstSubscribeCalls: number= 0; + firstUnsubscribeCalls: number = 0; + + onSubscribeFirst() { this.firstSubscribeCalls += 1; } onUnsubscribeLast() { this.firstUnsubscribeCalls += 1; } }