|
|
@@ -0,0 +1,60 @@
|
|
|
+
|
|
|
+var
|
|
|
+ /**
|
|
|
+ * @type {number} next period to wait before next retry in case of failure, in seconds
|
|
|
+ **/
|
|
|
+ NEXT_RETRY = 5
|
|
|
+;
|
|
|
+
|
|
|
+function poll(callback) {
|
|
|
+ var xhr = new XMLHttpRequest();
|
|
|
+ xhr.timeout = 18000; // 2 min timeout
|
|
|
+ xhr.timeout = 1800;
|
|
|
+ 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(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?t=' +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);
|
|
|
+}
|
|
|
+
|