From 0e0238687a34d6af611e98f4df3dcb3d65178477 Mon Sep 17 00:00:00 2001 From: "Florine W. Dekker" Date: Sat, 26 Mar 2022 19:44:55 +0100 Subject: [PATCH] Use template's Storage class --- package.json | 2 +- src/main/index.html | 1 + src/main/js/Field.ts | 3 +- src/main/js/HighScores.ts | 5 +- src/main/js/Preferences.ts | 3 +- src/main/js/Statistics.ts | 3 +- src/main/js/Storage.ts | 178 ------------------------------------- 7 files changed, 11 insertions(+), 184 deletions(-) delete mode 100644 src/main/js/Storage.ts diff --git a/package.json b/package.json index 7e7b1cc..63887f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minesweeper", - "version": "0.82.15", + "version": "0.82.16", "description": "Just Minesweeper!", "author": "Florine W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/index.html b/src/main/index.html index 5d2b838..b815f5f 100644 --- a/src/main/index.html +++ b/src/main/index.html @@ -205,6 +205,7 @@ + diff --git a/src/main/js/Field.ts b/src/main/js/Field.ts index bfc1ff2..5d5fcf3 100644 --- a/src/main/js/Field.ts +++ b/src/main/js/Field.ts @@ -6,8 +6,9 @@ import {findDifficulty} from "./Difficulty"; import {HighScores} from "./HighScores"; import {Solver} from "./Solver"; import {Statistics} from "./Statistics"; -import {MemoryStorage} from "./Storage"; import {Timer} from "./Timer"; +// @ts-ignore +const {MemoryStorage} = window.fwdekker.storage; /** diff --git a/src/main/js/HighScores.ts b/src/main/js/HighScores.ts index 7a8ecee..eb30f13 100644 --- a/src/main/js/HighScores.ts +++ b/src/main/js/HighScores.ts @@ -1,6 +1,7 @@ import {formatTime} from "./Common"; import {difficulties, Difficulty} from "./Difficulty"; -import {LocalStorage, Storage} from "./Storage"; +// @ts-ignore +const {Storage, LocalStorage} = window.fwdekker.storage; /** @@ -43,7 +44,7 @@ export class HighScores { addScore(difficulty: Difficulty, score: Score): void { const scores = this.storage.getArray(difficulty.name, []); scores.push(score); - scores.sort((a, b) => { + scores.sort((a: Score, b: Score) => { return Math.floor(a.time / 1000) !== Math.floor(b.time / 1000) ? a.time - b.time : a.deaths - b.deaths; diff --git a/src/main/js/Preferences.ts b/src/main/js/Preferences.ts index 56a47e5..714a4f6 100644 --- a/src/main/js/Preferences.ts +++ b/src/main/js/Preferences.ts @@ -1,5 +1,6 @@ import {BasicIconFont, IconFont} from "./Display"; -import {LocalStorage, Storage} from "./Storage"; +// @ts-ignore +const {Storage, LocalStorage} = window.fwdekker.storage; /** diff --git a/src/main/js/Statistics.ts b/src/main/js/Statistics.ts index 27bd8bd..6c68060 100644 --- a/src/main/js/Statistics.ts +++ b/src/main/js/Statistics.ts @@ -1,5 +1,6 @@ import {formatTime} from "./Common"; -import {LocalStorage, Storage} from "./Storage"; +// @ts-ignore +const {Storage, LocalStorage} = window.fwdekker.storage; /** diff --git a/src/main/js/Storage.ts b/src/main/js/Storage.ts deleted file mode 100644 index 6b92381..0000000 --- a/src/main/js/Storage.ts +++ /dev/null @@ -1,178 +0,0 @@ -/** - * Stores key-value pairs. - */ -export interface Storage { - /** - * Removes the data from storage. - */ - clear(): void - - /** - * Retrieves an array from storage. - * - * @param name the name of the array to retrieve - * @param def the value to return if no array is stored with the given name - */ - getArray(name: string, def: any[]): any[] - - /** - * Stores an array. - * - * @param name the name of the array to store - * @param value the array to store under the given name - * @protected - */ - setArray(name: string, value: any[]): void - - /** - * Retrieves a boolean from storage. - * - * @param name the name of the boolean to retrieve - * @param def the value to return if no boolean is stored with the given name - * @protected - */ - getBoolean(name: string, def: boolean): boolean - - /** - * Stores a boolean. - * - * @param name the name of the boolean to store - * @param value the boolean to store under the given name - * @protected - */ - setBoolean(name: string, value: boolean): void - - /** - * Retrieves a number from storage. - * - * @param name the name of the number to retrieve - * @param def the value to return if no number is stored with the given name - * @protected - */ - getNumber(name: string, def: number): number - - /** - * Stores a number. - * - * @param name the name of the number to store - * @param value the number to store under the given name - * @protected - */ - setNumber(name: string, value: number): void -} - -/** - * Stores key-value pairs in a single entry in `localStorage`. - */ -export class LocalStorage implements Storage { - private readonly key: string; - private cache: { [key: string]: string } | null = null; - - - /** - * Constructs a new persistent storage item under the given key. - * - * @param key the unique identifier to store the data under - */ - constructor(key: string) { - this.key = key; - } - - - /** - * Reads the object stored in local storage. - * - * @return the object stored in local storage, or an empty object if there is nothing in the local storage - * @private - */ - private read(): { [key: string]: string } { - if (this.cache === null) - this.cache = JSON.parse(localStorage.getItem(this.key) ?? "{}"); - - return this.cache!; - } - - /** - * Writes the given object to local storage. - * - * @param item the object to write to local storage - * @private - */ - private write(item: { [key: string]: string }): void { - this.cache = item; - localStorage.setItem(this.key, JSON.stringify(item)); - } - - - clear(): void { - this.cache = null; - localStorage.removeItem(this.key); - } - - getArray(name: string, def: any[] = []): any[] { - const array = this.read()[name]; - return array === undefined ? def : JSON.parse(array); - } - - setArray(name: string, value: any[]): void { - const item = this.read(); - item[name] = JSON.stringify(value); - this.write(item); - } - - getBoolean(name: string, def: boolean = false): boolean { - return (this.read()[name] ?? `${def}`) === "true"; - } - - setBoolean(name: string, value: boolean): void { - const item = this.read(); - item[name] = "" + value; - this.write(item); - } - - getNumber(name: string, def: number = 0): number { - return +(this.read()[name] ?? def); - } - - setNumber(name: string, value: number): void { - const item = this.read(); - item[name] = "" + value; - this.write(item); - } -} - -/** - * Stores key-value pairs in an object. - */ -export class MemoryStorage implements Storage { - private storage: { [key: string]: any } = {}; - - - clear(): void { - this.storage = {}; - } - - setArray(name: string, value: any[] = []): void { - this.storage[name] = value; - } - - getArray(name: string, def: any[]): any[] { - return this.storage[name] ?? def; - } - - setBoolean(name: string, value: boolean): void { - this.storage[name] = value; - } - - getBoolean(name: string, def: boolean): boolean { - return this.storage[name] ?? def; - } - - setNumber(name: string, value: number): void { - this.storage[name] = value; - } - - getNumber(name: string, def: number): number { - return this.storage[name] ?? def; - } -}