Add close to observable
This commit is contained in:
parent
11a3b00c12
commit
d1fdb789c1
8
package-lock.json
generated
8
package-lock.json
generated
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "@hibas123/utils",
|
||||
"version": "2.0.0",
|
||||
"version": "2.1.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"typescript": {
|
||||
"version": "3.3.3333",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.3333.tgz",
|
||||
"integrity": "sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw==",
|
||||
"version": "3.4.5",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
|
||||
"integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/utils",
|
||||
"version": "2.0.5",
|
||||
"version": "2.1.0",
|
||||
"description": "Different Utilities, that are not worth own packages",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
@ -16,7 +16,7 @@
|
||||
"type": "git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^3.3.3333"
|
||||
"typescript": "^3.4.5"
|
||||
},
|
||||
"files": [
|
||||
"src/",
|
||||
|
@ -19,12 +19,21 @@ export type ObservableInterface<T> = {
|
||||
subscribeCollect: (callback: ObserverCallbackCollect<T>) => void;
|
||||
};
|
||||
|
||||
const ClosedSymbol = Symbol("Observable Closed");
|
||||
|
||||
export default class Observable<T = any> {
|
||||
private subscriber: ObserverCallback<T>[] = [];
|
||||
private subscriberCollect: ObserverCallbackCollect<T>[] = [];
|
||||
private events: T[] = [];
|
||||
private timeout: number | undefined = undefined;
|
||||
|
||||
// Use symbol to make sure this property cannot be changed from the outside
|
||||
private [ClosedSymbol] = false;
|
||||
|
||||
get closed() {
|
||||
return this[ClosedSymbol];
|
||||
}
|
||||
|
||||
constructor(private collect_intervall: number = 100) { }
|
||||
|
||||
/**
|
||||
@ -33,13 +42,21 @@ export default class Observable<T = any> {
|
||||
* @returns {object}
|
||||
*/
|
||||
getPublicApi(): ObservableInterface<T> {
|
||||
if (this[ClosedSymbol])
|
||||
throw new Error("Observable is closed!");
|
||||
return {
|
||||
subscribe: (callback: ObserverCallback<T>) => {
|
||||
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<T> | ObserverCallbackCollect<T>) => {
|
||||
if (this[ClosedSymbol])
|
||||
return;
|
||||
|
||||
let idx = this.subscriber.findIndex(e => e === callback);
|
||||
if (idx >= 0) {
|
||||
this.subscriber.splice(idx, 1);
|
||||
@ -50,6 +67,9 @@ export default class Observable<T = any> {
|
||||
}
|
||||
},
|
||||
subscribeCollect: (callback: ObserverCallbackCollect<T>) => {
|
||||
if (this[ClosedSymbol])
|
||||
throw new Error("Observable is closed!");
|
||||
|
||||
let oldcb = this.subscriberCollect.find(e => e === callback);
|
||||
if (!oldcb)
|
||||
this.subscriberCollect.push(callback)
|
||||
@ -62,6 +82,8 @@ export default class Observable<T = any> {
|
||||
* @param data data to be sent
|
||||
*/
|
||||
send(data: T) {
|
||||
if (this[ClosedSymbol])
|
||||
throw new Error("Observable is closed!")
|
||||
this.subscriber.forEach(e => e(data));
|
||||
this.events.push(data);
|
||||
if (!this.timeout) {
|
||||
@ -74,4 +96,17 @@ export default class Observable<T = any> {
|
||||
}, this.collect_intervall);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes Observable. This will remove all subscribers and mark this observable as closed.
|
||||
* You won't be able to reopen this observable. All maybe collected data will be discardet.
|
||||
*/
|
||||
close() {
|
||||
this[ClosedSymbol] = true;
|
||||
this.subscriber = [];
|
||||
this.subscriberCollect = [];
|
||||
this.events = [];
|
||||
if (this.timeout)
|
||||
clearTimeout(this.timeout)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user