forked from hibas123/SecureFileWrapper
Switched to @hibas123/utils package for observable and lock
This commit is contained in:
parent
a8d5382ac3
commit
8e0b859408
1982
package-lock.json
generated
1982
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
15
package.json
15
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/secure-file-wrapper",
|
||||
"version": "2.3.1",
|
||||
"version": "2.3.2",
|
||||
"main": "lib/index.js",
|
||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||
"license": "MIT",
|
||||
@ -12,17 +12,18 @@
|
||||
"test": "mocha lib/test.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"cross-fetch": "^3.0.0",
|
||||
"@hibas123/utils": "^1.0.1",
|
||||
"cross-fetch": "^3.0.1",
|
||||
"uuid": "^3.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.1.4",
|
||||
"@types/mocha": "^5.2.2",
|
||||
"@types/node": "^10.12.18",
|
||||
"@types/node-fetch": "^2.1.4",
|
||||
"@types/mocha": "^5.2.6",
|
||||
"@types/node": "^11.10.5",
|
||||
"@types/node-fetch": "^2.1.6",
|
||||
"@types/uuid": "^3.4.4",
|
||||
"chai": "^4.1.2",
|
||||
"mocha": "^5.2.0",
|
||||
"typescript": "^3.2.2"
|
||||
"mocha": "^6.0.2",
|
||||
"typescript": "^3.3.3333"
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import Observable from "./observable";
|
||||
import Lock from "./lock";
|
||||
import { Observable, Lock } from "@hibas123/utils";
|
||||
import fetch from "cross-fetch"
|
||||
|
||||
export interface IFileVersion {
|
||||
|
36
src/lock.ts
36
src/lock.ts
@ -1,36 +0,0 @@
|
||||
export type Release = { release: () => void };
|
||||
export default class Lock {
|
||||
private _locked: boolean = false;
|
||||
get locked() {
|
||||
return this._locked;
|
||||
}
|
||||
private toCome: (() => void)[] = [];
|
||||
|
||||
constructor() {
|
||||
this.release = this.release.bind(this);
|
||||
}
|
||||
|
||||
async getLock(): Promise<Release> {
|
||||
if (!this._locked) return { release: this.lock() };
|
||||
else {
|
||||
return new Promise<Release>((resolve) => {
|
||||
this.toCome.push(() => {
|
||||
resolve({ release: this.lock() });
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private lock() {
|
||||
this._locked = true;
|
||||
return this.release;
|
||||
}
|
||||
|
||||
private async release() {
|
||||
if (this.toCome.length > 0) {
|
||||
this.toCome.shift()();
|
||||
} else {
|
||||
this._locked = false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
export type ObserverCallback<T> = (data: T) => void;
|
||||
|
||||
export default class Observable<T = any> {
|
||||
private subscriber: ObserverCallback<T[]>[] = [];
|
||||
private events: T[] = [];
|
||||
private timeout = undefined;
|
||||
|
||||
constructor(private collect: boolean = true, private collect_intervall: number = 100) { }
|
||||
|
||||
getPublicApi() {
|
||||
return {
|
||||
subscribe: (callback: ObserverCallback<T[]>) => {
|
||||
if (this.subscriber.indexOf(callback) < 0)
|
||||
this.subscriber.push(callback)
|
||||
},
|
||||
unsubscribe: (callback: ObserverCallback<T[]>) => {
|
||||
let idx = this.subscriber.indexOf(callback);
|
||||
if (idx >= 0) {
|
||||
this.subscriber.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
send(data: T) {
|
||||
if (!this.collect)
|
||||
this.subscriber.forEach(e => e([data]));
|
||||
else {
|
||||
this.events.push(data);
|
||||
if (!this.timeout) {
|
||||
this.timeout = setTimeout(() => {
|
||||
this.subscriber.forEach(e => e(this.events));
|
||||
this.timeout = 0;
|
||||
}, this.collect_intervall);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user