title: How to check if Twilio.Device handlers already exist
body: <p>I'm using Twilio in an Angular app. I'm initializing the Twilio device whenever the user visits a certain page (let's call it the customer page) so that the user can call a specific customer. This initialize function is called in the customer controller:</p>
  
  <pre><code>function _initializeDevice(token, connectHandler, disconnectHandler) {
      console.log('CALLED INITIALIZE DEVICE');
      var device = Twilio.Device;
      device.setup(token, {debug: true});
      console.log(device);
  
      device.connect(connectHandler);
      device.disconnect(disconnectHandler);
  
      device.offline(function() {
          _getToken().then(function(result) {
              device.setup(result.data.token, {debug: true});
          });
      });
  
      device.error(_handleTwilioError);
  }
  </code></pre>
  
  <p>And this is the disconnect handler that gets passed in above:</p>
  
  <pre><code>function onDisconnect() {
      console.log('SAVING CALL');
      // code to save call
  }
  </code></pre>
  
  <p>The problem is, whenever the user navigates away from the customer page and back (without refreshing the page), the customer controller runs again, causing <code>_initializeDevice</code> function to also run again. Multiple connect/disconnect/etc. handlers end up getting registered to the same device, which causes things that should only be run once to run multiple times.</p>
  
  <p>Here's a sample of my console logs to illustrate the issue...</p>
  
  <p>So here's what happens when I <em>first</em> go to the customer page and call <code>_initializeDevice</code> the first time:</p>
  
  <pre><code>CALLED INITIALIZE DEVICE
  [Device] Setting up PStream
  [WSTransport] Opening socket
  [WSTransport] attempting to connect
  [WSTransport] Socket opened
  [PStream] Setting token and publishing listen
  [Device] Stream is ready
  [Device] Found existing Device; using new token but ignoring options
  [PStream] Setting token and publishing listen
  [Twilio.PeerConnection] signalingState is "have-local-offer"
  [Twilio.PeerConnection] signalingState is "stable"
  [Twilio.PeerConnection] iceConnectionState is "checking"
  [Twilio.PeerConnection] iceConnectionState is "connected"
  [Twilio.PeerConnection] iceConnectionState is "completed"
  [Connection] Disconnecting...
  [Twilio.PeerConnection] iceConnectionState is "closed"
  [Twilio.PeerConnection] signalingState is "closed"
  SAVING CALL
  </code></pre>
  
  <p>Then I navigate away from the customer page and back again, without refreshing, so the controller runs the initialize code again and duplicates the handlers:</p>
  
  <pre><code>CALLED INITIALIZE DEVICE
  [Device] Found existing Device; using new token but ignoring options
  [PStream] Setting token and publishing listen
  CALLED INITIALIZE DEVICE
  [Device] Found existing Device; using new token but ignoring options
  [PStream] Setting token and publishing listen
  [Device] Found existing Device; using new token but ignoring options
  [PStream] Setting token and publishing listen
  [Twilio.PeerConnection] signalingState is "have-local-offer"
  [Twilio.PeerConnection] signalingState is "stable"
  [Twilio.PeerConnection] iceConnectionState is "checking"
  [Twilio.PeerConnection] iceConnectionState is "connected"
  [Twilio.PeerConnection] iceConnectionState is "completed"
  [Connection] Received HANGUP from gateway
  [Connection] Disconnecting...
  [Twilio.PeerConnection] iceConnectionState is "closed"
  [Twilio.PeerConnection] signalingState is "closed"
  SAVING CALL
  SAVING CALL
  SAVING CALL
  </code></pre>
  
  <p>I tried using<code>Twilio.Device.destroy()</code>, but the handlers are still there.</p>
  
  <p>How can I check if handlers have already been attached to the Twilio device? Or, am I supposed to be attaching the event handlers somewhere else in my Angular app?</p>
  
  <p>Edit: for reference, here's how I'm disconnecting calls (attached to a button):</p>
  
  <pre><code>function hangUp() {
      Twilio.Device.disconnectAll();
  }
  </code></pre>