From 858a508190d777fa52e31416621d1f5ea78e99fb Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 13 Apr 2020 03:11:12 +0200 Subject: [PATCH] Remove unnecessary timer from obeservable --- .editorconfig | 6 +++++ package-lock.json | 2 +- package.json | 54 ++++++++++++++++++------------------- src/awaiter.ts | 42 ++++++++++++++++------------- src/index.ts | 22 ++++++++------- src/lock.ts | 10 +++---- src/observable.ts | 68 +++++++++++++++++++++-------------------------- 7 files changed, 106 insertions(+), 98 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ba92aaf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root=true +[*] +charset = utf-8 +indent_size = 3 +indent_style = space +insert_final_newline = true \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5bc9387..985e21c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "2.2.2", + "version": "2.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a815faf..d9d7069 100644 --- a/package.json +++ b/package.json @@ -1,28 +1,28 @@ { - "name": "@hibas123/utils", - "version": "2.2.3", - "description": "Different Utilities, that are not worth own packages", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "scripts": { - "prepublishOnly": "tsc", - "build": "tsc", - "watch-ts": "tsc -w" - }, - "author": "Fabian Stamm ", - "license": "MIT", - "repository": { - "url": "https://git.stamm.me/OpenServer/Utils.git", - "type": "git" - }, - "devDependencies": { - "typescript": "^3.4.5" - }, - "files": [ - "src/", - "lib/", - "tsconfig.json", - "readme.md" - ], - "private": false -} \ No newline at end of file + "name": "@hibas123/utils", + "version": "2.2.4", + "description": "Different Utilities, that are not worth own packages", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "scripts": { + "prepublishOnly": "tsc", + "build": "tsc", + "watch-ts": "tsc -w" + }, + "author": "Fabian Stamm ", + "license": "MIT", + "repository": { + "url": "https://git.stamm.me/OpenServer/Utils.git", + "type": "git" + }, + "devDependencies": { + "typescript": "^3.4.5" + }, + "files": [ + "src/", + "lib/", + "tsconfig.json", + "readme.md" + ], + "private": false +} diff --git a/src/awaiter.ts b/src/awaiter.ts index 72ce391..448434a 100644 --- a/src/awaiter.ts +++ b/src/awaiter.ts @@ -41,24 +41,28 @@ export default class AwaitStore { } /** - * Await a specific value and return. - * + * Await a specific value and return. + * * For example if val = true then this function would block until the value * is actually true. If it is true, then the promise will resolve immediatly - * + * * @param val Value to await */ - awaitValue(val: T): PromiseLike & { catch: (cb: (err: any) => PromiseLike) => PromiseLike, ignore: () => void } { - + awaitValue( + val: T + ): PromiseLike & { + catch: (cb: (err: any) => PromiseLike) => PromiseLike; + ignore: () => void; + } { let ignore: () => void = () => undefined; - let prms = new Promise(yes => { + let prms = new Promise((yes) => { const cb = () => { if (this._value === val) { yes(); this.unsubscribe(cb); } - } + }; this.subscribe(cb); }); @@ -66,23 +70,23 @@ export default class AwaitStore { return { then: prms.then.bind(prms), catch: prms.catch.bind(prms), - ignore: () => ignore() - } + ignore: () => ignore(), + }; } /** - * Creates Public API with subscribe and unsubscribe - * - * @returns {object} - */ + * Creates Public API with subscribe and unsubscribe + * + * @returns {object} + */ getPublicApi() { - if (this.observable.closed) - throw new Error("Observable is closed!"); + if (this.observable.closed) throw new Error("Observable is closed!"); return { subscribe: (callback: ObserverCallback) => this.subscribe(callback), - unsubscribe: (callback: ObserverCallback) => this.unsubscribe(callback), - awaitValue: (value: T) => this.awaitValue(value) - } + unsubscribe: (callback: ObserverCallback) => + this.unsubscribe(callback), + awaitValue: (value: T) => this.awaitValue(value), + }; } /** @@ -91,4 +95,4 @@ export default class AwaitStore { close() { this.observable.close(); } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index dd390cb..f7e77c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,17 @@ import Lock, { Release } from "./lock"; -import Observable, { ObserverCallback, ObserverCallbackCollect, ObservableInterface } from "./observable"; +import Observable, { + ObserverCallback, + ObserverCallbackCollect, + ObservableInterface, +} from "./observable"; import AwaitStore from "./awaiter"; export { - Lock, - Release, - Observable, - ObserverCallback, - ObserverCallbackCollect, - ObservableInterface, - AwaitStore -} \ No newline at end of file + Lock, + Release, + Observable, + ObserverCallback, + ObserverCallbackCollect, + ObservableInterface, + AwaitStore, +}; diff --git a/src/lock.ts b/src/lock.ts index cd7e7b0..fce468c 100755 --- a/src/lock.ts +++ b/src/lock.ts @@ -2,14 +2,14 @@ export type Release = { release: () => void }; /** * Basic Locking mechanism for JavaScript - * + * */ export default class Lock { private _locked: boolean = false; /** * Returns the state of the Locken - * + * * @returns {boolean} */ get locked(): boolean { @@ -23,7 +23,7 @@ export default class Lock { /** * Waits till lock is free and returns a release function - * + * * @return {function} */ async getLock(): Promise { @@ -32,8 +32,8 @@ export default class Lock { return new Promise((resolve) => { this.toCome.push(() => { resolve({ release: this.lock() }); - }) - }) + }); + }); } } diff --git a/src/observable.ts b/src/observable.ts index b9835a4..dd77080 100755 --- a/src/observable.ts +++ b/src/observable.ts @@ -8,9 +8,9 @@ export type ObservableInterface = { */ subscribe: (callback: ObserverCallback) => void; /** - * Unsubscribe fron Observable - * @param {function} callback - */ + * Unsubscribe fron Observable + * @param {function} callback + */ unsubscribe: (callback: ObserverCallback) => void; /** * Subscribe to Observable in collect mode @@ -34,55 +34,51 @@ export default class Observable { return this[ClosedSymbol]; } - constructor(private collect_intervall: number = 100) { } + constructor(private collect_intervall: number = 100) {} subscribe(callback: ObserverCallback) { - if (this[ClosedSymbol]) - throw new Error("Observable is closed!"); + if (this[ClosedSymbol]) throw new Error("Observable is closed!"); - let oldcb = this.subscriber.find(e => e === callback); - if (!oldcb) - this.subscriber.push(callback) + let oldcb = this.subscriber.find((e) => e === callback); + if (!oldcb) this.subscriber.push(callback); return () => this.unsubscribe(callback); } unsubscribe(callback: ObserverCallback | ObserverCallbackCollect) { - if (this[ClosedSymbol]) - return; + if (this[ClosedSymbol]) return; - let idx = this.subscriber.findIndex(e => e === callback); + 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); + idx = this.subscriberCollect.findIndex((e) => e === callback); + if (idx >= 0) this.subscriberCollect.splice(idx, 1); } } subscribeCollect(callback: ObserverCallbackCollect) { - if (this[ClosedSymbol]) - throw new Error("Observable is closed!"); + if (this[ClosedSymbol]) throw new Error("Observable is closed!"); - let oldcb = this.subscriberCollect.find(e => e === callback); - if (!oldcb) - this.subscriberCollect.push(callback) + let oldcb = this.subscriberCollect.find((e) => e === callback); + if (!oldcb) this.subscriberCollect.push(callback); } /** * Creates Public API with subscribe and unsubscribe - * + * * @returns {object} */ getPublicApi(): ObservableInterface { - if (this[ClosedSymbol]) - throw new Error("Observable is closed!"); + if (this[ClosedSymbol]) throw new Error("Observable is closed!"); return { subscribe: (callback: ObserverCallback) => this.subscribe(callback), - unsubscribe: (callback: ObserverCallback | ObserverCallbackCollect) => this.unsubscribe(callback), - subscribeCollect: (callback: ObserverCallbackCollect) => this.subscribeCollect(callback) - } + unsubscribe: ( + callback: ObserverCallback | ObserverCallbackCollect + ) => this.unsubscribe(callback), + subscribeCollect: (callback: ObserverCallbackCollect) => + this.subscribeCollect(callback), + }; } /** @@ -90,22 +86,21 @@ export default class Observable { * @param data data to be sent */ send(data: T) { - if (this[ClosedSymbol]) - throw new Error("Observable is closed!") + if (this[ClosedSymbol]) throw new Error("Observable is closed!"); - Array.from(this.subscriber.values()).forEach(e => { + Array.from(this.subscriber.values()).forEach((e) => { try { - e(data) + e(data); } catch (err) { // Catch error, so it doesn't affect other subscribers - console.error(err) + console.error(err); } }); this.events.push(data); - if (!this.timeout) { + if (!this.timeout && this.subscriberCollect.length > 0) { this.timeout = setTimeout(() => { - this.subscriberCollect.forEach(cb => { - cb(this.events) + this.subscriberCollect.forEach((cb) => { + cb(this.events); }); this.events = []; this.timeout = undefined; @@ -122,7 +117,6 @@ export default class Observable { this.subscriber = []; this.subscriberCollect = []; this.events = []; - if (this.timeout) - clearTimeout(this.timeout) + if (this.timeout) clearTimeout(this.timeout); } -} \ No newline at end of file +}