
Is there a common pattern or means of performing updates when opening a new version of a sheet for the first time? For example, to populate fields normally modified by sheet worker events or to replace existing attributes?
My approach is using a hidden "attr_version" and moving through migration steps to update it to current:
// Version update for new or modified attributes on("sheet:opened", function() { getAttrs(["version", "sheettype"], function(v) { const version = parseInt(v.version) || 0; var attrs = {}; // get attribute updates for each version until the sheet has completed all steps // NOTE: to maintain backwards compatibility with sheets predating the version attribute, all steps will be performed for new sheets if (version < 1) { if (v.sheettype !== "mortal1") { attrs["game_line"] = getGameLine(v.sheettype); attrs["edition"] = getEdition(v.sheettype); } attrs["version"] = 1; } /* * if (version < 2) { * attrs["version"] = 2; * } */ if (Object.keys(attrs).length) { setAttrs(attrs); } }); });(Note that for the sake of backwards compatibility, new sheets will also run through all the migration steps.)
As a related question, is there a way to delete a sheet attribute? I can always set it to an empty string or some default value, but it'd be nice to be able to remove it from the "attributes" list altogether as a means of avoiding clutter. Use cases include having a "clear section" button, resetting selected toggles, or migrating sheet values from a hard-coded list of "attr_item1", "attr_item2", etc to a repeating section.
(If there is not currently a way to do this in sheet workers, can one be considered? It feels bad to leave a bunch of obsolete attributes.)