From dee2f2ada7608de3c7dfa2af2f192393cfcb0cd3 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 14 Feb 2013 09:32:30 +0000 Subject: [PATCH] Add new features to JavaScript Server class Add ability to remove periodically called functions and to unregister api functions(that can be called by the server) --- .../webim/js/compiled/default/server.js | 15 ++-- .../webim/js/compiled/default_app.js | 15 ++-- .../webim/js/source/default/server.js | 79 +++++++++++++++---- 3 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/messenger/webim/js/compiled/default/server.js b/src/messenger/webim/js/compiled/default/server.js index c4d2081c..25248be3 100644 --- a/src/messenger/webim/js/compiled/default/server.js +++ b/src/messenger/webim/js/compiled/default/server.js @@ -5,10 +5,11 @@ Copyright (c) 2005-2011 Mibew Messenger Community License: http://mibew.org/license.php */ -(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