-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a790fd4
commit bbac10c
Showing
3 changed files
with
17,193 additions
and
16,330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,118 @@ | ||
import type { Change } from "@legendapp/state"; | ||
import { applyChanges,internal,isArray } from "@legendapp/state"; | ||
import type { Change } from '@legendapp/state'; | ||
import { applyChanges, internal, isArray } from '@legendapp/state'; | ||
import type { | ||
ObservablePersistPlugin, | ||
ObservablePersistPluginOptions, | ||
ObservablePersistSQLiteStoragePluginOptions, | ||
PersistMetadata, | ||
} from "@legendapp/state/sync"; | ||
import type { SQLiteStorage as SQLiteStorageStatic } from "expo-sqlite/kv-store"; | ||
ObservablePersistPlugin, | ||
ObservablePersistPluginOptions, | ||
ObservablePersistSQLiteStoragePluginOptions, | ||
PersistMetadata, | ||
} from '@legendapp/state/sync'; | ||
import type { SQLiteStorage as SQLiteStorageStatic } from 'expo-sqlite/kv-store'; | ||
|
||
const MetadataSuffix = "__m"; | ||
const MetadataSuffix = '__m'; | ||
|
||
let SQLiteStorage: SQLiteStorageStatic; | ||
|
||
const { safeParse, safeStringify } = internal; | ||
|
||
export class ObservablePersistSQLiteStorage implements ObservablePersistPlugin { | ||
private data: Record<string, any> = {}; | ||
private configuration: ObservablePersistSQLiteStoragePluginOptions; | ||
private data: Record<string, any> = {}; | ||
private configuration: ObservablePersistSQLiteStoragePluginOptions; | ||
|
||
constructor(configuration: ObservablePersistSQLiteStoragePluginOptions) { | ||
this.configuration = configuration; | ||
} | ||
public async initialize(configOptions: ObservablePersistPluginOptions) { | ||
const storageConfig = this.configuration || configOptions.sQLiteStorage; | ||
constructor(configuration: ObservablePersistSQLiteStoragePluginOptions) { | ||
this.configuration = configuration; | ||
} | ||
public async initialize(configOptions: ObservablePersistPluginOptions) { | ||
const storageConfig = this.configuration || configOptions.sQLiteStorage; | ||
|
||
let tables: readonly string[] = []; | ||
if (storageConfig) { | ||
SQLiteStorage = storageConfig.SQLiteStorage; | ||
const { preload } = storageConfig; | ||
try { | ||
if (preload === true) { | ||
// If preloadAllKeys, load all keys and preload tables on startup | ||
tables = await SQLiteStorage.getAllKeys(); | ||
} else if (isArray(preload)) { | ||
// If preloadKeys, preload load the tables on startup | ||
const metadataTables = preload.map((table) => | ||
table.endsWith(MetadataSuffix) ? undefined : table + MetadataSuffix | ||
); | ||
tables = [ | ||
...preload, | ||
...(metadataTables.filter(Boolean) as string[]), | ||
]; | ||
} | ||
if (tables) { | ||
const values = await SQLiteStorage.multiGet(tables); | ||
let tables: readonly string[] = []; | ||
if (storageConfig) { | ||
SQLiteStorage = storageConfig.SQLiteStorage; | ||
const { preload } = storageConfig; | ||
try { | ||
if (preload === true) { | ||
// If preloadAllKeys, load all keys and preload tables on startup | ||
tables = await SQLiteStorage.getAllKeys(); | ||
} else if (isArray(preload)) { | ||
// If preloadKeys, preload load the tables on startup | ||
const metadataTables = preload.map((table) => | ||
table.endsWith(MetadataSuffix) ? undefined : table + MetadataSuffix, | ||
); | ||
tables = [...preload, ...(metadataTables.filter(Boolean) as string[])]; | ||
} | ||
if (tables) { | ||
const values = await SQLiteStorage.multiGet(tables); | ||
|
||
values.forEach(([table, value]) => { | ||
this.data[table] = value ? safeParse(value) : undefined; | ||
}); | ||
values.forEach(([table, value]) => { | ||
this.data[table] = value ? safeParse(value) : undefined; | ||
}); | ||
} | ||
} catch (e) { | ||
console.error('[legend-state] ObservablePersistSQLiteStorage failed to initialize', e); | ||
} | ||
} else { | ||
console.error('[legend-state] Missing sQLiteStorage configuration'); | ||
} | ||
} | ||
public loadTable(table: string): void | Promise<void> { | ||
if (this.data[table] === undefined) { | ||
return SQLiteStorage.multiGet([table, table + MetadataSuffix]) | ||
.then((values) => { | ||
try { | ||
values.forEach(([table, value]) => { | ||
this.data[table] = value ? safeParse(value) : undefined; | ||
}); | ||
} catch (err) { | ||
console.error('[legend-state] ObservablePersistLocalSQLiteStorage failed to parse', table, err); | ||
} | ||
}) | ||
.catch((err: Error) => { | ||
if (err?.message !== 'window is not defined') { | ||
console.error('[legend-state] SQLiteStorage.multiGet failed', table, err); | ||
} | ||
}); | ||
} | ||
} catch (e) { | ||
console.error( | ||
"[legend-state] ObservablePersistSQLiteStorage failed to initialize", | ||
e | ||
); | ||
} | ||
} else { | ||
console.error("[legend-state] Missing sQLiteStorage configuration"); | ||
} | ||
} | ||
public loadTable(table: string): void | Promise<void> { | ||
if (this.data[table] === undefined) { | ||
return SQLiteStorage.multiGet([table, table + MetadataSuffix]) | ||
.then((values) => { | ||
try { | ||
values.forEach(([table, value]) => { | ||
this.data[table] = value ? safeParse(value) : undefined; | ||
}); | ||
} catch (err) { | ||
console.error( | ||
"[legend-state] ObservablePersistLocalSQLiteStorage failed to parse", | ||
table, | ||
err | ||
); | ||
} | ||
}) | ||
.catch((err: Error) => { | ||
if (err?.message !== "window is not defined") { | ||
console.error( | ||
"[legend-state] SQLiteStorage.multiGet failed", | ||
table, | ||
err | ||
); | ||
} | ||
}); | ||
// Gets | ||
public getTable(table: string, init: object) { | ||
return this.data[table] ?? init ?? {}; | ||
} | ||
} | ||
// Gets | ||
public getTable(table: string, init: object) { | ||
return this.data[table] ?? init ?? {}; | ||
} | ||
public getMetadata(table: string): PersistMetadata { | ||
return this.getTable(table + MetadataSuffix, {}); | ||
} | ||
// Sets | ||
public set(table: string, changes: Change[]): Promise<void> { | ||
if (!this.data[table]) { | ||
this.data[table] = {}; | ||
public getMetadata(table: string): PersistMetadata { | ||
return this.getTable(table + MetadataSuffix, {}); | ||
} | ||
// Sets | ||
public set(table: string, changes: Change[]): Promise<void> { | ||
if (!this.data[table]) { | ||
this.data[table] = {}; | ||
} | ||
|
||
this.data[table] = applyChanges(this.data[table], changes); | ||
return this.save(table); | ||
} | ||
public setMetadata(table: string, metadata: PersistMetadata) { | ||
return this.setValue(table + MetadataSuffix, metadata); | ||
} | ||
public async deleteTable(table: string) { | ||
return SQLiteStorage.removeItem(table); | ||
} | ||
public deleteMetadata(table: string) { | ||
return this.deleteTable(table + MetadataSuffix); | ||
} | ||
// Private | ||
private async setValue(table: string, value: any) { | ||
this.data[table] = value; | ||
await this.save(table); | ||
} | ||
private async save(table: string) { | ||
const v = this.data[table]; | ||
this.data[table] = applyChanges(this.data[table], changes); | ||
return this.save(table); | ||
} | ||
public setMetadata(table: string, metadata: PersistMetadata) { | ||
return this.setValue(table + MetadataSuffix, metadata); | ||
} | ||
public async deleteTable(table: string) { | ||
return SQLiteStorage.removeItem(table); | ||
} | ||
public deleteMetadata(table: string) { | ||
return this.deleteTable(table + MetadataSuffix); | ||
} | ||
// Private | ||
private async setValue(table: string, value: any) { | ||
this.data[table] = value; | ||
await this.save(table); | ||
} | ||
private async save(table: string) { | ||
const v = this.data[table]; | ||
|
||
if (v !== undefined && v !== null) { | ||
return SQLiteStorage.setItem(table, safeStringify(v)); | ||
} else { | ||
return SQLiteStorage.removeItem(table); | ||
if (v !== undefined && v !== null) { | ||
return SQLiteStorage.setItem(table, safeStringify(v)); | ||
} else { | ||
return SQLiteStorage.removeItem(table); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function observablePersistSQLiteStorage( | ||
configuration: ObservablePersistSQLiteStoragePluginOptions | ||
) { | ||
return new ObservablePersistSQLiteStorage(configuration); | ||
export function observablePersistSQLiteStorage(configuration: ObservablePersistSQLiteStoragePluginOptions) { | ||
return new ObservablePersistSQLiteStorage(configuration); | ||
} |
Oops, something went wrong.