diff --git a/package-lock.json b/package-lock.json index a6ff4e0..bb9f27e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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 } } diff --git a/package.json b/package.json index 2f4b178..1f02401 100644 --- a/package.json +++ b/package.json @@ -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/", @@ -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 2badc07..7f3ca81 100755 --- a/src/observable.ts +++ b/src/observable.ts @@ -19,12 +19,21 @@ export type ObservableInterface = { subscribeCollect: (callback: ObserverCallbackCollect) => void; }; +const ClosedSymbol = Symbol("Observable Closed"); + export default class Observable { private subscriber: ObserverCallback[] = []; private subscriberCollect: ObserverCallbackCollect[] = []; 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 { * @returns {object} */ getPublicApi(): ObservableInterface { + 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); @@ -50,6 +67,9 @@ export default class Observable { } }, 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) @@ -62,6 +82,8 @@ export default class Observable { * @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 { }, 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) + } } \ No newline at end of file