@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
182 lines • 7.13 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var random_tag_1 = __importDefault(require("../util/random-tag"));
var emission_1 = require("../shared/emission");
var map_1 = require("../pin/map");
var sink_1 = require("../pin/sink");
var group_1 = require("../pin/group");
var filter_1 = require("../pin/filter");
var source_1 = require("../pin/source");
var agent_1 = require("./agent");
var state_1 = require("./state");
/**
*
* Represents non-keyed (simple) [deep states](https://connective.dev/docs/deep).
*
*/
var SimpleDeep = /** @class */ (function (_super) {
__extends(SimpleDeep, _super);
/**
*
* @param stateOrAccessor underlying state of this deep state or a state tree accessor (for sub-states)
* @param compare equality function used to detect changes. If state is passed as first argument this is ignored.
* @param signature the signature of the state, to be overriden by child classes.
*
*/
function SimpleDeep(stateOrAccessor, compare, signature) {
var _this_1 = _super.call(this, signature || {
inputs: ['value'],
outputs: ['value']
}) || this;
_this_1.bound = false;
_this_1.reemit = new source_1.Source();
_this_1.downPropageteKey = random_tag_1.default();
if (stateOrAccessor instanceof state_1.State)
_this_1.state = stateOrAccessor;
else {
_this_1.accessor = stateOrAccessor;
_this_1.state = new state_1.State(_this_1.accessor.initial, compare);
_this_1.accessor.get
.to(map_1.map(function (_, done, __, context) {
context[_this_1.downPropageteKey] = true;
done(_);
}))
.to(_this_1)
.to(filter_1.filter(function (_, done, __, context) {
var downPropagated = context[_this_1.downPropageteKey];
delete context[_this_1.downPropageteKey];
done(!downPropagated);
}))
.to(_this_1.accessor.set);
}
return _this_1;
}
/**
*
* Creates a sub-state for given index/property.
* [Read this](https://connective.dev/docs/deep) for more details
*
* @param index
* @param factory the factory function to be used to construct the sub-state
*
*/
SimpleDeep.prototype.sub = function (index, factory) {
var _this_1 = this;
var initialized = false;
var _this = this;
var _factory = factory || (function (accessor, compare) { return new SimpleDeep(accessor, compare); });
return _factory({
initial: (_this.value || [])[index],
get: _this.output.to(map_1.map(function (v) { return (v || [])[index]; })),
set: sink_1.sink(function (v, context) {
var _a, _b;
try {
if (!_this.value)
_this.value = [];
if (_this_1.accessor) {
_this.value = (Array.isArray(_this.value)) ?
Object.assign([], _this.value, (_a = {}, _a[index] = v, _a)) :
Object.assign({}, _this.value, (_b = {}, _b[index] = v, _b));
}
else {
_this.value[index] = v;
if (initialized)
_this_1.reemit.emit(emission_1.emission(v, context));
else
initialized = true;
}
}
catch (_) { }
}),
bind: function (track) { return track(this.set.subscribe()); },
}, this.state.compare);
};
Object.defineProperty(SimpleDeep.prototype, "value", {
/**
*
* Allows reading or updating state's value directly.
*
*/
get: function () { return this.state.value; },
set: function (v) { this.state.value = v; },
enumerable: true,
configurable: true
});
Object.defineProperty(SimpleDeep.prototype, "compare", {
/**
*
* The equality function used by this deep state. Is used for change detection.
*
*/
get: function () { return this.state.compare; },
enumerable: true,
configurable: true
});
Object.defineProperty(SimpleDeep.prototype, "input", {
/**
*
* Shortcut for `.in('value')`, on which the state receives new values.
* [Read this](https://connective.dev/docs/state#signature) for more details.
*
*/
get: function () { return this.in('value'); },
enumerable: true,
configurable: true
});
Object.defineProperty(SimpleDeep.prototype, "output", {
/**
*
* Shortcut for `.out('value')`, on which the state emits new values.
* [Read this](https://connective.dev/docs/state#signature) for more details.
*
*/
get: function () { return this.out('value'); },
enumerable: true,
configurable: true
});
/**
*
* Binds the underlying state. If this is a sub-state, it will also
* allow up-propagation of state value, causing the parent state to pick up
* changes made to the value of this sub-state. [Read this](https://connective.dev/docs/deep#two-way-data)
* for more details and examples.
*
*/
SimpleDeep.prototype.bind = function () {
var _this_1 = this;
if (!this.bound) {
if (this.accessor)
this.accessor.bind(function (sub) { return _this_1.track(sub); });
else
this.track(this.output.subscribe());
this.bound = true;
}
return this;
};
SimpleDeep.prototype.createOutput = function (_) {
var _this_1 = this;
this.checkOutput(_);
return group_1.group(this.input.to(this.state), this.reemit.to(map_1.map(function () { return _this_1.value; })));
};
SimpleDeep.prototype.createEntries = function () { return [this.input]; };
SimpleDeep.prototype.createExits = function () { return [this.output]; };
return SimpleDeep;
}(agent_1.Agent));
exports.SimpleDeep = SimpleDeep;
//# sourceMappingURL=simple-deep.js.map