40 lines
1.2 KiB
JavaScript
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
|