diff --git a/.npmingore b/.npmingore deleted file mode 100644 index a3f930d..0000000 --- a/.npmingore +++ /dev/null @@ -1,2 +0,0 @@ -src/ -node_modules/ diff --git a/package.json b/package.json index 9b9373c..9904f45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "1.0.2", + "version": "2.0.0", "description": "Different Utilities, that are not worth own packages", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -18,5 +18,10 @@ "devDependencies": { "typescript": "^3.3.3333" }, + "files": [ + "lib/", + "tsconfig.json", + "readme.md" + ], "private": false } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index ec3ae34..03d06a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,11 @@ import Lock, { Release } from "./lock"; -import Observable, { ObserverCallback } from "./observable"; +import Observable, { ObserverCallback, ObserverCallbackCollect, ObservableInterface } from "./observable"; export { Lock, Release, Observable, - ObserverCallback + ObserverCallback, + ObserverCallbackCollect, + ObservableInterface } \ No newline at end of file diff --git a/src/observable.ts b/src/observable.ts index f9395b1..66d9f57 100755 --- a/src/observable.ts +++ b/src/observable.ts @@ -1,39 +1,58 @@ -export type ObserverCallback = (data: T[]) => void; +export type ObserverCallbackCollect = (data: T[]) => void; +export type ObserverCallback = (ata: T) => void; + +export type ObservableInterface = { + /** + * Subscribe to Observable + * @param {function} callback + */ + subscribe: (callback: ObserverCallback) => void; + /** + * Unsubscribe fron Observable + * @param {function} callback + */ + unsubscribe: (callback: ObserverCallback) => void; + /** + * Subscribe to Observable in collect mode + * @param {function} callback + */ + subscribeCollect: (callback: ObserverCallbackCollect) => void; +}; export default class Observable { - private subscriber: { callback: ObserverCallback, one: boolean }[] = []; + private subscriber: ObserverCallback[] = []; + private subscriberCollect: ObserverCallbackCollect[] = []; private events: T[] = []; - private timeout: number | undefined = undefined; + private timeout: NodeJS.Timeout | number | undefined = undefined; - constructor(private collect: boolean = true, private collect_intervall: number = 100) { } + constructor(private collect_intervall: number = 100) { } /** * Creates Public API with subscribe and unsubscribe * * @returns {object} */ - getPublicApi() { + getPublicApi(): ObservableInterface { return { - /** - * Subscribe to Observable - * @param {function} callback - */ - subscribe: (callback: ObserverCallback, one: boolean = false) => { - let oldcb = this.subscriber.find(e => e.callback === callback); - if (oldcb) - oldcb.one = one - else - this.subscriber.push({ callback, one }) + subscribe: (callback: ObserverCallback) => { + let oldcb = this.subscriber.find(e => e === callback); + if (!oldcb) + this.subscriber.push(callback) }, - /** - * Subscribe fron Observable - * @param {function} callback - */ - unsubscribe: (callback: ObserverCallback) => { - let idx = this.subscriber.findIndex(e => e.callback === callback); + unsubscribe: (callback: ObserverCallback | ObserverCallbackCollect) => { + 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) => { + let oldcb = this.subscriberCollect.find(e => e === callback); + if (!oldcb) + this.subscriberCollect.push(callback) } } } @@ -43,21 +62,15 @@ export default class Observable { * @param data data to be sent */ send(data: T) { - if (!this.collect) - this.subscriber.forEach(e => e.callback([data])); - else { - this.events.push(data); - if (!this.timeout) { - this.timeout = setTimeout(() => { - this.subscriber.forEach(cb => { - if (cb.one) - this.events.forEach(e => cb.callback([e])); - else - cb.callback(this.events) - }); - this.timeout = undefined; - }, this.collect_intervall); - } + this.subscriber.forEach(e => e(data)); + this.events.push(data); + if (!this.timeout) { + this.timeout = setTimeout(() => { + this.subscriberCollect.forEach(cb => { + cb(this.events) + }); + this.timeout = undefined; + }, this.collect_intervall); } } } \ No newline at end of file