@platform/state
Version:
A small, simple, strongly typed, [rx/observable] state-machine.
140 lines (139 loc) • 5.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateObject = void 0;
var tslib_1 = require("tslib");
var util_value_1 = require("@platform/util.value");
var immer_1 = require("immer");
var rxjs_1 = require("rxjs");
var common_1 = require("../common");
var Patch_1 = require("../Patch");
var combine = require("./StateObject.combine");
var events = require("./StateObject.events");
if (typeof immer_1.setAutoFreeze === 'function') {
(0, immer_1.setAutoFreeze)(false);
}
var StateObject = (function () {
function StateObject(args) {
var _this = this;
this._dispose$ = new rxjs_1.Subject();
this.dispose$ = this._dispose$.asObservable();
this._event$ = new rxjs_1.Subject();
this.event = events.create(this._event$, this._dispose$);
this.change = function (fn) {
var cid = (0, util_value_1.slug)();
var from = _this.state;
var res = Patch_1.Patch.change(from, fn);
var patches = res.patches, op = res.op;
var to = res.to;
if (Patch_1.Patch.isEmpty(patches)) {
return { op: op, cid: cid, patches: patches };
}
else {
return _this._changeComplete({ cid: cid, from: from, to: to, op: op, patches: patches });
}
};
this.changeAsync = function (fn) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var cid, from, next;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
cid = (0, util_value_1.slug)();
from = this.state;
return [4, Patch_1.Patch.changeAsync(from, fn)];
case 1:
next = _a.sent();
if (Patch_1.Patch.isEmpty(next.patches)) {
return [2, { op: next.op, cid: cid, patches: next.patches }];
}
else {
return [2, this._changeComplete({
cid: cid,
from: from,
to: next.to,
op: next.op,
patches: next.patches,
})];
}
return [2];
}
});
}); };
this.fire = function (e) { return _this._event$.next(e); };
this._state = tslib_1.__assign({}, args.initial);
this.original = this.state;
}
StateObject.create = function (initial) {
return new StateObject({ initial: initial });
};
StateObject.readonly = function (obj) {
return obj;
};
StateObject.toObject = function (input) {
return (0, immer_1.isDraft)(input) ? (0, immer_1.original)(input) : input;
};
StateObject.isStateObject = function (input) {
return common_1.is.stateObject(input);
};
StateObject.isProxy = function (input) {
return (0, immer_1.isDraft)(input);
};
StateObject.prototype.dispose = function () {
if (!this.isDisposed) {
this.fire({
type: 'StateObject/disposed',
payload: { original: this.original, final: this.state },
});
this._dispose$.next();
this._dispose$.complete();
}
};
Object.defineProperty(StateObject.prototype, "isDisposed", {
get: function () {
return this._dispose$.isStopped;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StateObject.prototype, "state", {
get: function () {
return this._state;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StateObject.prototype, "readonly", {
get: function () {
return this;
},
enumerable: false,
configurable: true
});
StateObject.prototype._changeComplete = function (args) {
var cid = args.cid, op = args.op, patches = args.patches, from = args.from, to = args.to;
var changing = {
op: op,
cid: cid,
from: from,
to: to,
patches: patches,
cancelled: false,
cancel: function () { return (changing.cancelled = true); },
};
this.fire({ type: 'StateObject/changing', payload: changing });
var cancelled = changing.cancelled ? changing : undefined;
if (cancelled) {
this.fire({ type: 'StateObject/cancelled', payload: cancelled });
}
var changed = cancelled
? undefined
: { op: op, cid: cid, from: from, to: to, patches: patches };
if (changed) {
this._state = to;
this.fire({ type: 'StateObject/changed', payload: changed });
}
return { op: op, cid: cid, changed: changed, cancelled: cancelled, patches: patches };
};
StateObject.combine = combine.create(StateObject.create);
return StateObject;
}());
exports.StateObject = StateObject;