vscroll
Version:
Virtual scroll engine
70 lines • 1.88 kB
JavaScript
import { __read, __values } from "tslib";
var Reactive = /** @class */ (function () {
function Reactive(value, options) {
this.id = 0;
if (value !== void 0) {
this.value = value;
this.initialValue = value;
}
this.options = options || {};
this.subscriptions = new Map();
}
Reactive.prototype.set = function (value) {
var e_1, _a;
if (this.value === value && !this.options.emitEqual) {
return;
}
this.value = value;
try {
for (var _b = __values(this.subscriptions), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), sub = _d[1];
sub.emit(value);
if (this.value !== value) {
break;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
Reactive.prototype.get = function () {
return this.value;
};
Reactive.prototype.on = function (func) {
var _this = this;
var id = this.id++;
var subscription = {
emit: func,
off: function () {
subscription.emit = function () { return null; };
_this.subscriptions.delete(id);
}
};
this.subscriptions.set(id, subscription);
if (this.options.emitOnSubscribe) {
subscription.emit(this.value);
}
return function () { return subscription.off(); };
};
Reactive.prototype.once = function (func) {
var off = this.on(function (v) {
off();
func(v);
});
return off;
};
Reactive.prototype.reset = function () {
this.set(this.initialValue);
};
Reactive.prototype.dispose = function () {
this.subscriptions.forEach(function (sub) { return sub.off(); });
};
return Reactive;
}());
export { Reactive };
//# sourceMappingURL=reactive.js.map