Saving/serializing package state across multiple windows


My package tracks some statistics about the user’s activities (e.g., how many times keys are pressed) and displays a count on the status bar. My initial solution stored this data in an object that was serialized/deserialized when the window was closed/opened, but I just realized that serialized state seems to be specific to an atom window opened for a particular directory. That is, if I run atom dir1/ the serialized state will be different than if I start it with atom dir2/. I’m surprised this isn’t mentioned in the docs anywhere (that I can find).

Ideally I want my package state to be stored in one central place so that even if multiple windows are opened, they can all read and edit the data. It looks like I could do this with the config class and a set of observers. Is that the best practice even for very frequently changing data?


The Config class and config.cson are intended for user-editable information. If I’m understanding your use case correctly, there’s no occasion for the user to edit the information you’re intending on serializing. So I wouldn’t store it in the Config class … but write to a file in the ~/.atom directory and use that.


My use case boils down to needing to increase a global counter every time the user types a key into any atom window. Saving to a file sounds like a good way to persist across window closes, but is there a better way to get the global shared counter while things are running?

Are windows in atom separate threads or processes? I’d originally assumed threads which would make the sharing easier, but since I don’t see anything in the API for getting access to other windows I’m now thinking it must be the latter…



No, separate windows are separate processes. You possibly could save the information to Local Storage, but I don’t know if that is shared across processes.


OK, good to know!

I just checked and it appears that local storage is shared across windows. I’m a little worried about using it since it looks like there is just one global namespace with all of atom’s own storage stuff in there as well. Don’t want to accidentally clear() that!

I’m also considering using sqlite, (I first thought it couldn’t handle multiple processes on the same DB, but it appears I was wrong). Is there a standard place to store dynamic package data such as a db file? Should I use config.getUserConfigPath and try to dump it there?


That’s where i would put it, yes.


I think this also. Are you sure sqlite can? I googled it in the past. Maybe it has changed?


Apparently it is safe to do

(Haven’t tried it from atom yet though)