1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-20 14:37:43 +01:00

more error handling and better builder structure

This commit is contained in:
mrjvs 2021-10-24 23:28:03 +02:00
parent b6f27f6350
commit f6a4f59adb
2 changed files with 48 additions and 24 deletions

View file

@ -11,7 +11,7 @@ function buildStoreObject(data) {
while (obj["--version"] !== this.currentVersion) { while (obj["--version"] !== this.currentVersion) {
// get version // get version
let version = obj["--version"] || 0; let version = obj["--version"] || 0;
if (version === undefined || version.constructor !== Number) if (version.constructor !== Number || version < 0)
version = -42; // invalid on purpose so it will reset version = -42; // invalid on purpose so it will reset
else { else {
version = (version+1).toString() version = (version+1).toString()
@ -98,26 +98,33 @@ export function versionedStoreBuilder() {
this._data.storageString = str; this._data.storageString = str;
return this; return this;
}, },
addVersion(num, updateFunc, initFunc) { addVersion({ version, migrate, create }) {
// input checking
if (version < 0)
throw new Error("Cannot add version below 0 in store");
if (version > 0 && !migrate)
throw new Error(`Missing migration on version ${version} (needed for any version above 0)`);
// update max version list // update max version list
if (num > this._data.maxVersion) if (version > this._data.maxVersion)
this._data.maxVersion = num; this._data.maxVersion = version;
// add to version list // add to version list
this._data.versionList.push(num); this._data.versionList.push(version);
// register version // register version
this._data.versions[num.toString()] = { this._data.versions[version.toString()] = {
version: num, // version number version: version, // version number
update: (data) => { // update function, and increment version update: migrate ? (data) => { // update function, and increment version
updateFunc(data); migrate(data);
data["--version"] = num; data["--version"] = version;
return data; return data;
}, } : null,
init: () => { // return an initial object init: create ? () => { // return an initial object
const data = initFunc(); const data = create();
data["--version"] = num; data["--version"] = version;
return data; return data;
} } : null
} }
return this; return this;
}, },
@ -135,6 +142,10 @@ export function versionedStoreBuilder() {
if (versionListSorted[0] !== 0) if (versionListSorted[0] !== 0)
throw new Error("Version 0 doesn't exist in version list of store"); throw new Error("Version 0 doesn't exist in version list of store");
// max version must have init function
if (!this._data.versions[this._data.maxVersion.toString()].init)
throw new Error(`Missing create function on version ${this._data.maxVersion} (needed for latest version of store)`);
// check storage string // check storage string
if (!this._data.storageString) if (!this._data.storageString)
throw new Error("storage key not set in store"); throw new Error("storage key not set in store");

View file

@ -2,13 +2,26 @@ import { versionedStoreBuilder } from './base.js';
export const store = versionedStoreBuilder() export const store = versionedStoreBuilder()
.setKey('test-store') .setKey('test-store')
.addVersion(0, (d) => { .addVersion({
d.v0 = "v0"; version: 0,
}, () => ({ v0: "v0" })) })
.addVersion(1, (d) => { .addVersion({
d.v1 = "v1"; version: 1,
}, () => ({ v1: "v1" })) migrate(d) {
.addVersion(2, (d) => { d.v1 = "v1"
d.v2 = "v2"; return d;
}, () => ({ v2: "v2" })) },
})
.addVersion({
version: 2,
migrate(d) {
d.v2 = "v2"
return d;
},
create() {
return {
v2: "v2"
}
}
})
.build() .build()