Remove unnecessary timer from obeservable
This commit is contained in:
parent
2545f3a050
commit
858a508190
6
.editorconfig
Normal file
6
.editorconfig
Normal file
@ -0,0 +1,6 @@
|
||||
root=true
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_size = 3
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/utils",
|
||||
"version": "2.2.2",
|
||||
"version": "2.2.3",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
54
package.json
54
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 <dev@fabianstamm.de>",
|
||||
"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
|
||||
}
|
||||
"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 <dev@fabianstamm.de>",
|
||||
"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
|
||||
}
|
||||
|
@ -41,24 +41,28 @@ export default class AwaitStore<T = any> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<void> & { catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>, ignore: () => void } {
|
||||
|
||||
awaitValue(
|
||||
val: T
|
||||
): PromiseLike<void> & {
|
||||
catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>;
|
||||
ignore: () => void;
|
||||
} {
|
||||
let ignore: () => void = () => undefined;
|
||||
|
||||
let prms = new Promise<void>(yes => {
|
||||
let prms = new Promise<void>((yes) => {
|
||||
const cb = () => {
|
||||
if (this._value === val) {
|
||||
yes();
|
||||
this.unsubscribe(cb);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.subscribe(cb);
|
||||
});
|
||||
@ -66,23 +70,23 @@ export default class AwaitStore<T = any> {
|
||||
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<T>) => this.subscribe(callback),
|
||||
unsubscribe: (callback: ObserverCallback<T>) => this.unsubscribe(callback),
|
||||
awaitValue: (value: T) => this.awaitValue(value)
|
||||
}
|
||||
unsubscribe: (callback: ObserverCallback<T>) =>
|
||||
this.unsubscribe(callback),
|
||||
awaitValue: (value: T) => this.awaitValue(value),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,4 +95,4 @@ export default class AwaitStore<T = any> {
|
||||
close() {
|
||||
this.observable.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
src/index.ts
22
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
|
||||
}
|
||||
Lock,
|
||||
Release,
|
||||
Observable,
|
||||
ObserverCallback,
|
||||
ObserverCallbackCollect,
|
||||
ObservableInterface,
|
||||
AwaitStore,
|
||||
};
|
||||
|
10
src/lock.ts
10
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<Release> {
|
||||
@ -32,8 +32,8 @@ export default class Lock {
|
||||
return new Promise<Release>((resolve) => {
|
||||
this.toCome.push(() => {
|
||||
resolve({ release: this.lock() });
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,9 +8,9 @@ export type ObservableInterface<T> = {
|
||||
*/
|
||||
subscribe: (callback: ObserverCallback<T>) => void;
|
||||
/**
|
||||
* Unsubscribe fron Observable
|
||||
* @param {function} callback
|
||||
*/
|
||||
* Unsubscribe fron Observable
|
||||
* @param {function} callback
|
||||
*/
|
||||
unsubscribe: (callback: ObserverCallback<T>) => void;
|
||||
/**
|
||||
* Subscribe to Observable in collect mode
|
||||
@ -34,55 +34,51 @@ export default class Observable<T = any> {
|
||||
return this[ClosedSymbol];
|
||||
}
|
||||
|
||||
constructor(private collect_intervall: number = 100) { }
|
||||
constructor(private collect_intervall: number = 100) {}
|
||||
|
||||
subscribe(callback: ObserverCallback<T>) {
|
||||
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<T> | ObserverCallbackCollect<T>) {
|
||||
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<T>) {
|
||||
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<T> {
|
||||
if (this[ClosedSymbol])
|
||||
throw new Error("Observable is closed!");
|
||||
if (this[ClosedSymbol]) throw new Error("Observable is closed!");
|
||||
return {
|
||||
subscribe: (callback: ObserverCallback<T>) => this.subscribe(callback),
|
||||
unsubscribe: (callback: ObserverCallback<T> | ObserverCallbackCollect<T>) => this.unsubscribe(callback),
|
||||
subscribeCollect: (callback: ObserverCallbackCollect<T>) => this.subscribeCollect(callback)
|
||||
}
|
||||
unsubscribe: (
|
||||
callback: ObserverCallback<T> | ObserverCallbackCollect<T>
|
||||
) => this.unsubscribe(callback),
|
||||
subscribeCollect: (callback: ObserverCallbackCollect<T>) =>
|
||||
this.subscribeCollect(callback),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,22 +86,21 @@ export default class Observable<T = any> {
|
||||
* @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<T = any> {
|
||||
this.subscriber = [];
|
||||
this.subscriberCollect = [];
|
||||
this.events = [];
|
||||
if (this.timeout)
|
||||
clearTimeout(this.timeout)
|
||||
if (this.timeout) clearTimeout(this.timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user