olark/browsertab — pull request #1 by meltzerj (2013-02-27 04:50:04+00:00)
Title: Added new event and fallback to cookie storage

Not sure if what I did is something you want in your master branch... feel free to take it or not :)

I added the "change:switchedBackAsPrimary" event which notifies you when a tab becomes a primary again. I basically needed a way to be notified only if a primary changed from one to another (excluding when BrowserTab is initialized). So I just hooked into the "makePrimary" method which seems to be the most reliable (although hacky) way to do this. I initially bound a callback to "listenForVisibilityChange" but found that there was a race condition when the visibilitychange event didn't exist: the "mousemove" event would be called twice before the callback would be fired, thus overwriting the first previousPrimary (which is how I detect whether a new primary is being a selected, or if the same primary is being selected again).