Skip to content

Commit

Permalink
Merge pull request #505 from microbit-foundation/462-proposition-addi…
Browse files Browse the repository at this point in the history
…ng-colors-to-gesture-object-2

Enforce correct storage version
  • Loading branch information
r59q authored Jul 3, 2024
2 parents 604dfc8 + 7993656 commit 6343850
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
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

0 comments on commit 6343850

Please sign in to comment.