File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff 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+
5568type 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}
You can’t perform that action at this time.
0 commit comments