forked from hibas123/SecureFileWrapper
Compare commits
No commits in common. "c63ff06453f8a4a8c9cf280ce37b25c96ed4d8a4" and "a8d5382ac3a3b903859c2af04f52bdcac9f92804" have entirely different histories.
c63ff06453
...
a8d5382ac3
1980
package-lock.json
generated
1980
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",
|
"name": "@hibas123/secure-file-wrapper",
|
||||||
"version": "2.3.3",
|
"version": "2.3.1",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@ -12,18 +12,17 @@
|
|||||||
"test": "mocha lib/test.js"
|
"test": "mocha lib/test.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hibas123/utils": "^1.0.1",
|
"cross-fetch": "^3.0.0",
|
||||||
"cross-fetch": "^3.0.1",
|
|
||||||
"uuid": "^3.3.2"
|
"uuid": "^3.3.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chai": "^4.1.4",
|
"@types/chai": "^4.1.4",
|
||||||
"@types/mocha": "^5.2.6",
|
"@types/mocha": "^5.2.2",
|
||||||
"@types/node": "^11.10.5",
|
"@types/node": "^10.12.18",
|
||||||
"@types/node-fetch": "^2.1.6",
|
"@types/node-fetch": "^2.1.4",
|
||||||
"@types/uuid": "^3.4.4",
|
"@types/uuid": "^3.4.4",
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.1.2",
|
||||||
"mocha": "^6.0.2",
|
"mocha": "^5.2.0",
|
||||||
"typescript": "^3.3.3333"
|
"typescript": "^3.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { Observable, Lock } from "@hibas123/utils";
|
import Observable from "./observable";
|
||||||
//this references global on node and window in browser
|
import Lock from "./lock";
|
||||||
const fetch = this.fetch ? this.fetch : require("cross-fetch").default;
|
import fetch from "cross-fetch"
|
||||||
|
|
||||||
export interface IFileVersion {
|
export interface IFileVersion {
|
||||||
version: string;
|
version: string;
|
||||||
|
36
src/lock.ts
Normal file
36
src/lock.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
src/observable.ts
Normal file
38
src/observable.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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