I have tried everything I can think of but I cannot get simple-storage to actually store anything. My code is as simple as this.
var ss = require(“sdk/simple-storage”);
…
ss.storage.derp = “DERP”;
ss.storage.arr = [{id: “arr”}];
ss.storage.exists = true;
ss.storage.keys = {};
None of these values ever get stored. If I view the “ss” object in the add-on debugger I don’t see any values being set, and after a restart nothing is present. So nothing is being stored.
What am I doing wrong?
To further debug, I calculated MD5 sums for every file in the profile directory and watched to see which change between restarts. None of the files that change between restarts have anything to do with simple storage. So I’m left wondering if this is even intended to work.
simple-storage essentially works although it certainly has bugs. It is also a synchronous API so Mozilla will always be looking for a replacement. If you are using “jpm run” then simple storage doesn’t work, full stop. simple-storage stores data specific to the scope of one addon in one profile. jpm run by default creates a new profile for each run so no data will be persisted. I think the debugger suffers from essentially the same problem, where it doesn’t see the correct scope.
You can use jpm to run with a specific profile directory (not just a profile name) and that should work.
Note that simple storage is a “synchronous” API. Although IO is not actually performed synchronously with each access, it may still cause some jank if it is run on the main thread. There are other possible solutions although I don’t think I’d install a third party module unless there were real big problems.
Simple storage is stored in jetpack/{{addon-uuid}}/simple-storage/store.json. The file is just used as a backing store and may not be written at all. Any data will be flushed every 5 minutes, when Firefox is shut down cleanly, or when an addon is uninstalled. There is also a quota limit, 5MB by default.
I can see those files for the “default” profile. But I don’t see anything for my development profiles. Including the “bob” profile that I reference above. My development profiles do not contain “jetpack” sub-directories.
So I installed Ghostery on my “bob” development profile and it created the jetpack directory, and subsequent Ghostery simple-storage sub-directories.(jetpack/firefox@ghostery.com/simple-storage)
However, after running my add-on a few times it still did not create any directories for my add-on’s simple-storage.
I see these log messages that look like they might relate to storage.
If the simple storage pages suggests using the profile argument it should. A hint that the profile is copied should be everywhere the profile argument is used. Luckily MDN is a wimi, so you can help fix that
As I tried to say before: MDN is a wiki. And you can login with persona or github. So please feel free and add it where you think it will help others the most.
The exact location of the file made is in a your profile folder, in a folder named jetpack then your addon id, then a folder called simple-storage, then in a file in that folder called store.json. Example path:
ProfD/jetpack/addon-id/simple-storage/store.json
It then writes data to that file. Every time your profile folder is recreated (due to the nature of temp profile, due to jpm / cfx), your data is erased.
I like to just use OS.File (as it is async) to create your own file (at same path as simple-storage module) to save data. OS.File is better way then nsIFile which is what simple-storage does. Save it outside that ProfD folder, so but make sure to remove it on uninstall of your addon otherwise you pollute your users computers.