|
|
@@ -294,59 +294,92 @@ MultiChatManager.prototype.pollPeriodical = function(knownVersion, withTyping) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
-MultiChatManager.prototype.startPolling = function(knownVersion, reqT, callback, withTyping) {
|
|
|
- var _this = this;
|
|
|
- var res = _this.pollPeriodical(knownVersion, withTyping),
|
|
|
- now = Date.now();
|
|
|
- if (res) {
|
|
|
- callback(res);
|
|
|
- } else if (now -reqT > POLL_TIMEOUT) {
|
|
|
- callback({
|
|
|
- "v": knownVersion
|
|
|
- });
|
|
|
- } else {
|
|
|
- setTimeout(function() { _this.startPolling(knownVersion, reqT, callback, withTyping); }, 2000);
|
|
|
- }
|
|
|
-};
|
|
|
+MultiChatManager.polling = [];
|
|
|
|
|
|
/**
|
|
|
* @param {number} knownVersion
|
|
|
- * @param {Function} callback
|
|
|
* @param {(function(number, function((Array<{expose:Function, modified: number}>|null))))=} checkConfigUpdate
|
|
|
* @param {boolean=} withTyping default true
|
|
|
**/
|
|
|
-MultiChatManager.prototype.poll = function(knownVersion, reqT, callback, checkConfigUpdate, withTyping) {
|
|
|
- this.contexts.forEach(function(ctx) {
|
|
|
+MultiChatManager.prototype.poll = function(knownVersion, reqT, checkConfigUpdate, withTyping) {
|
|
|
+ var _this = this;
|
|
|
+ _this.contexts.forEach(function(ctx) {
|
|
|
ctx.onRequest();
|
|
|
});
|
|
|
- var _this = this;
|
|
|
- if (checkConfigUpdate) {
|
|
|
- checkConfigUpdate(knownVersion, function(config) {
|
|
|
- if (config) {
|
|
|
- var exposedConfig = [],
|
|
|
- v = 0;
|
|
|
-
|
|
|
- config.forEach(function(conf) {
|
|
|
- exposedConfig.push(conf.expose());
|
|
|
- v = Math.max(v, conf.modified);
|
|
|
- });
|
|
|
-
|
|
|
- var res = _this.pollPeriodical(knownVersion, withTyping);
|
|
|
- if (res) {
|
|
|
- res["config"] = exposedConfig;
|
|
|
- callback(res);
|
|
|
- } else {
|
|
|
- callback({
|
|
|
- "config": exposedConfig,
|
|
|
- "v": v
|
|
|
+ var p = new Promise(function(succ, err) {
|
|
|
+ function launchPolling() {
|
|
|
+ var currentState = _this.pollPeriodical(knownVersion, withTyping);
|
|
|
+ if (currentState) {
|
|
|
+ succ(currentState);
|
|
|
+ } else {
|
|
|
+ var timeo = setTimeout(function() {
|
|
|
+ for (var i =0, nbCtx = MultiChatManager.polling.length; i < nbCtx; i++) {
|
|
|
+ if (MultiChatManager.polling[i].reqT === reqT &&
|
|
|
+ MultiChatManager.polling[i].ctx === _this &&
|
|
|
+ MultiChatManager.polling[i].knownVersion === knownVersion) {
|
|
|
+ MultiChatManager.polling.splice(i, 1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err();
|
|
|
+ }, 55000);
|
|
|
+ MultiChatManager.polling.push({ ctx: _this, knownVersion: knownVersion, reqT: reqT, checkConfigUpdate: checkConfigUpdate, withTyping: withTyping, promise: succ, timeo: timeo });
|
|
|
+ }
|
|
|
+ };
|
|
|
+ if (checkConfigUpdate) {
|
|
|
+ checkConfigUpdate(knownVersion, function(config) {
|
|
|
+ if (config) {
|
|
|
+ var exposedConfig = [],
|
|
|
+ v = 0;
|
|
|
+
|
|
|
+ config.forEach(function(conf) {
|
|
|
+ exposedConfig.push(conf.expose());
|
|
|
+ v = Math.max(v, conf.modified);
|
|
|
});
|
|
|
+
|
|
|
+ var res = _this.pollPeriodical(knownVersion, withTyping);
|
|
|
+ if (res) {
|
|
|
+ res["config"] = exposedConfig;
|
|
|
+ succ(res);
|
|
|
+ } else {
|
|
|
+ succ({
|
|
|
+ "config": exposedConfig,
|
|
|
+ "v": v
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ launchPolling();
|
|
|
}
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ launchPolling();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return p;
|
|
|
+};
|
|
|
+
|
|
|
+MultiChatManager.prototype.containsCtx = function(ctx) {
|
|
|
+ for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++)
|
|
|
+ if (ctx === this.contexts[i])
|
|
|
+ return true;
|
|
|
+ return false;
|
|
|
+};
|
|
|
+
|
|
|
+/** @param {ChatContext} ctx */
|
|
|
+MultiChatManager.onCtxUpdated = function(ctx) {
|
|
|
+ var i =0;
|
|
|
+ while (MultiChatManager.polling[i]) {
|
|
|
+ var pollingObj = MultiChatManager.polling[i];
|
|
|
+ if (pollingObj.ctx.containsCtx(ctx)) {
|
|
|
+ var res = pollingObj.ctx.pollPeriodical(pollingObj.knownVersion, pollingObj.withTyping);
|
|
|
+ if (res) {
|
|
|
+ clearTimeout(pollingObj.timeo);
|
|
|
+ pollingObj.promise(res);
|
|
|
+ MultiChatManager.polling.splice(i, 1);
|
|
|
} else {
|
|
|
- _this.startPolling(knownVersion, reqT, callback, withTyping);
|
|
|
+ i++;
|
|
|
}
|
|
|
- });
|
|
|
- } else {
|
|
|
- _this.startPolling(knownVersion, reqT, callback, withTyping);
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
|