Commits

Matthew Schinckel committed 85f5276

Allow for multiple delegates.

Comments (0)

Files changed (1)

   // Safari bug that requires this.
 
   Garmin.Plugin = function() {
+    if (arguments.callee._singletonInstance) {
+      return arguments.callee._singletonInstance;
+    }
+    
     var plugin;
   
     // Inject the plugin into the page if it isn't already there.
         plugin = $('#GarminNetscapePlugin');
       }
     }
-  
+    arguments.callee._singletonInstance = plugin[0];
+    
     return plugin[0];
   };
   
   ];
   
   // == Communicator ==
-  
   Garmin.Communicator = function Communicator(delegate, autostart) {
     
-    delegate = delegate || {};
+    // We actually want to be  singleton, and each call will just add
+    // the delegate to the list of delegates we call handlers on.
+    if (arguments.callee._singletonInstance) {
+      if (delegate) {
+        Garmin.Communicator.delegates.push(delegate);
+      }
+      return arguments.callee._singletonInstance;
+    } else {
+      arguments.callee._singletonInstance = this;
+    }
     
     // We need to have a flag so we can't try to have two things happening at once.
     var inUse = false;
+    var delegates = Garmin.Communicator.delegates = [delegate];
     var currentDevice = null;
     var plugin = new Garmin.Plugin();
     var $plugin = $(plugin);
     //************** Helpers **************//
     
     var send = function(eventType, data) {
-      if (delegate[eventType]) {
-        delegate[eventType](data);
-      }
+      _.each(delegates, function(delegate){
+        if (delegate[eventType]) {
+          delegate[eventType](data);
+        }        
+      });
       $plugin.trigger(eventType, data);
     };
     
     
     this.findDevices = function() {
       checkUnlocked();
+      inUse = "findDevices";
       plugin.StartFindDevices();
       // Check in 100 ms if we have finished.
       setTimeout(finishFindDevices, 100);
           devices.push(new Device(i));
         });
         send('deviceListUpdate', {devices: devices});
+        inUse = false;
       } else {
         // Keep checking every .1 sec until we finish.
         setTimeout(finishFindDevices, 100);