This repository has been archived on 2019-08-30. You can view files and clone it, but cannot push or open issues or pull requests.
SecureFileWrapper/lib/observable.js

40 lines
1.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Observable {
constructor(collect = true, collect_intervall = 100) {
this.collect = collect;
this.collect_intervall = collect_intervall;
this.subscriber = [];
this.events = [];
this.timeout = undefined;
}
getPublicApi() {
return {
subscribe: (callback) => {
if (this.subscriber.indexOf(callback) < 0)
this.subscriber.push(callback);
},
unsubscribe: (callback) => {
let idx = this.subscriber.indexOf(callback);
if (idx >= 0) {
this.subscriber.splice(idx, 1);
}
}
};
}
send(data) {
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);
}
}
}
}
exports.default = Observable;
//# sourceMappingURL=observable.js.map