import type { KVStorageBackend } from './types' ; import { defer } from '../utils/defer' ; export interface ProxiedKVStorageBackend extends KVStorageBackend { replaceBackend ( backend ) : void ; } export function createProxyKVStorageBackend ( ) { let target ; let [ promise , resolve , reject ] = defer < void > ( ) ; return { get beforeAutoSync ( ) { return promise ; } , async getValue ( ... args ) { return target ! . getValue ( ... args ) ; } , async setValue ( ... args ) { return target ! . setValue ( ... args ) ; } , replaceBackend ( backend ) { target = backend ; backend . beforeAutoSync . then ( resolve , reject ) ; [ promise , resolve , reject ] = defer ( ) ; backend . beforeAutoSync . then ( resolve , reject ) ; } , } ; }