import { StorageBoxInterface , StorageBoxDriveInterface , StorageBoxDriveRegister , } from "./interfaces" ; import { memoryDriveReguster } from "./memory-drive" ; export const DEFAULT_STORAGE_DRIVE_NAME = < const > "memory" ; class implements StorageBoxInterface export class StorageBox < T extends StorageBoxDriveInterface > implements StorageBoxInterface { static #registers : Record < string , StorageBoxDriveRegister < StorageBoxDriveInterface > > = { } ; readonly #internal : Record < string , T > = { } ; readonly #currentDrive : T ; param { string } name param { string } drive constructor ( public readonly name , public readonly drive = DEFAULT_STORAGE_DRIVE_NAME ) { if ( ! name ) { throw new Error ( "Storage box name is required." ) ; } if ( typeof drive === "string" ) { this . #currentDrive = this . getDrive ( drive ) ; } else { this . #currentDrive = drive ( this ) ; } } param { string } drive returns { T } throws getDrive < T extends StorageBoxDriveInterface > ( name ) { const register = StorageBox . #registers [ name ] as any ; if ( ! register || typeof register !== "function" ) { throw new Error ( `Drive "${ name }" not found.` ) ; } if ( ! this . #internal [ name ] ) { this . #internal [ name ] = register ( this ) as any ; } return this . #internal [ name ] as any ; } param { string } name param { StorageDriveRegister < T > } register register < T extends StorageBoxDriveInterface > ( name , drive ) { StorageBox . register ( name , drive ) ; if ( this . #internal [ name ] ) { delete this . #internal [ name ] ; } } param { string } key returns { T } get < T > ( key ) { return this . #currentDrive . get < T > ( key ) ; } param { string } key param { T } value returns { Promise < void > } set < T > ( key , value ) { return this . #currentDrive . set < T > ( key , value ) ; } param { string } key returns { Promise < void > } remove ( key ) { return this . #currentDrive . remove ( key ) ; } param { string } name param { StorageDriveRegister < T > } register returns { void } static register < T extends StorageBoxDriveInterface > ( name , drive ) { StorageBox . #registers [ name ] = drive ; } } StorageBox . register ( DEFAULT_STORAGE_DRIVE_NAME , memoryDriveReguster ) ;