meses-messaging
Version:
Meses messaging SDK in JavaScript
86 lines (70 loc) • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 ConversationUpdateHandler = function () {
function ConversationUpdateHandler(service, username, identifier, callback, timeout) {
_classCallCheck(this, ConversationUpdateHandler);
this._service = service;
this._username = username;
this._identifier = identifier;
this._callback = callback;
this._timeout = timeout ? timeout : 3000;
this._lastCheckTime = new Date().getTime();
this._intervalId = null;
}
/**
* Stop checking for update
*
* @return {Void}
*/
_createClass(ConversationUpdateHandler, [{
key: "destroy",
value: function destroy() {
clearInterval(this._intervalId);
}
/**
* Start checking for update
*
* @return {Void}
*/
}, {
key: "activate",
value: function activate() {
var checkUpdatedConversations = this.checkUpdatedConversations.bind(this);
var callback = this._callback;
var timeout = this._timeout;
this._intervalId = setInterval(function () {
checkUpdatedConversations().then(function (result) {
return callback(null, result);
}).catch(function (err) {
return callback(err, null);
});
}, timeout);
}
/**
* Method for retrieving updated conversations
*
* @return {Promise} array of updatedConversation if fulfilled.
*/
}, {
key: "checkUpdatedConversations",
value: function checkUpdatedConversations() {
var service = this._service;
var username = this._username;
var lastCheckTime = this._lastCheckTime;
var now = new Date().getTime();
return new Promise(function (resolve, reject) {
service.getUpdatedConversations(username, lastCheckTime, now).then(function (result) {
this._lastCheckTime = now + 1;resolve(result);
}.bind(this)).catch(function (err) {
return reject(err);
});
}.bind(this));
}
}]);
return ConversationUpdateHandler;
}();
exports.default = ConversationUpdateHandler;