@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
70 lines • 2.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var emission_1 = __importDefault(require("../shared/emission"));
var map_1 = __importDefault(require("../pin/map"));
var value_1 = __importDefault(require("../pin/value"));
var source_1 = __importDefault(require("../pin/source"));
/**
*
* Creates a [map](https://connective.dev/docs/map) pin. This map pin will
* expect objects whose keys matches agents that will be created by given factory.
* For each such object, the factory will be called and a new instance of the agent
* will be created, the provided inputs (key-values of the incoming object) will
* be fed to its inputs, and its first ouput will be passed on.
*
* @param factory the agent factory to create new instances per incoming object
* @param sub a callback to handle the subscription object holding the reference to all
* subscriptions created in response to each incoming object
* @param unsub a callback to handle when the created subscriptions of each incoming
* object are unsubscribed from
* @param outs an optional function to be used to determine possible outputs instead of utilizing
* each created agent's signature.
*
*/
function exec(factory, sub, unsub, outs) {
return map_1.default(function (data, done, error, context) {
var _agent = factory();
var _sources = {};
var _subs = new rxjs_1.Subscription();
var _cleanup = function () {
Object.values(_sources).forEach(function (s) { return s.clear(); });
_agent.clear();
_subs.unsubscribe();
if (unsub)
unsub(_subs);
};
if (data)
Object.keys(data).forEach(function (input) { return _agent.in(input).from(_sources[input] = source_1.default()); });
var _outs = _agent.signature.outputs || [];
if (outs)
_outs = outs();
_outs.forEach(function (label) {
_subs.add(_agent.out(label).subscribe(function (value) { _cleanup(); done({ label: label, value: value }); }, function (err) { _cleanup(); error(err); }));
});
if (sub)
sub(_subs);
if (data)
Object.entries(data).forEach(function (_a) {
var input = _a[0], value = _a[1];
return _sources[input].emit(emission_1.default(value, context));
});
});
}
exports.exec = exec;
/**
*
* Creates an agent using given agent factory, feed its inputs based on key-value
* pairs of given data, and return a pin who will emit the first output of the created agent.
*
* @param factory
* @param data
*
*/
function call(factory, data) { return value_1.default(data).to(exec(factory)); }
exports.call = call;
exports.default = call;
//# sourceMappingURL=call.js.map