-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: plug in db implementation for device cache
- Loading branch information
1 parent
c8f83cf
commit 8cf5ca1
Showing
20 changed files
with
408 additions
and
56 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { useMemo, useRef } from 'react' | ||
import { reactCache } from './shared' | ||
import { DBDriverFactory } from './driver/abstract' | ||
import { lazyDBDriver } from './driver/lazy' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
type CacheContext = { | ||
cache: { | ||
[dbName: string]: { | ||
[storeName: string]: reactCache, | ||
}, | ||
}, | ||
dbDriverFactory: DBDriverFactory, | ||
} | ||
|
||
const lazyIndexedDB = lazyDBDriver(() => import('./driver/IndexedDB')) | ||
|
||
export const globalContext: CacheContext = { | ||
cache: {}, | ||
dbDriverFactory: lazyIndexedDB, | ||
} | ||
|
||
export const CacheContext = React.createContext<CacheContext>(globalContext) | ||
|
||
/** | ||
* Configure the cache used outside of CacheProvider. | ||
* Be aware that changing the global config does not rerender any components. | ||
*/ | ||
export function configureGlobalCache( | ||
{ | ||
dbDriverFactory, | ||
}: { | ||
dbDriverFactory: DBDriverFactory, | ||
}, | ||
): void { | ||
globalContext.dbDriverFactory = dbDriverFactory | ||
} | ||
|
||
export function CacheProvider( | ||
{ | ||
dbDriverFactory = lazyIndexedDB, | ||
children, | ||
}: React.PropsWithChildren<{ | ||
dbDriverFactory?: DBDriverFactory | ||
}>, | ||
): React.ReactElement { | ||
const cache = useRef<CacheContext['cache']>({}).current | ||
const context: CacheContext = useMemo(() => ({cache, dbDriverFactory}), [cache, dbDriverFactory]) | ||
|
||
return <CacheContext.Provider value={context}>{children}</CacheContext.Provider> | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { clear, createStore, del, entries, getMany, setMany } from 'idb-keyval'; | ||
import { DBDriver, DBKey, DBValue } from './abstract'; | ||
|
||
export default (dbName: string, storeName: string): IndexedDBDriver => new IndexedDBDriver(dbName, storeName) | ||
|
||
class IndexedDBDriver implements DBDriver { | ||
constructor(dbName: string, storeName: string) { | ||
this.store = createStore(dbName, storeName) | ||
} | ||
store: ReturnType<typeof createStore> | ||
|
||
clear(): Promise<void> { | ||
return clear(this.store) | ||
} | ||
|
||
del(key: DBKey): Promise<void> { | ||
return del(key, this.store) | ||
} | ||
|
||
entries<T extends unknown>(): Promise<[DBKey, NonNullable<DBValue<T>>][]> { | ||
return entries(this.store) | ||
} | ||
|
||
getMany<T extends unknown>(keys: DBKey[]): Promise<DBValue<T>[]> { | ||
return getMany(keys, this.store) | ||
} | ||
|
||
setMany<T extends unknown>(entries: [DBKey, DBValue<T>][]): Promise<void> { | ||
return setMany(entries, this.store) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { DBDriver, DBKey, DBValue } from './abstract'; | ||
|
||
export default (): typeof NullDBDriver => NullDBDriver | ||
|
||
const NullDBDriver: DBDriver = { | ||
async clear(): Promise<void> { | ||
return | ||
}, | ||
async del(): Promise<void> { | ||
return | ||
}, | ||
async entries<T extends unknown>(): Promise<[DBKey, NonNullable<DBValue<T>>][]> { | ||
return [] | ||
}, | ||
async getMany<T extends unknown>(keys: DBKey[]): Promise<DBValue<T>[]> { | ||
return keys.map(() => undefined) | ||
}, | ||
async setMany(): Promise<void> { | ||
return | ||
}, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { cachedObj } from '../shared' | ||
|
||
export type DBKey = IDBValidKey | ||
export type DBValue<T> = cachedObj<T> | undefined | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface DBDriverFactory { | ||
(dbName: string, storeName: string): DBDriver | ||
} | ||
|
||
/** | ||
* Driver for local/device/session cache. | ||
* | ||
* @internal | ||
*/ | ||
export interface DBDriver { | ||
clear(): Promise<void> | ||
del(key: DBKey): Promise<void> | ||
entries<T = any>(): Promise<[DBKey, NonNullable<DBValue<T>>][]> | ||
getMany<T = any>(keys: DBKey[]): Promise<DBValue<T>[]> | ||
setMany<T = any>(entries: [DBKey, DBValue<T>][]): Promise<void> | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { DBDriver, DBDriverFactory, DBKey, DBValue } from './abstract' | ||
|
||
export function lazyDBDriver(loader: () => Promise<DBDriverFactory|{default: DBDriverFactory}>): DBDriverFactory { | ||
return (dbName, storeName) => new LazyDB(loader, dbName, storeName) | ||
} | ||
|
||
class LazyDB implements DBDriver { | ||
constructor( | ||
loader: () => Promise<DBDriverFactory|{default: DBDriverFactory}>, | ||
dbName: string, | ||
storeName: string, | ||
) { | ||
this.loader = loader | ||
this.dbName = dbName | ||
this.storeName = storeName | ||
} | ||
private loader: () => Promise<DBDriverFactory|{default: DBDriverFactory}> | ||
private driver?: DBDriver | ||
private dbName: string | ||
private storeName: string | ||
|
||
private async loadDriver() { | ||
return this.driver ?? this.loader().then(m => { | ||
const factory = typeof m === 'function' ? m : m.default | ||
return this.driver = factory(this.dbName, this.storeName) | ||
}) | ||
} | ||
|
||
async clear(): Promise<void> { | ||
return this.loadDriver().then(d => d.clear()) | ||
} | ||
async del(key: DBKey): Promise<void> { | ||
return this.loadDriver().then(d => d.del(key)) | ||
} | ||
async entries<T extends unknown>(): Promise<[DBKey, NonNullable<DBValue<T>>][]> { | ||
return this.loadDriver().then(d => d.entries()) | ||
} | ||
async getMany<T extends unknown>(keys: DBKey[]): Promise<DBValue<T>[]> { | ||
return this.loadDriver().then(d => d.getMany(keys)) | ||
} | ||
async setMany<T extends unknown>(entries: [DBKey, DBValue<T>][]): Promise<void> { | ||
return this.loadDriver().then(d => d.setMany(entries)) | ||
} | ||
} |
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
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
Oops, something went wrong.