From 205794f4a7203e7748fd3d74bd57bc653aefbe04 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 16 Jan 2014 12:13:16 +0000 Subject: [PATCH] notify server about closed windows, so we can destroy no longer eT needed session/requests and so keep cache small --- etemplate/inc/class.etemplate.inc.php | 15 ++++++++++ etemplate/js/etemplate2.js | 42 +++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/etemplate/inc/class.etemplate.inc.php b/etemplate/inc/class.etemplate.inc.php index e52ba6a8b9..66f3eb2c0e 100644 --- a/etemplate/inc/class.etemplate.inc.php +++ b/etemplate/inc/class.etemplate.inc.php @@ -309,6 +309,21 @@ class etemplate_new extends etemplate_widget_template return $content; } + /** + * Notify server that eT session/request is no longer needed, because user closed window + * + * @param string $_exec_id + */ + static public function ajax_destroy_session($_exec_id) + { + //error_log(__METHOD__."('$_exec_id')"); + if (($request = etemplate_request::read($_exec_id))) + { + $request->remove_if_not_modified(); + unset($request); + } + } + /** * Process via POST submitted content */ diff --git a/etemplate/js/etemplate2.js b/etemplate/js/etemplate2.js index 60cb969b76..3cb25cf5f9 100644 --- a/etemplate/js/etemplate2.js +++ b/etemplate/js/etemplate2.js @@ -120,7 +120,7 @@ etemplate2.prototype.clear = function() { $j(window).off("."+this.uniqueId); } - + if (this.widgetContainer != null) { // Un-register handler @@ -190,11 +190,43 @@ etemplate2.prototype._createArrayManagers = function(_data) return result; }; +/** + * Bind our unload handler to notify server that eT session/request no longer needed + */ +etemplate2.prototype.bind_unload = function() +{ + if (!window.onbeforeunload) + { + window.onbeforeunload = this.destroy_session = jQuery.proxy(function(ev) + { + var request = egw.json(self.egw().getAppName()+".etemplate_new.ajax_destroy_session.etemplate", + [this.etemplate_exec_id], null, null, false); + request.sendRequest(); + }, this); + } +}; + +/** + * Unbind our unload handler + */ +etemplate2.prototype.unbind_unload = function() +{ + if (window.onbeforeunload === this.destroy_session) + { + window.onbeforeunload = null; + delete this.destroy_session; + } +}; + /** * Loads the template from the given URL and sets the data object */ etemplate2.prototype.load = function(_name, _url, _data, _callback) { + if (_data.etemplate_exec_id) + { + this.bind_unload(); + } egw().debug("info", "Loaded data", _data); var currentapp = _data.currentapp || window.egw_appName; @@ -294,7 +326,7 @@ etemplate2.prototype.load = function(_name, _url, _data, _callback) // Connect to the window resize event $j(window).on("resize."+this.uniqueId, this, function(e) {e.data.resize();}); - + // Insert the document fragment to the DOM Container this.DOMContainer.appendChild(frag); @@ -468,6 +500,9 @@ etemplate2.prototype.submit = function(button, async) // Create the request object if (this.menuaction) { + // unbind our session-destroy handler, as we are submitting + this.unbind_unload(); + var api = this.widgetContainer.egw(); var request = api.json(this.menuaction, [this.etemplate_exec_id,values], null, this, async); request.sendRequest(); @@ -500,6 +535,9 @@ etemplate2.prototype.postSubmit = function() if (canSubmit) { + // unbind our session-destroy handler, as we are submitting + this.unbind_unload(); + var form = jQuery("
");