@acaprojects/a2-composer
Version:
Angular 2 Interface for composer
349 lines • 13.6 kB
JavaScript
import { watch } from 'watch-object';
var BIND = 'bind';
var UNBIND = 'unbind';
var DEBUG = 'debug';
var IGNORE = 'ignore';
var PONG = 'pong';
var EXEC = 'exec';
var SUCCESS = 'success';
var ERROR = 'error';
var NOTIFY = 'notify';
var SECONDS = 1000;
var RECONNECT_TIMER_SECONDS = 5 * SECONDS;
var KEEP_ALIVE_TIMER_SECONDS = 60 * SECONDS;
import { COMPOSER } from '../settings';
var MockWebSocketInterface = (function () {
function MockWebSocketInterface(srv, auth, fixed, host, port) {
if (fixed === void 0) { fixed = false; }
if (host === void 0) { host = location.hostname; }
if (port === void 0) { port = '3000'; }
var _this = this;
this.req_id = 0;
this.connected = true;
this.reconnected = false;
this.connect_check = null;
this.connect_promise = null;
this.connecting = false;
this.requests = {};
this.fixed = false;
this.watching = {};
this.systems = [];
this.fixed = fixed;
this.serv = srv;
COMPOSER.observe('control').subscribe(function (data) {
_this.systems = data;
});
this.setup(auth, host, port);
}
MockWebSocketInterface.prototype.setup = function (auth, host, port) {
if (host === void 0) { host = location.hostname; }
if (port === void 0) { port = '3000'; }
this.auth = auth;
var protocol = (port === '443' ? 'wss://' : 'ws://');
var use_port = (port === '80' || port === '443' ? '' : (':' + port));
this.end_point = protocol + host + use_port;
this.uri = this.end_point + '/control/websocket';
this.setupSystems();
};
MockWebSocketInterface.prototype.onopen = function (evt) {
this.connected = true;
COMPOSER.log('WS(M)', 'Connected');
this.startKeepAlive();
if (this.reconnected) {
this.serv.rebind();
}
this.reconnected = false;
};
MockWebSocketInterface.prototype.onclose = function (evt) {
this.connected = false;
COMPOSER.log('WS(M)', 'Closed');
this.io = null;
this.stopKeepAlive();
};
MockWebSocketInterface.prototype.bind = function (sys_id, mod_id, i, name, callback) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.sendRequest(BIND, sys_id, mod_id, i, name, null)
.then(function (id) {
_this.requests[id] = {
resolve: resolve,
reject: reject,
};
});
});
};
MockWebSocketInterface.prototype.unbind = function (sys_id, mod_id, i, name, callback) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.sendRequest(UNBIND, sys_id, mod_id, i, name, null)
.then(function (id) {
_this.requests[id] = {
resolve: resolve,
reject: reject,
};
});
});
};
MockWebSocketInterface.prototype.exec = function (sys_id, mod_id, i, fn, args) {
var _this = this;
return new Promise(function (resolve, reject) {
var id = _this.sendRequest(EXEC, sys_id, mod_id, i, fn, args);
_this.requests[id] = {
resolve: resolve,
reject: reject,
};
});
};
MockWebSocketInterface.prototype.debug = function (sys_id, mod_id, i) {
return this.sendRequest(DEBUG, sys_id, mod_id, i, DEBUG);
};
MockWebSocketInterface.prototype.ignore = function (sys_id, mod_id, inst) {
return this.sendRequest(IGNORE, sys_id, mod_id, null, IGNORE);
};
MockWebSocketInterface.prototype.setupSystems = function () {
var _this = this;
if (!this.systems || this.systems.length <= 0) {
setTimeout(function () {
_this.setupSystems();
}, 200);
}
};
MockWebSocketInterface.prototype.connect = function () {
var _this = this;
if (!this.connect_promise) {
this.connect_promise = new Promise(function (resolve, reject) {
if (_this.connecting) {
reject({ message: 'Already attempting to connect to websocket.' });
_this.connect_promise = null;
return;
}
_this.connecting = true;
setTimeout(function () {
_this.onopen({});
setTimeout(function () { _this.connecting = false; }, 100);
if (!_this.connect_check) {
_this.connect_check = setInterval(function () { _this.reconnect(); }, 3 * 1000);
}
}, Math.floor(Math.random() * 1000) + 200);
});
}
return this.connect_promise;
};
MockWebSocketInterface.prototype.reconnect = function () {
return;
};
MockWebSocketInterface.prototype.startKeepAlive = function () {
var _this = this;
this.keepAliveInterval = setInterval(function () {
_this.onmessage({ data: PONG });
}, KEEP_ALIVE_TIMER_SECONDS);
};
MockWebSocketInterface.prototype.stopKeepAlive = function () {
clearInterval(this.keepAliveInterval);
};
MockWebSocketInterface.prototype.onmessage = function (evt) {
var msg;
var meta;
var system;
var module;
var binding;
if (evt.data === PONG || !evt.data) {
return;
}
else {
msg = JSON.parse(evt.data);
}
if (msg.type === SUCCESS || msg.type === ERROR || msg.type === NOTIFY) {
meta = msg.meta;
var meta_list = msg.type + "(" + meta.id + "). " + meta.sys + ", " + meta.mod + " " + meta.index + ", " + meta.name;
COMPOSER.log('WS(M)', "Recieved " + meta_list, msg.value);
if (msg.type === SUCCESS) {
if (this.requests[msg.id] && this.requests[msg.id].resolve) {
this.requests[msg.id].resolve(msg.value);
}
}
else if (msg.type === ERROR) {
if (this.requests[msg.id] && this.requests[msg.id].resolve) {
this.requests[msg.id].reject(msg.msg);
}
}
if (this.requests[msg.id]) {
delete this.requests[msg.id];
}
if (!meta) {
return this.fail(msg, 'meta');
}
system = this.serv.get(meta.sys);
if (!system) {
return this.fail(msg, 'system');
}
module = system.get(meta.mod, meta.index);
if (!module) {
return this.fail(msg, 'module');
}
binding = module.get(meta.name);
if (!binding) {
return this.fail(msg, 'binding');
}
else {
binding[msg.type](msg);
}
}
else if (msg.type === 'debug') {
return true;
}
return true;
};
MockWebSocketInterface.prototype.fail = function (msg, type) {
COMPOSER.log('WS(M)', "Failed(" + type + "): " + JSON.stringify(msg));
return false;
};
MockWebSocketInterface.prototype.sendRequest = function (type, system, mod, index, name, args) {
var _this = this;
if (args === void 0) { args = []; }
return new Promise(function (resolve) {
if (!_this.connected) {
COMPOSER.log('WS(M)', 'Not connected to websocket. Attempting to connect to websocket');
return _this.connect().then(function () {
setTimeout(function () {
_this.sendRequest(type, system, mod, index, name, args).then(function (id) {
resolve(id);
});
}, 200);
}, function () { return -1; });
}
_this.req_id += 1;
if (!(args instanceof Array)) {
args = [args];
}
var request = {
id: _this.req_id,
cmd: type,
sys: system,
mod: mod,
index: index,
name: name,
args: args,
};
COMPOSER.log('WS(M)', "Sent " + type + " request(" + _this.req_id + "). " + system + ", " + mod + " " + index + ", " + name, args);
if (args !== null) {
request.args = args;
}
setTimeout(function () {
_this.respondTo(type, request);
}, 200);
resolve(_this.req_id);
});
};
MockWebSocketInterface.prototype.notifyChange = function (r, value) {
var _this = this;
var evt_ex = { data: JSON.stringify({
id: r.id,
type: NOTIFY,
meta: r,
value: value,
}) };
setTimeout(function () {
_this.onmessage(evt_ex);
}, 100);
};
MockWebSocketInterface.prototype.respondTo = function (type, r) {
var _this = this;
var evt = {};
var evt_ex = null;
switch (type) {
case BIND:
if (this.systems && this.systems[r.sys] && this.systems[r.sys][r.mod] &&
this.systems[r.sys][r.mod][r.index - 1]) {
var val = this.systems[r.sys][r.mod][r.index - 1][r.name];
evt = { data: JSON.stringify({
id: r.id,
type: SUCCESS,
meta: r,
value: val === undefined ? null : val,
}) };
evt_ex = { data: JSON.stringify({
id: r.id,
type: NOTIFY,
meta: r,
value: val === undefined ? null : val,
}) };
if (!this.watching[r.sys + "," + r.mod + "," + (r.index - 1) + "," + r.name]) {
this.watching[r.sys + "," + r.mod + "," + (r.index - 1) + "," + r.name] = true;
setTimeout(function () {
watch(_this.systems[r.sys][r.mod][r.index - 1], r.name, function (newval, oldval) {
_this.notifyChange(r, newval);
});
}, 100);
}
else {
setTimeout(function () {
_this.notifyChange(r, _this.systems[r.sys][r.mod][r.index - 1][r.name]);
});
}
}
break;
case UNBIND:
if (this.systems && this.systems[r.sys] && this.systems[r.sys][r.mod]) {
evt = { data: JSON.stringify({
id: r.id,
type: SUCCESS,
meta: r,
value: this.systems[r.sys][r.mod][r.index - 1][r.name],
}) };
if (this.watching[r.sys + "," + r.mod + "," + (r.index - 1) + "," + r.name]) {
}
}
break;
case EXEC:
if (this.systems && this.systems[r.sys] && this.systems[r.sys][r.mod] &&
this.systems[r.sys][r.mod][r.index - 1]) {
if (this.systems[r.sys][r.mod][r.index - 1].$system === undefined) {
this.systems[r.sys][r.mod][r.index - 1].$system = this.systems[r.sys];
}
var fn = this.systems[r.sys][r.mod][r.index - 1]["$" + r.name];
if (fn instanceof Function) {
evt = { data: JSON.stringify({
id: r.id,
type: SUCCESS,
meta: r,
value: fn.apply(this.systems[r.sys][r.mod][r.index - 1], r.args),
}) };
}
else {
this.systems[r.sys][r.mod][r.index - 1][r.name] = r.args[0];
evt = { data: JSON.stringify({
id: r.id,
type: SUCCESS,
meta: r,
value: this.systems[r.sys][r.mod][r.index - 1][r.name],
}) };
}
}
break;
case DEBUG:
evt = {
data: JSON.stringify({
id: r.id,
type: SUCCESS,
meta: r,
value: r.args[0],
}),
};
break;
default:
break;
}
this.onmessage(evt);
if (evt_ex) {
setTimeout(function () {
_this.onmessage(evt_ex);
}, 100);
}
};
return MockWebSocketInterface;
}());
export { MockWebSocketInterface };
MockWebSocketInterface.retries = 0;
export var $WebSocketMock = MockWebSocketInterface;
//# sourceMappingURL=websocket.mock.js.map