I'm working on a Firefox extension where I'll want to keep multiple windows in sync with the same information. The toolbar queries a remote server for info periodically, based on when the window was opened. Because Firefox windows are all separately self-contained environments, each with their own toolbar running separate code I thought I'd use a singleton here. There's really no need for multiple requests, there can be one request for each of the windows, the problem though is that there is no global master scope over-lording it over the multiple windows context, there are only windows contexts. I thought I'd create a factory class that checks to see if one of the windows already has an instance of my notification class running and if so uses that same instance to get updates.
It seems like a legitimate use of a singleton, but I keep reading about how they're evil beasts. Is this an ok use?
The problem is that each Firefox window is an entirely separate process as far as JavaScript is concerned.
So yeah, it'll work, but only if you conditionally create the singleton. If it's just unconditionally created at global scope (in my case, as a member of my top-level extension object), it's going to be in all windows.
The other problem you're going to have is that windows take a while to start and run JavaScript, and you don't have anything like synchronization. It's entirely possible that window 1 checks to see if window 2 has created the singleton, sees it hasn't, window 2 check window 1, sees it hasn't, and then both create their own singleton.
I speak from experience: I wrote a Firefox extension that does something very much like what you want to do: only one window is supposed to check the remote server (and all other windows need to be informed when anyone window closes).
(In mine, each window on start-up queries all other windows to find the "master" window.)
The cleaner way around this is to create a service, just like the ones the browser exposes to javascript; but that requires coding in C, not javascript, and makes installing the extension cross-platform more of a pain.