Release the resource. Resolves once the player confirmed the release (or immediately for purely local resources). Never rejects.
A "Turn feature off" item removes the feature's own menu item at once,
before the plugin stops. dispose takes the command out of the menu and
the binding table and resolves once the player confirmed it; the id can
be added again later.
const extra = plugin.commands.add({ id: "extra", path: ["Tools", "Extra"], run() {} });
plugin.commands.add({
id: "extra-off",
path: ["Tools", "Turn extra off"],
run: () => extra.dispose(), // never rejects
});
Replace the value now and write it after debounceMs.
Only save writes: mutating value in place is not saved.
Dragging the plugin's volume slider updates the stored value at once but
writes the file only once the user stops for 300 ms. Each save
replaces the value and restarts the debounce timer.
const store = plugin.storage.json("prefs", { defaults: { volume: 100 } });
volumeWindow.on("volumeChanged", (volume: number) => store.save({ ...store.value, volume }));
Write a pending save now.
Error (rejects) Node's file system error when the file cannot
be written; its code is Node's (ENOSPC, EACCES and so on), not a
player error code.
Before exporting its settings, the plugin makes sure the file on disk has
the latest value. flush writes a pending save at once (or waits for
the write in progress) and resolves when the file is written.
const store = plugin.storage.json("settings", { defaults: { step: 5 } });
await store.flush(); // the file now has the newest value
await copyFile(join(plugin.dataDirectory, "settings.json"), exportPath);
A JSON file
dataDirectory/<name>.json, loaded once and saved debounced.Remarks
One JSON file,
<dataDirectory>/<name>.json, made by Storage.json.valueis read once when the store is made;savereplaces it in memory at once and writes the filedebounceMslater (300 by default) through a temporary file and a rename, so a crash never leaves half a file. Writes to one file run one at a time, newest last. Disposing the store, its scope or stopping the plugin writes a pending save first.Example: Count how often the plugin was started
The counter grows by one at every start and is kept in
stats.json. The store reads the file when it is made (the defaults when there is none), andsavewrites the new count a moment later.Since
1.0.0