Making publicAPI also available on Observable class

This commit is contained in:
Fabian 2019-08-13 17:34:38 +02:00
parent d1fdb789c1
commit edd37246fb
2 changed files with 37 additions and 31 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.1.0", "version": "2.1.1",
"description": "Different Utilities, that are not worth own packages", "description": "Different Utilities, that are not worth own packages",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",

View File

@ -36,24 +36,16 @@ export default class Observable<T = any> {
constructor(private collect_intervall: number = 100) { } constructor(private collect_intervall: number = 100) { }
/** subscribe(callback: ObserverCallback<T>) {
* Creates Public API with subscribe and unsubscribe
*
* @returns {object}
*/
getPublicApi(): ObservableInterface<T> {
if (this[ClosedSymbol])
throw new Error("Observable is closed!");
return {
subscribe: (callback: ObserverCallback<T>) => {
if (this[ClosedSymbol]) if (this[ClosedSymbol])
throw new Error("Observable is closed!"); throw new Error("Observable is closed!");
let oldcb = this.subscriber.find(e => e === callback); let oldcb = this.subscriber.find(e => e === callback);
if (!oldcb) if (!oldcb)
this.subscriber.push(callback) this.subscriber.push(callback)
}, }
unsubscribe: (callback: ObserverCallback<T> | ObserverCallbackCollect<T>) => {
unsubscribe(callback: ObserverCallback<T> | ObserverCallbackCollect<T>) {
if (this[ClosedSymbol]) if (this[ClosedSymbol])
return; return;
@ -65,8 +57,9 @@ export default class Observable<T = any> {
if (idx >= 0) if (idx >= 0)
this.subscriberCollect.splice(idx, 1); this.subscriberCollect.splice(idx, 1);
} }
}, }
subscribeCollect: (callback: ObserverCallbackCollect<T>) => {
subscribeCollect(callback: ObserverCallbackCollect<T>) {
if (this[ClosedSymbol]) if (this[ClosedSymbol])
throw new Error("Observable is closed!"); throw new Error("Observable is closed!");
@ -74,6 +67,19 @@ export default class Observable<T = any> {
if (!oldcb) if (!oldcb)
this.subscriberCollect.push(callback) this.subscriberCollect.push(callback)
} }
/**
* Creates Public API with subscribe and unsubscribe
*
* @returns {object}
*/
getPublicApi(): ObservableInterface<T> {
if (this[ClosedSymbol])
throw new Error("Observable is closed!");
return {
subscribe: (callback: ObserverCallback<T>) => this.subscribe(callback),
unsubscribe: (callback: ObserverCallback<T> | ObserverCallbackCollect<T>) => this.unsubscribe(callback),
subscribeCollect: (callback: ObserverCallbackCollect<T>) => this.subscribeCollect(callback)
} }
} }