Archive
is already/automatically read at startup, and written at shutdown (see Process:startup
and Process:shutdown
. I would caution against trusting the “write on shutdown” too much, since it obviously doesn’t do this if you crash or if there’s an error anywhere else in shutdown code. If I’m writing something that I really want to make sure is saved, I use something like this (with the Collapse quark):
ArchiveLazyWrite {
classvar <>func;
*initClass { func = Collapse({ Archive.write }, 10) }
}
+Archive {
*lazyWrite {
ArchiveLazyWrite.func.();
}
}
And then call Archive.lazyWrite
after I make archive changes (Collapse just avoids writing to disk again and again in case of repeated changes).
More specifically, for personal projects, I’ll often use a class based on Singleton
, with my current project path as the name (so, I have one global object per project folder, just in case I end up running code from multiple project folders at a given time). You can use a MultiLevelIdentityDictionary
for storage, and expose at
and put
methods that forward to this, and do the lazy read/write. A quick mockup (just off the top of my head, so this might not work):
LocalStorage : Singleton {
classvar <>timeBetweenWrites=10;
var storage, doWrite;
init {
|name|
storage = MultiLevelIdentityDictionary();
doWrite = Collapse({ this.write }, timeBetweenWrites);
this.read();
}
read { /* same read code from Archive, using `name` as the path */ }
write { /* same write code from Archive, using `name` as the path */ }
at { |...path| ^storage.atPath(path) }
put {
|...path|
storage.put(*path);
doWrite.();
}
}
Then, you can do LocalStorage(projectPath).put(\a, \b, \c, value)
. You can also make the path the second parameter to the Singleton, so you can give it a friendly short name:
LocalStorage(\myProj, "/path/to/my/project");
LocalStorage(\myProj).at(...);
FWIW the big benefit of project-local storage is that everything can be tracked in git