mirror of
https://github.com/Mibew/design.git
synced 2025-01-22 18:10:33 +03:00
Add restartAfter method to JavaScript Server object
This commit is contained in:
parent
d24daf52eb
commit
aaf3bafb02
@ -5,10 +5,10 @@
|
||||
Copyright (c) 2005-2011 Mibew Messenger Community
|
||||
License: http://mibew.org/license.php
|
||||
*/
|
||||
(function(b,f,g,c){b.Server=function(a){this.updateTimer=null;this.options=c.extend({url:"",requestsFrequency:2,reconnectPause:1,onTimeout:function(){},onTransportError:function(){},onCallError:function(){},onUpdateError:function(){},onResponseError:function(){}},a);this.callbacks={};this.callPeriodically=[];this.ajaxRequest=null;this.buffer=[];this.functions={};this.mibewAPI=new f(new this.options.interactionType)};b.Server.prototype.callFunctions=function(a,e,b){try{if(!(a instanceof Array))throw Error("The first arguments must be an array");
|
||||
for(var d in a)a.hasOwnProperty(d)&&this.mibewAPI.checkFunction(a[d],!1);var c=this.generateToken();this.callbacks[c]=e;this.buffer.push({token:c,functions:a});b&&this.update()}catch(f){return this.options.onCallError(f),!1}return!0};b.Server.prototype.callFunctionsPeriodically=function(a,e){this.callPeriodically.push({functionsListBuilder:a,callbackFunction:e})};b.Server.prototype.generateToken=function(){var a;do a="wnd"+(new Date).getTime().toString()+Math.round(50*Math.random()).toString();while(a in
|
||||
this.callbacks);return a};b.Server.prototype.processRequest=function(a){var e=new MibewAPIExecutionContext,b=this.mibewAPI.getResultFunction(a.functions,this.callbacks.hasOwnProperty(a.token));if(null===b)for(var d in a.functions)a.functions.hasOwnProperty(d)&&(this.processFunction(a.functions[d],e),this.buffer.push(this.mibewAPI.buildResult(e.getResults(),a.token)));else this.callbacks.hasOwnProperty(a.token)&&(this.callbacks[a.token](b.arguments),delete this.callbacks[a.token])};b.Server.prototype.processFunction=
|
||||
function(a,e){if(this.functions.hasOwnProperty(a["function"])){var b=e.getArgumentsList(a),d={},f;for(f in this.functions[a["function"]])this.functions[a["function"]].hasOwnProperty(f)&&(d=c.extend(d,this.functions[a["function"]][f](b)));e.storeFunctionResults(a,d)}};b.Server.prototype.sendRequests=function(a){this.ajaxRequest=g.ajax({url:this.options.url,timeout:5E3,async:!0,cache:!1,type:"POST",dataType:"text",data:{data:this.mibewAPI.encodePackage(a)},success:c.bind(this.receiveResponse,this),
|
||||
error:c.bind(this.onError,this)})};b.Server.prototype.runUpdater=function(){this.updateTimer=setTimeout(c.bind(this.update,this),1E3*this.options.requestsFrequency)};b.Server.prototype.restartUpdater=function(){this.updateTimer&&clearTimeout(this.updateTimer);this.ajaxRequest&&this.ajaxRequest.abort();this.updateTimer=setTimeout(c.bind(this.update,this),1E3*this.options.reconnectPause)};b.Server.prototype.update=function(){this.updateTimer&&clearTimeout(this.updateTimer);for(var a=0;a<this.callPeriodically.length;a++)this.callFunctions(this.callPeriodically[a].functionsListBuilder(),
|
||||
this.callPeriodically[a].callbackFunction);if(0==this.buffer.length)this.runUpdater();else try{this.sendRequests(this.buffer),this.buffer=[]}catch(b){this.options.onUpdateError(b)}};b.Server.prototype.receiveResponse=function(a){""==a&&this.runUpdater();try{var b=this.mibewAPI.decodePackage(a),c;for(c in b.requests)this.processRequest(b.requests[c])}catch(d){this.options.onResponseError(d)}finally{this.runUpdater()}};b.Server.prototype.registerFunction=function(a,b){a in this.functions||(this.functions[a]=
|
||||
[]);this.functions[a].push(b)};b.Server.prototype.onError=function(a,b){if("abort"!=b)if(this.restartUpdater(),"timeout"==b)this.options.onTimeout();else if("error"==b)this.options.onTransportError()}})(Mibew,MibewAPI,$,_);
|
||||
(function(b,g,h,d){b.Server=function(a){this.updateTimer=null;this.options=d.extend({url:"",requestsFrequency:2,reconnectPause:1,onTimeout:function(){},onTransportError:function(){},onCallError:function(){},onUpdateError:function(){},onResponseError:function(){}},a);this.callbacks={};this.callPeriodically=[];this.ajaxRequest=null;this.buffer=[];this.functions={};this.mibewAPI=new g(new this.options.interactionType)};b.Server.prototype.callFunctions=function(a,e,b){try{if(!(a instanceof Array))throw Error("The first arguments must be an array");
|
||||
for(var c in a)a.hasOwnProperty(c)&&this.mibewAPI.checkFunction(a[c],!1);var f=this.generateToken();this.callbacks[f]=e;this.buffer.push({token:f,functions:a});b&&this.update()}catch(d){return this.options.onCallError(d),!1}return!0};b.Server.prototype.callFunctionsPeriodically=function(a,e){this.callPeriodically.push({functionsListBuilder:a,callbackFunction:e})};b.Server.prototype.generateToken=function(){var a;do a="wnd"+(new Date).getTime().toString()+Math.round(50*Math.random()).toString();while(a in
|
||||
this.callbacks);return a};b.Server.prototype.processRequest=function(a){var e=new MibewAPIExecutionContext,b=this.mibewAPI.getResultFunction(a.functions,this.callbacks.hasOwnProperty(a.token));if(null===b)for(var c in a.functions)a.functions.hasOwnProperty(c)&&(this.processFunction(a.functions[c],e),this.buffer.push(this.mibewAPI.buildResult(e.getResults(),a.token)));else this.callbacks.hasOwnProperty(a.token)&&(this.callbacks[a.token](b.arguments),delete this.callbacks[a.token])};b.Server.prototype.processFunction=
|
||||
function(a,e){if(this.functions.hasOwnProperty(a["function"])){var b=e.getArgumentsList(a),c={},f;for(f in this.functions[a["function"]])this.functions[a["function"]].hasOwnProperty(f)&&(c=d.extend(c,this.functions[a["function"]][f](b)));e.storeFunctionResults(a,c)}};b.Server.prototype.sendRequests=function(a){this.ajaxRequest=h.ajax({url:this.options.url,timeout:5E3,async:!0,cache:!1,type:"POST",dataType:"text",data:{data:this.mibewAPI.encodePackage(a)},success:d.bind(this.receiveResponse,this),
|
||||
error:d.bind(this.onError,this)})};b.Server.prototype.runUpdater=function(){this.update()};b.Server.prototype.updateAfter=function(a){this.updateTimer=setTimeout(d.bind(this.update,this),1E3*a)};b.Server.prototype.restartUpdater=function(){this.updateTimer&&clearTimeout(this.updateTimer);this.ajaxRequest&&this.ajaxRequest.abort();this.updateAfter(this.options.reconnectPause)};b.Server.prototype.update=function(){this.updateTimer&&clearTimeout(this.updateTimer);for(var a=0;a<this.callPeriodically.length;a++)this.callFunctions(this.callPeriodically[a].functionsListBuilder(),
|
||||
this.callPeriodically[a].callbackFunction);if(0==this.buffer.length)this.updateAfter(this.options.requestsFrequency);else try{this.sendRequests(this.buffer),this.buffer=[]}catch(b){this.options.onUpdateError(b)}};b.Server.prototype.receiveResponse=function(a){""==a&&this.updateAfter(this.options.requestsFrequency);try{var b=this.mibewAPI.decodePackage(a),d;for(d in b.requests)this.processRequest(b.requests[d])}catch(c){this.options.onResponseError(c)}finally{this.updateAfter(this.options.requestsFrequency)}};
|
||||
b.Server.prototype.registerFunction=function(a,b){a in this.functions||(this.functions[a]=[]);this.functions[a].push(b)};b.Server.prototype.onError=function(a,b){if("abort"!=b)if(this.restartUpdater(),"timeout"==b)this.options.onTimeout();else if("error"==b)this.options.onTransportError()}})(Mibew,MibewAPI,$,_);
|
||||
|
@ -33,13 +33,13 @@ return new b.SafeString(a)});b.registerHelper("formatTime",function(a){var c=new
|
||||
Copyright (c) 2005-2011 Mibew Messenger Community
|
||||
License: http://mibew.org/license.php
|
||||
*/
|
||||
(function(b,f,g,c){b.Server=function(a){this.updateTimer=null;this.options=c.extend({url:"",requestsFrequency:2,reconnectPause:1,onTimeout:function(){},onTransportError:function(){},onCallError:function(){},onUpdateError:function(){},onResponseError:function(){}},a);this.callbacks={};this.callPeriodically=[];this.ajaxRequest=null;this.buffer=[];this.functions={};this.mibewAPI=new f(new this.options.interactionType)};b.Server.prototype.callFunctions=function(a,e,b){try{if(!(a instanceof Array))throw Error("The first arguments must be an array");
|
||||
for(var d in a)a.hasOwnProperty(d)&&this.mibewAPI.checkFunction(a[d],!1);var c=this.generateToken();this.callbacks[c]=e;this.buffer.push({token:c,functions:a});b&&this.update()}catch(f){return this.options.onCallError(f),!1}return!0};b.Server.prototype.callFunctionsPeriodically=function(a,e){this.callPeriodically.push({functionsListBuilder:a,callbackFunction:e})};b.Server.prototype.generateToken=function(){var a;do a="wnd"+(new Date).getTime().toString()+Math.round(50*Math.random()).toString();while(a in
|
||||
this.callbacks);return a};b.Server.prototype.processRequest=function(a){var e=new MibewAPIExecutionContext,b=this.mibewAPI.getResultFunction(a.functions,this.callbacks.hasOwnProperty(a.token));if(null===b)for(var d in a.functions)a.functions.hasOwnProperty(d)&&(this.processFunction(a.functions[d],e),this.buffer.push(this.mibewAPI.buildResult(e.getResults(),a.token)));else this.callbacks.hasOwnProperty(a.token)&&(this.callbacks[a.token](b.arguments),delete this.callbacks[a.token])};b.Server.prototype.processFunction=
|
||||
function(a,e){if(this.functions.hasOwnProperty(a["function"])){var b=e.getArgumentsList(a),d={},f;for(f in this.functions[a["function"]])this.functions[a["function"]].hasOwnProperty(f)&&(d=c.extend(d,this.functions[a["function"]][f](b)));e.storeFunctionResults(a,d)}};b.Server.prototype.sendRequests=function(a){this.ajaxRequest=g.ajax({url:this.options.url,timeout:5E3,async:!0,cache:!1,type:"POST",dataType:"text",data:{data:this.mibewAPI.encodePackage(a)},success:c.bind(this.receiveResponse,this),
|
||||
error:c.bind(this.onError,this)})};b.Server.prototype.runUpdater=function(){this.updateTimer=setTimeout(c.bind(this.update,this),1E3*this.options.requestsFrequency)};b.Server.prototype.restartUpdater=function(){this.updateTimer&&clearTimeout(this.updateTimer);this.ajaxRequest&&this.ajaxRequest.abort();this.updateTimer=setTimeout(c.bind(this.update,this),1E3*this.options.reconnectPause)};b.Server.prototype.update=function(){this.updateTimer&&clearTimeout(this.updateTimer);for(var a=0;a<this.callPeriodically.length;a++)this.callFunctions(this.callPeriodically[a].functionsListBuilder(),
|
||||
this.callPeriodically[a].callbackFunction);if(0==this.buffer.length)this.runUpdater();else try{this.sendRequests(this.buffer),this.buffer=[]}catch(b){this.options.onUpdateError(b)}};b.Server.prototype.receiveResponse=function(a){""==a&&this.runUpdater();try{var b=this.mibewAPI.decodePackage(a),c;for(c in b.requests)this.processRequest(b.requests[c])}catch(d){this.options.onResponseError(d)}finally{this.runUpdater()}};b.Server.prototype.registerFunction=function(a,b){a in this.functions||(this.functions[a]=
|
||||
[]);this.functions[a].push(b)};b.Server.prototype.onError=function(a,b){if("abort"!=b)if(this.restartUpdater(),"timeout"==b)this.options.onTimeout();else if("error"==b)this.options.onTransportError()}})(Mibew,MibewAPI,$,_);
|
||||
(function(b,g,h,d){b.Server=function(a){this.updateTimer=null;this.options=d.extend({url:"",requestsFrequency:2,reconnectPause:1,onTimeout:function(){},onTransportError:function(){},onCallError:function(){},onUpdateError:function(){},onResponseError:function(){}},a);this.callbacks={};this.callPeriodically=[];this.ajaxRequest=null;this.buffer=[];this.functions={};this.mibewAPI=new g(new this.options.interactionType)};b.Server.prototype.callFunctions=function(a,e,b){try{if(!(a instanceof Array))throw Error("The first arguments must be an array");
|
||||
for(var c in a)a.hasOwnProperty(c)&&this.mibewAPI.checkFunction(a[c],!1);var f=this.generateToken();this.callbacks[f]=e;this.buffer.push({token:f,functions:a});b&&this.update()}catch(d){return this.options.onCallError(d),!1}return!0};b.Server.prototype.callFunctionsPeriodically=function(a,e){this.callPeriodically.push({functionsListBuilder:a,callbackFunction:e})};b.Server.prototype.generateToken=function(){var a;do a="wnd"+(new Date).getTime().toString()+Math.round(50*Math.random()).toString();while(a in
|
||||
this.callbacks);return a};b.Server.prototype.processRequest=function(a){var e=new MibewAPIExecutionContext,b=this.mibewAPI.getResultFunction(a.functions,this.callbacks.hasOwnProperty(a.token));if(null===b)for(var c in a.functions)a.functions.hasOwnProperty(c)&&(this.processFunction(a.functions[c],e),this.buffer.push(this.mibewAPI.buildResult(e.getResults(),a.token)));else this.callbacks.hasOwnProperty(a.token)&&(this.callbacks[a.token](b.arguments),delete this.callbacks[a.token])};b.Server.prototype.processFunction=
|
||||
function(a,e){if(this.functions.hasOwnProperty(a["function"])){var b=e.getArgumentsList(a),c={},f;for(f in this.functions[a["function"]])this.functions[a["function"]].hasOwnProperty(f)&&(c=d.extend(c,this.functions[a["function"]][f](b)));e.storeFunctionResults(a,c)}};b.Server.prototype.sendRequests=function(a){this.ajaxRequest=h.ajax({url:this.options.url,timeout:5E3,async:!0,cache:!1,type:"POST",dataType:"text",data:{data:this.mibewAPI.encodePackage(a)},success:d.bind(this.receiveResponse,this),
|
||||
error:d.bind(this.onError,this)})};b.Server.prototype.runUpdater=function(){this.update()};b.Server.prototype.updateAfter=function(a){this.updateTimer=setTimeout(d.bind(this.update,this),1E3*a)};b.Server.prototype.restartUpdater=function(){this.updateTimer&&clearTimeout(this.updateTimer);this.ajaxRequest&&this.ajaxRequest.abort();this.updateAfter(this.options.reconnectPause)};b.Server.prototype.update=function(){this.updateTimer&&clearTimeout(this.updateTimer);for(var a=0;a<this.callPeriodically.length;a++)this.callFunctions(this.callPeriodically[a].functionsListBuilder(),
|
||||
this.callPeriodically[a].callbackFunction);if(0==this.buffer.length)this.updateAfter(this.options.requestsFrequency);else try{this.sendRequests(this.buffer),this.buffer=[]}catch(b){this.options.onUpdateError(b)}};b.Server.prototype.receiveResponse=function(a){""==a&&this.updateAfter(this.options.requestsFrequency);try{var b=this.mibewAPI.decodePackage(a),d;for(d in b.requests)this.processRequest(b.requests[d])}catch(c){this.options.onResponseError(c)}finally{this.updateAfter(this.options.requestsFrequency)}};
|
||||
b.Server.prototype.registerFunction=function(a,b){a in this.functions||(this.functions[a]=[]);this.functions[a].push(b)};b.Server.prototype.onError=function(a,b){if("abort"!=b)if(this.restartUpdater(),"timeout"==b)this.options.onTimeout();else if("error"==b)this.options.onTransportError()}})(Mibew,MibewAPI,$,_);
|
||||
/*
|
||||
This file is part of Mibew Messenger project.
|
||||
http://mibew.org
|
||||
|
@ -259,12 +259,21 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up next automatic updater iteration
|
||||
* Start automatic updater
|
||||
*/
|
||||
Mibew.Server.prototype.runUpdater = function() {
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call Mibew.Server.update after specified timeout
|
||||
* @param {Number} time Timeout in seconds
|
||||
* @private
|
||||
*/
|
||||
Mibew.Server.prototype.updateAfter = function(time) {
|
||||
this.updateTimer = setTimeout(
|
||||
_.bind(this.update, this),
|
||||
this.options.requestsFrequency * 1000
|
||||
time * 1000
|
||||
);
|
||||
}
|
||||
|
||||
@ -281,14 +290,11 @@
|
||||
this.ajaxRequest.abort();
|
||||
}
|
||||
// Restart updater. Try to reconnect after a while
|
||||
this.updateTimer = setTimeout(
|
||||
_.bind(this.update, this),
|
||||
this.options.reconnectPause * 1000
|
||||
);
|
||||
this.updateAfter(this.options.reconnectPause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request for update thread and client code's requests
|
||||
* Send request to server
|
||||
* @private
|
||||
*/
|
||||
Mibew.Server.prototype.update = function() {
|
||||
@ -304,7 +310,7 @@
|
||||
// Check buffer length
|
||||
if (this.buffer.length == 0) {
|
||||
// Rerun updater later
|
||||
this.runUpdater();
|
||||
this.updateAfter(this.options.requestsFrequency);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@ -329,7 +335,7 @@
|
||||
Mibew.Server.prototype.receiveResponse = function(data, textStatus, jqXHR) {
|
||||
// Do not parse empty responses
|
||||
if (data == '') {
|
||||
this.runUpdater();
|
||||
this.updateAfter(this.options.requestsFrequency);
|
||||
}
|
||||
try {
|
||||
var packageObject = this.mibewAPI.decodePackage(data);
|
||||
@ -341,7 +347,7 @@
|
||||
} catch (e) {
|
||||
this.options.onResponseError(e);
|
||||
} finally {
|
||||
this.runUpdater();
|
||||
this.updateAfter(this.options.requestsFrequency);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user