Skip to content

Commit 235dfab

Browse files
committed
added QPixel.Storage helper module & migration for fixing user preferences
1 parent 9532900 commit 235dfab

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
window.QPixel = window.QPixel || {};
2+
3+
QPixel.Storage = {
4+
get latestMigration() {
5+
return localStorage.getItem("qpixel.latest_storage_migration");
6+
},
7+
migrations: [],
8+
addMigration(migration) {
9+
this.migrations.push(migration);
10+
return this;
11+
},
12+
async runMigrations() {
13+
const { latestMigration, migrations } = this;
14+
15+
const latestIndex = migrations.findIndex((m) => m.name === latestMigration);
16+
const pending = migrations.slice(latestIndex + 1);
17+
18+
for (const migration of pending) {
19+
try {
20+
await migration.up();
21+
} catch (e) {
22+
console.warn(`[qpixel/storage] migration ${migration.name} error`, e);
23+
break;
24+
}
25+
}
26+
},
27+
};
28+
29+
document.addEventListener("DOMContentLoaded", async () => {
30+
await QPixel.Storage.addMigration({
31+
name: "fix-user-preferences",
32+
async up() {
33+
localStorage.removeItem("qpixel.user_undefined_preferences");
34+
},
35+
}).runMigrations();
36+
});

global.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ interface QPixelMD {
5252
stripMarkdown(content: string, options?: StripMarkdownOptions): string;
5353
}
5454

55+
interface QPixelStorageMigration {
56+
name: string
57+
up: () => Promise<void>
58+
down?: () => Promise<void>
59+
}
60+
61+
interface QPixelStorage {
62+
readonly latestMigration: string | null
63+
migrations: QPixelStorageMigration[]
64+
addMigration: (migration: QPixelStorageMigration) => QPixelStorage
65+
runMigrations:() => Promise<void>
66+
}
67+
5568
type QPixelKeyboardState =
5669
| "home"
5770
| "goto"
@@ -386,6 +399,8 @@ interface QPixel {
386399
MD?: QPixelMD;
387400
// qpixel popups
388401
Popup?: typeof QPixelPopup;
402+
// qpixel storage management
403+
Storage?: QPixelStorage;
389404
// Stripe integration, TODO: types
390405
stripe?: any;
391406
}

0 commit comments

Comments
 (0)