meses-messaging
Version:
Meses messaging SDK in JavaScript
55 lines (49 loc) • 1.53 kB
JavaScript
class ConversationUpdateHandler {
constructor(service, username, identifier, callback, timeout) {
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}
*/
destroy() { clearInterval(this._intervalId) }
/**
* Start checking for update
*
* @return {Void}
*/
activate() {
const checkUpdatedConversations = this.checkUpdatedConversations.bind(this)
const callback = this._callback
const timeout = this._timeout
this._intervalId = setInterval(function() {
checkUpdatedConversations()
.then(result => callback(null, result))
.catch(err => callback(err, null))
}, timeout)
}
/**
* Method for retrieving updated conversations
*
* @return {Promise} array of updatedConversation if fulfilled.
*/
checkUpdatedConversations() {
const service = this._service
const username = this._username
const lastCheckTime = this._lastCheckTime
const 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(err => reject(err))
}.bind(this))
}
}
export default ConversationUpdateHandler