UNPKG

@acaprojects/a2-composer

Version:
130 lines 4.36 kB
import { Observable } from 'rxjs/Observable'; import { COMPOSER } from '../../../settings'; var EXEC_LIMIT = 10; var EXEC_TIME_DELAY = 100; var StatusVariable = (function () { function StatusVariable(srv, parent, name, init_val) { var _this = this; this.previous = null; this.current = null; this.bindings = 0; this.exec_active = false; this.execs = []; this.value = {}; this.local_change = false; this.callbacks = []; this.obs = null; this.view = null; this.id = name; this.previous = init_val; this.current = init_val; this.service = srv; this.parent = parent; this.value = { system_id: this.parent.parent.id, module_name: this.parent.id, module_index: this.parent.index, name: this.id, }; setInterval(function () { _this.exec(); }, 1000 / EXEC_LIMIT); } Object.defineProperty(StatusVariable.prototype, "observe", { get: function () { if (!this.obs) { return this.bound(); } else { return this.obs; } }, enumerable: true, configurable: true }); StatusVariable.prototype.bound = function () { var _this = this; var mod = this.parent.id + " " + this.parent.index; var msg = "Bound to " + this.id + " on " + mod + " in " + this.parent.parent.id + ", Value:"; COMPOSER.log('BIND', msg, this.current); if (!this.obs) { this.obs = new Observable(function (observer) { var val = _this.current; _this.view = observer; setInterval(function () { if (_this.bindings === 0) { observer.complete(); _this.obs = null; _this.view = null; } if (_this.current !== val) { val = _this.current; observer.next(_this.current); } }, 100); }); } setTimeout(function () { if (_this.view) { _this.view.next(_this.current); } _this.callback(); }, 200); return this.obs; }; StatusVariable.prototype.exec = function () { if (this.execs.length > 0) { var mod = this.parent; var e_1 = this.execs[this.execs.length - 1]; if (this.current !== e_1.value) { this.service.io.exec(mod.parent.id, mod.id, mod.index, e_1.fn, e_1.value) .then(function (data) { e_1.resolve(data); }, function (err) { e_1.reject(err); }); } this.execs = []; } }; StatusVariable.prototype.update = function (params) { return; }; StatusVariable.prototype.unbind = function () { COMPOSER.log('VAR', "Unbound binding. " + (this.bindings - 1) + " remaining."); if (this.bindings <= 1) { this.parent.unbind(this.id); } }; StatusVariable.prototype.add_cb_fn = function (cb_fn) { if (cb_fn !== undefined || cb_fn !== null) { this.callbacks.push(cb_fn); } }; StatusVariable.prototype.success = function (msg) { if (msg.meta.cmd === 'exec') { this.previous = this.current; this.current = msg.meta.args; this.local_change = true; this.exec_active = false; this.callback(); this.exec(); } }; StatusVariable.prototype.error = function (msg) { return; }; StatusVariable.prototype.notify = function (msg) { this.local_change = false; this.previous = this.current; this.current = msg.value; this.callback(); }; StatusVariable.prototype.callback = function () { for (var _i = 0, _a = this.callbacks; _i < _a.length; _i++) { var cb = _a[_i]; if (cb instanceof Function) { cb(this.current, this.previous); } } }; return StatusVariable; }()); export { StatusVariable }; //# sourceMappingURL=status-variable.class.js.map