@acaprojects/a2-composer
Version:
Angular 2 Interface for composer
130 lines • 4.49 kB
JavaScript
import { COMPOSER } from '../../../settings';
import { StatusVariable } from './status-variable.class';
var Module = (function () {
function Module(srv, parent, name, i) {
var _this = this;
this.index = 0;
this.status_variables = [];
this._debug = false;
this.id = name;
this.service = srv;
this.parent = parent;
this.index = i;
COMPOSER.observe('debug').subscribe(function (data) {
_this._debug = data;
});
}
Module.prototype.bind = function (prop, cb_fn, force) {
var _this = this;
if (force === void 0) { force = false; }
var val = this.get(prop);
setTimeout(function () {
if (val.bindings > 0) {
val.bindings++;
val.add_cb_fn(cb_fn);
val.bound();
}
}, 500);
return new Promise(function (resolve) {
if (val.bindings <= 0 || force) {
_this.service.io.bind(_this.parent.id, _this.id, _this.index, prop)
.then(function (data) {
if (!force) {
val.bindings++;
}
val.add_cb_fn(cb_fn);
resolve(function () { val.unbind(); });
}, function (err) {
resolve(null);
});
}
else {
resolve(function () { val.unbind(); });
}
});
};
Module.prototype.exec = function (fn, prop, args) {
var now = (new Date()).getTime();
if (prop && prop !== '') {
var ids = {
system_id: this.parent.id,
module_name: this.id,
module_index: this.index,
name: prop,
};
var sv_1 = this.get(prop);
if (sv_1.bindings <= 0) {
COMPOSER.error('Module', "Variable \"" + prop + "\"\" not bound!");
return new Promise(function (resolve, reject) {
resolve({
type: 'error',
message: "Error: Variable not bound!, " + fn + ", " + prop,
args: args,
});
});
}
else {
return new Promise(function (resolve, reject) {
sv_1.execs.push({
prop: prop,
fn: fn,
value: args,
resolve: resolve,
reject: reject,
});
});
}
}
else {
return this.service.io.exec(this.parent.id, this.id, this.index, fn, args);
}
};
Module.prototype.unbind = function (prop) {
var val = this.get(prop);
val.bindings--;
if (val.bindings <= 1) {
this.service.io.unbind(this.parent.id, this.id, this.index, prop)
.then(function (data) {
return;
}, function (err) {
val.bindings++;
});
}
};
Module.prototype.debug = function () {
var id = this.service.io.debug(this.parent.id, this.id, this.index);
};
Module.prototype.ignore = function () {
var id = this.service.io.ignore(this.parent.id, this.id, this);
};
Module.prototype.get = function (prop) {
for (var _i = 0, _a = this.status_variables; _i < _a.length; _i++) {
var variable = _a[_i];
if (variable.id === prop) {
return variable;
}
}
var s_var = new StatusVariable(this.service, this, prop, 0);
this.status_variables.push(s_var);
return s_var;
};
Module.prototype.rebind = function () {
for (var _i = 0, _a = this.status_variables; _i < _a.length; _i++) {
var variable = _a[_i];
if (variable.bindings > 0) {
this.bind(variable.id, null, true);
}
}
};
Module.prototype.setDebug = function (debug) {
if (typeof debug !== 'object') {
return false;
}
this.debugger = debug;
COMPOSER.log('Debugger', "Bound to module " + this.id + " " + this.index + " on system " + this.parent.id);
return true;
};
return Module;
}());
export { Module };
//# sourceMappingURL=module.class.js.map