var /** * @type {number} next period to wait before next retry in case of failure, in seconds **/ NEXT_RETRY = 5 /** * @type {SlackChan|SlackIms|SlackGroup} **/ ,SELECTED_ROOM = null ; function poll(callback) { var xhr = new XMLHttpRequest(); xhr.timeout = 1000 * 60 * 1; // 3 min timeout xhr.onreadystatechange = function(e) { if (xhr.readyState === 4) { if (xhr.status === 0) { poll(callback); // retry on timeout NEXT_RETRY = 5; return; } var resp = null ,success = Math.floor(xhr.status / 100) === 2; if (success) { NEXT_RETRY = 5; resp = xhr.response; try { resp = JSON.parse(/** @type {string} */ (resp)); } catch (e) { resp = null; } } else { NEXT_RETRY += Math.floor(NEXT_RETRY /2); NEXT_RETRY = Math.min(60, NEXT_RETRY); } callback(success, resp); } }; xhr.open('GET', 'api?v=' +SLACK.lastServerVersion, true); xhr.send(null); } /** * @param {boolean} success * @param {*} response **/ function onPollResponse(success, response) { if (success) { if (response) { SLACK.update(response); } startPolling(); } else { setTimeout(startPolling, NEXT_RETRY * 1000); } } function startPolling() { poll(onPollResponse); } /** * @param {SlackChan|SlackIms|SlackGroup} room **/ function selectRoom(room) { if (SELECTED_ROOM) unselectRoom(); document.getElementById(room.id).classList.add(R.klass.selected); document.body.classList.remove(R.klass.noRoomSelected); SELECTED_ROOM = room; onRoomSelected(); } function unselectRoom() { document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected); }