title: Updating Preferences in Multiple Tabs in realtime worker.emit
body: <p>I am writing an extension that will call a content script and do some work. The content script's activity depends on preferences. I want the preferences to be updated in real time, not just on the tab load. I have setup a listener <code>require("sdk/simple-prefs").on("", onPrefChange);</code> which will fire correctly.</p>
  
  <pre><code>var tabs = require("sdk/tabs");
  var self = require("sdk/self");
  var prefSet = require("sdk/simple-prefs");
  var pageMod = require("sdk/page-mod");
  var data    = self.data;
  
  var workers = {};
  
  prefSet.on("", onPrefChange);
  
  function onPrefChange(prefName) {
      for each (var tab in tabs){     
          // the below code created a new worker and re-attached the script instead of reusing the worker
          // created in tabs.on('ready'...
          //changeWorker = tab.attach({
          //  contentScriptFile: self.data.url("my-script.js"),
          //});       
          //changeWorker.port.emit("setPrefs", prefSet.prefs);
          workers[tab.id].port.emit("setPrefs", prefSet.prefs);
      }
      console.log(workers);
  }
  
  tabs.on('ready', function(tab) {
      worker = workers[tab.id] = tab.attach({
          contentScriptFile: self.data.url("my-script.js"),
      });     
      worker.port.emit("setPrefs", prefSet.prefs);
  });
  
  tabs.on('close', function(tab) {
      delete workers[tab.id];
  });
  </code></pre>
  
  <p>I started with some code from <a href="https://stackoverflow.com/questions/20868824/firefox-port-emit-and-port-on-not-working-in-extension/20895341#20895341">this question</a>, but I am not using PageMod. I tried doing something similar with my code but it would not fire properly from the <code>ready</code> listener. In my <code>onPrefChange</code> function I was originally re-attaching the <code>contentScriptFile</code>, but this created a new attachment and worker. From <a href="https://stackoverflow.com/questions/16867853/firefox-sdk-content-script-cant-access-self-port/16871317#16871317">this question</a>, I found that I could create an array of workers and then call it later. The code above works as intended, but this seems less efficient.</p>
  
  <p>I would like to know if there is a way to setup the <code>onPrefChange</code> inside of the <code>ready</code> listener or to iterate through the list of tabs and find the worker attached; I cannot figure out how to iterate over the tabs and reuse the worker which is already created (which would remove the need to manage the <code>workers</code> array). Another way of asking this is given a tab, how can I list all workers attached? The update to the preferences should impact all open tabs, not just the last worker created or the current tab, which was the case with some of the other ways I tried.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>If in one tab I start at <code>http://www.example.com</code> then navigate to <code>http://www.example.org</code> and the hit the back button, the <code>setPreferences</code> will not fire and I will get an error: "The page is currently hidden and can no longer be used until it is visible again.". It looks like the way I attached the worker and then call iy while iterating through the tabs is not correct. I want it to go on the currently visible page, but it only wants to work on the last loaded page. Perhaps on each load of the page its creating a new worker and replacing the existing value in the array, and the worker is attached to the page not the tab?</p>
  
  <p><strong>Edit 2:</strong>
  I have been experimeting with getting this to work by using PageMod instead.
  The page being visible may be a different problem, <a href="https://stackoverflow.com/a/25345258/689554">as I discovered here</a>. I would still like to figure out how to accomplish the same thing with this original code for the sake of understanding how everything works together.</p>