From edd37246fb14ca01ee5811fa9bc165d1bebdcbfc Mon Sep 17 00:00:00 2001 From: Fabian Date: Tue, 13 Aug 2019 17:34:38 +0200 Subject: [PATCH] Making publicAPI also available on Observable class --- package.json | 4 +-- src/observable.ts | 64 ++++++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 1f02401..0e8d8cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "2.1.0", + "version": "2.1.1", "description": "Different Utilities, that are not worth own packages", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -25,4 +25,4 @@ "readme.md" ], "private": false -} +} \ No newline at end of file diff --git a/src/observable.ts b/src/observable.ts index 7f3ca81..5f144eb 100755 --- a/src/observable.ts +++ b/src/observable.ts @@ -36,6 +36,38 @@ export default class Observable { constructor(private collect_intervall: number = 100) { } + subscribe(callback: ObserverCallback) { + if (this[ClosedSymbol]) + throw new Error("Observable is closed!"); + + let oldcb = this.subscriber.find(e => e === callback); + if (!oldcb) + this.subscriber.push(callback) + } + + unsubscribe(callback: ObserverCallback | ObserverCallbackCollect) { + if (this[ClosedSymbol]) + return; + + let idx = this.subscriber.findIndex(e => e === callback); + if (idx >= 0) { + this.subscriber.splice(idx, 1); + } else { + idx = this.subscriberCollect.findIndex(e => e === callback); + if (idx >= 0) + this.subscriberCollect.splice(idx, 1); + } + } + + subscribeCollect(callback: ObserverCallbackCollect) { + if (this[ClosedSymbol]) + throw new Error("Observable is closed!"); + + let oldcb = this.subscriberCollect.find(e => e === callback); + if (!oldcb) + this.subscriberCollect.push(callback) + } + /** * Creates Public API with subscribe and unsubscribe * @@ -45,35 +77,9 @@ export default class Observable { if (this[ClosedSymbol]) throw new Error("Observable is closed!"); return { - subscribe: (callback: ObserverCallback) => { - if (this[ClosedSymbol]) - throw new Error("Observable is closed!"); - - let oldcb = this.subscriber.find(e => e === callback); - if (!oldcb) - this.subscriber.push(callback) - }, - unsubscribe: (callback: ObserverCallback | ObserverCallbackCollect) => { - if (this[ClosedSymbol]) - return; - - let idx = this.subscriber.findIndex(e => e === callback); - if (idx >= 0) { - this.subscriber.splice(idx, 1); - } else { - idx = this.subscriberCollect.findIndex(e => e === callback); - if (idx >= 0) - this.subscriberCollect.splice(idx, 1); - } - }, - subscribeCollect: (callback: ObserverCallbackCollect) => { - if (this[ClosedSymbol]) - throw new Error("Observable is closed!"); - - let oldcb = this.subscriberCollect.find(e => e === callback); - if (!oldcb) - this.subscriberCollect.push(callback) - } + subscribe: (callback: ObserverCallback) => this.subscribe(callback), + unsubscribe: (callback: ObserverCallback | ObserverCallbackCollect) => this.unsubscribe(callback), + subscribeCollect: (callback: ObserverCallbackCollect) => this.subscribeCollect(callback) } }