Compare commits

..

No commits in common. "c63ff06453f8a4a8c9cf280ce37b25c96ed4d8a4" and "a8d5382ac3a3b903859c2af04f52bdcac9f92804" have entirely different histories.

9 changed files with 846 additions and 2645 deletions

1980
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/secure-file-wrapper",
"version": "2.3.3",
"version": "2.3.1",
"main": "lib/index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT",
@ -12,18 +12,17 @@
"test": "mocha lib/test.js"
},
"dependencies": {
"@hibas123/utils": "^1.0.1",
"cross-fetch": "^3.0.1",
"cross-fetch": "^3.0.0",
"uuid": "^3.3.2"
},
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/mocha": "^5.2.6",
"@types/node": "^11.10.5",
"@types/node-fetch": "^2.1.6",
"@types/mocha": "^5.2.2",
"@types/node": "^10.12.18",
"@types/node-fetch": "^2.1.4",
"@types/uuid": "^3.4.4",
"chai": "^4.1.2",
"mocha": "^6.0.2",
"typescript": "^3.3.3333"
"mocha": "^5.2.0",
"typescript": "^3.2.2"
}
}

View File

@ -1,6 +1,6 @@
import { Observable, Lock } from "@hibas123/utils";
//this references global on node and window in browser
const fetch = this.fetch ? this.fetch : require("cross-fetch").default;
import Observable from "./observable";
import Lock from "./lock";
import fetch from "cross-fetch"
export interface IFileVersion {
version: string;

36
src/lock.ts Normal file
View 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
View 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);
}
}
}
}