Author: scottbw
Date: Sat Dec 1 11:43:14 2012
New Revision: 1415981
URL: http://svn.apache.org/viewvc?rev=1415981&view=rev
Log:
Improved OpenAjax client to deal with loading delays, queuing pub/sub actions
until the hub is successfully loaded
Modified:
wookie/trunk/features/openajax/OpenAjaxClient.js
Modified: wookie/trunk/features/openajax/OpenAjaxClient.js
URL:
http://svn.apache.org/viewvc/wookie/trunk/features/openajax/OpenAjaxClient.js?rev=1415981&r1=1415980&r2=1415981&view=diff
==============================================================================
--- wookie/trunk/features/openajax/OpenAjaxClient.js (original)
+++ wookie/trunk/features/openajax/OpenAjaxClient.js Sat Dec 1 11:43:14 2012
@@ -19,13 +19,75 @@
// "hub.subscribe(topic, callback)"
//
-window.hub = new OpenAjax.hub.IframeHubClient({
- HubClient: {
- onSecurityAlert: function(){}
+window.hub = {};
+
+window.hub.isConnected = false;
+window.hub.subscriptions = [];
+window.hub.publishings = [];
+
+window.hub.connect = function(){
+ if(typeof window.hub.oahub === 'undefined'){
+ window.hub.oahub = new OpenAjax.hub.IframeHubClient({
+ HubClient: {
+ onSecurityAlert: function(){
+ console.log("OpenAjax Security Error!");
+ }
+ }
+ });
+
+ // Connect to the ManagedHub with callback function for asynchronous
communication
+ window.hub.oahub.connect(window.hub.onConnect);
+ }
+};
+
+window.hub.onConnect = function(hub, success, error){
+ if (success){
+ window.hub.isConnected = true;
+ window.hub.loadpending();
+ } else {
+ console.log(error);
+ setTimeout(window.hub.connect, 1000);
}
-});
+};
+
+window.hub.subscribe = function(topic, callback){
+ //
+ // If the hub isn't connected yet, queue the action
+ //
+ if (window.hub.isConnected){
+ window.hub.oahub.subscribe(topic, callback);
+ } else {
+ window.hub.subscriptions.push({"topic":topic, "callback":callback});
+ }
+};
+
+window.hub.publish = function(topic, message){
+ //
+ // If the hub isn't connected yet, queue the action
+ //
+ if (window.hub.isConnected){
+ window.hub.oahub.publish(topic,message);
+ } else {
+ window.hub.publishings.push({"topic":topic, "message":message});
+ }
+};
+
+//
+// Perform any pub/sub actions that were queued while the hub was being loaded
+//
+hub.loadpending = function(){
+ for (var i=0;i<window.hub.subscriptions.length;i++){
+ window.hub.oahub.subscribe(window.hub.subscriptions[i].topic,
window.hub.subscriptions[i].callback);
+ }
+ window.hub.subscriptions = [];
+
+ for (var j=0;j<window.hub.publishings.length;j++){
+ window.hub.oahub.publish(window.hub.publishings[j].topic,
window.hub.publishings[j].message);
+ }
+ window.hub.publishings = [];
+};
+
+window.hub.connect();
+
+
-// Connect to the ManagedHub
-window.hub.connect(
- function(hub, success, error){}
-);
\ No newline at end of file