Add close to observable

This commit is contained in:
Fabian 2019-05-14 11:33:39 -04:00
parent 11a3b00c12
commit d1fdb789c1
3 changed files with 42 additions and 7 deletions

8
package-lock.json generated
View File

@ -1,13 +1,13 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.0.0", "version": "2.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"typescript": { "typescript": {
"version": "3.3.3333", "version": "3.4.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.3333.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
"integrity": "sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw==", "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==",
"dev": true "dev": true
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.0.5", "version": "2.1.0",
"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",
@ -16,7 +16,7 @@
"type": "git" "type": "git"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^3.3.3333" "typescript": "^3.4.5"
}, },
"files": [ "files": [
"src/", "src/",
@ -25,4 +25,4 @@
"readme.md" "readme.md"
], ],
"private": false "private": false
} }

View File

@ -19,12 +19,21 @@ export type ObservableInterface<T> = {
subscribeCollect: (callback: ObserverCallbackCollect<T>) => void; subscribeCollect: (callback: ObserverCallbackCollect<T>) => void;
}; };
const ClosedSymbol = Symbol("Observable Closed");
export default class Observable<T = any> { export default class Observable<T = any> {
private subscriber: ObserverCallback<T>[] = []; private subscriber: ObserverCallback<T>[] = [];
private subscriberCollect: ObserverCallbackCollect<T>[] = []; private subscriberCollect: ObserverCallbackCollect<T>[] = [];
private events: T[] = []; private events: T[] = [];
private timeout: number | undefined = undefined; 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) { } constructor(private collect_intervall: number = 100) { }
/** /**
@ -33,13 +42,21 @@ export default class Observable<T = any> {
* @returns {object} * @returns {object}
*/ */
getPublicApi(): ObservableInterface<T> { getPublicApi(): ObservableInterface<T> {
if (this[ClosedSymbol])
throw new Error("Observable is closed!");
return { return {
subscribe: (callback: ObserverCallback<T>) => { subscribe: (callback: ObserverCallback<T>) => {
if (this[ClosedSymbol])
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])
return;
let idx = this.subscriber.findIndex(e => e === callback); let idx = this.subscriber.findIndex(e => e === callback);
if (idx >= 0) { if (idx >= 0) {
this.subscriber.splice(idx, 1); this.subscriber.splice(idx, 1);
@ -50,6 +67,9 @@ export default class Observable<T = any> {
} }
}, },
subscribeCollect: (callback: ObserverCallbackCollect<T>) => { subscribeCollect: (callback: ObserverCallbackCollect<T>) => {
if (this[ClosedSymbol])
throw new Error("Observable is closed!");
let oldcb = this.subscriberCollect.find(e => e === callback); let oldcb = this.subscriberCollect.find(e => e === callback);
if (!oldcb) if (!oldcb)
this.subscriberCollect.push(callback) this.subscriberCollect.push(callback)
@ -62,6 +82,8 @@ export default class Observable<T = any> {
* @param data data to be sent * @param data data to be sent
*/ */
send(data: T) { send(data: T) {
if (this[ClosedSymbol])
throw new Error("Observable is closed!")
this.subscriber.forEach(e => e(data)); this.subscriber.forEach(e => e(data));
this.events.push(data); this.events.push(data);
if (!this.timeout) { if (!this.timeout) {
@ -74,4 +96,17 @@ export default class Observable<T = any> {
}, this.collect_intervall); }, 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)
}
} }