camelot-unchained
Version:
Camelot Unchained Client Library
58 lines (50 loc) • 2.29 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var RestAPI_1 = require('../../restapi/RestAPI');
var defaultTopics_1 = require('../defaultTopics');
var ControlGame_1 = require('../../core/classes/ControlGame');
var POLL_INTERVAL = 5000;
var timer = void 0;
function run(emitter, topic) {
function tick() {
// TODO: switch to using cu-restapi
RestAPI_1.getControlGame(true).then(function (data) {
var instance = new ControlGame_1.default(data);
emitter.emit(topic, instance);
}).catch(function (error) {
emitter.emit(topic, { error: { status: error.response.status, reason: error.message } });
});
}
if (!timer) {
setInterval(tick, POLL_INTERVAL);
}
}
var ControlGameListener = function () {
function ControlGameListener() {
_classCallCheck(this, ControlGameListener);
this.listening = false;
this.topic = defaultTopics_1.clientEventTopics.handlesControlGame;
}
_createClass(ControlGameListener, [{
key: 'start',
value: function start(emitter) {
if (!this.listening) {
this.listening = true;
run(emitter, this.topic);
}
}
}, {
key: 'stop',
value: function stop() {
if (timer) {
clearTimeout(timer);
timer = null;
this.listening = false;
}
}
}]);
return ControlGameListener;
}();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ControlGameListener;