Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce correct storage version #505

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/StaticConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,7 @@ class StaticConfiguration {
};

public static readonly knnNeighbourCount = 3;

public static readonly localStorageVersion = 2;
}
export default StaticConfiguration;
31 changes: 27 additions & 4 deletions src/script/ControlledStorage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import StaticConfiguration from "../StaticConfiguration";

/**
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
*
Expand All @@ -9,10 +11,19 @@ type StoredValue<T> = {
};

class ControlledStorage {

public static readonly localStorageVersion = 2;

public static get<T>(key: string): T {
const storedValue = this.getStoredItem(key);
const parsedValue = this.parseItem<T>(storedValue);
return parsedValue.value;
try {
const parsedValue = this.parseItem<T>(storedValue);
return parsedValue.value;
} catch (error) {
console.log(`An error occurred while parsing the stored value with key ${key}. The stored value will be deleted`, error)
localStorage.removeItem(key);
}
throw new Error(`Could not parse value '${storedValue}'`);
}

public static set<T>(key: string, value: T): void {
Expand All @@ -21,7 +32,14 @@ class ControlledStorage {
localStorage.setItem(key, stringified);
}

public static has(key: string): boolean {
public static hasValid(key: string): boolean {
try {
this.parseItem(this.getStoredItem(key));
} catch (error) {
console.log(`An error occurred while parsing the stored value with key ${key}. The stored value will be deleted`, error)
localStorage.removeItem(key);
return false;
}
return !!localStorage.getItem(key);
}

Expand All @@ -46,12 +64,17 @@ class ControlledStorage {
if (!('value' in parsed)) {
throw new Error(`Could not parse value '${storedValue}'. It did not contain value`);
}
if (parsed.version !== ControlledStorage.localStorageVersion) {
throw new Error(
`Could not parse value '${storedValue}'. Version mismatch. Expected version ${ControlledStorage.localStorageVersion}, found version ${parsed.version}`,
);
}
return parsed;
}

private static encapsulateItem<T>(value: T): StoredValue<T> {
return {
version: 1, // todo move this magic constant
version: ControlledStorage.localStorageVersion,
value,
};
}
Expand Down
11 changes: 7 additions & 4 deletions src/script/repository/LocalStorageGestureRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ class LocalStorageGestureRepository implements GestureRepository {

private getPersistedGestures(): Gesture[] {
const resultFromFetch: PersistantGestureData[] = this.getPersistedData();
return resultFromFetch.map(persistedData => this.buildGesture(persistedData));
return resultFromFetch.map((persistedData, index) => {
const gesture = this.buildGesture(persistedData);
return gesture;
});
}

private buildGesture(persistedData: PersistantGestureData) {
Expand All @@ -110,11 +113,11 @@ class LocalStorageGestureRepository implements GestureRepository {
}

private getPersistedData(): PersistantGestureData[] {
const result = localStorage.getItem(this.LOCAL_STORAGE_KEY);
if (!result) {
if (!ControlledStorage.hasValid(this.LOCAL_STORAGE_KEY)) {
return [];
}
return ControlledStorage.get(this.LOCAL_STORAGE_KEY);
const storedData = ControlledStorage.get<PersistantGestureData[]>(this.LOCAL_STORAGE_KEY)
return storedData;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/script/repository/PersistantWritable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PersistantWritable<T> implements Writable<T> {
initialValue: T,
private key: string,
) {
if (ControlledStorage.has(key)) {
if (ControlledStorage.hasValid(key)) {
const storedValue = ControlledStorage.get<T>(key);
this.store = writable(storedValue);
} else {
Expand Down