mobx-fetch-observable
Version:
170 lines (169 loc) • 5.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FetchObservable = void 0;
var mobx_1 = require("mobx");
var FetchObservable = /** @class */ (function () {
function FetchObservable(initialFetch) {
if (initialFetch === void 0) { initialFetch = undefined; }
Object.defineProperty(this, "_started", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_fetch", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_pending", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "value", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this._started = false;
this._fetch = initialFetch;
this.value = mobx_1.observable.box(undefined, { deep: false });
this._pending = mobx_1.observable.box(false);
}
Object.defineProperty(FetchObservable.prototype, "current", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
var _this = this;
if (this._fetch !== undefined && !this._started) {
this._started = true;
mobx_1._allowStateChanges(true, function () {
_this._pending.set(true);
});
this._fetch(function (newValue) {
mobx_1._allowStateChanges(true, function () {
_this.value.set(newValue);
_this._pending.set(false);
});
});
}
return this.value.get();
}
});
Object.defineProperty(FetchObservable.prototype, "fetch", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
if (this._started)
this._started = false;
return this.current();
}
});
Object.defineProperty(FetchObservable.prototype, "set", {
enumerable: false,
configurable: true,
writable: true,
value: function (newValue) {
var _this = this;
mobx_1.runInAction(function () {
_this.value.set(newValue);
_this._pending.set(false);
});
return this.value.get();
}
});
Object.defineProperty(FetchObservable.prototype, "setFetch", {
enumerable: false,
configurable: true,
writable: true,
value: function (newFetch) {
this._fetch = newFetch;
return this.value.get();
}
});
Object.defineProperty(FetchObservable.prototype, "pending", {
get: function () {
return this._pending.get();
},
enumerable: false,
configurable: true
});
Object.defineProperty(FetchObservable.prototype, "started", {
get: function () {
return this._started && this._fetch !== undefined;
},
enumerable: false,
configurable: true
});
Object.defineProperty(FetchObservable.prototype, "fulfilled", {
get: function () {
return this.value.get() !== undefined;
},
enumerable: false,
configurable: true
});
Object.defineProperty(FetchObservable.prototype, "forEach", {
enumerable: false,
configurable: true,
writable: true,
value: function (run) {
var _this = this;
mobx_1.reaction(function () { return _this.current(); }, function (newValue) {
if (newValue !== undefined)
run(newValue);
}, { fireImmediately: true, name: "fetchObservable-forEach" });
}
});
Object.defineProperty(FetchObservable.prototype, "map", {
enumerable: false,
configurable: true,
writable: true,
value: function (fn) {
var _this = this;
return new FetchObservable(function (sink) {
_this.forEach(function (newValue) {
sink(fn(newValue));
});
});
}
});
Object.defineProperty(FetchObservable.prototype, "flatMap", {
enumerable: false,
configurable: true,
writable: true,
value: function (fn) {
var _this = this;
return new FetchObservable(function (sink) {
_this.forEach(function (newValue) {
fn(newValue).forEach(sink);
});
});
}
});
Object.defineProperty(FetchObservable.prototype, "then", {
enumerable: false,
configurable: true,
writable: true,
value: function (fn) {
var _this = this;
return new FetchObservable(function (sink) {
_this.forEach(function (newValue) {
var maybeObs = fn(newValue);
if (maybeObs instanceof FetchObservable)
maybeObs.forEach(sink);
else
// @ts-ignore
sink(maybeObs);
});
});
}
});
return FetchObservable;
}());
exports.FetchObservable = FetchObservable;