/** * @license * Copyright 2022-2024 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { MatterError } from "../MatterError.js"; import { MaybePromise } from "../util/Promises.js"; import { SupportedStorageTypes } from "./StringifyTools.js"; export class StorageError extends MatterError {} /** * Matter.js uses this key/value API to manage persistent state. */ export interface Storage { readonly initialized: boolean; initialize(): MaybePromise; close(): MaybePromise; get(contexts: string[], key: string): MaybePromise; set(contexts: string[], values: Record): MaybePromise; set(contexts: string[], key: string, value: SupportedStorageTypes): MaybePromise; delete(contexts: string[], key: string): MaybePromise; keys(contexts: string[]): MaybePromise; values(contexts: string[]): MaybePromise>; contexts(contexts: string[]): MaybePromise; clearAll(contexts: string[]): MaybePromise; } // This extra class is needed because of https://github.com/microsoft/TypeScript/issues/57905 in order // to have the generics typing support on the "get" method and can be removed when the TS issue is fixed // or we remove the legacy API. export abstract class MaybeAsyncStorage implements Storage { abstract initialized: boolean; abstract initialize(): MaybePromise; abstract close(): MaybePromise; abstract get(contexts: string[], key: string): MaybePromise; abstract set(contexts: string[], values: Record): MaybePromise; abstract set(contexts: string[], key: string, value: SupportedStorageTypes): MaybePromise; abstract delete(contexts: string[], key: string): MaybePromise; abstract keys(contexts: string[]): MaybePromise; abstract values(contexts: string[]): MaybePromise>; abstract contexts(contexts: string[]): MaybePromise; abstract clearAll(contexts: string[]): MaybePromise; } // This can be removed once we remove the legacy API export abstract class SyncStorage implements Storage { abstract initialized: boolean; abstract initialize(): MaybePromise; abstract close(): MaybePromise; abstract get(contexts: string[], key: string): T | undefined; abstract set(contexts: string[], values: Record): void; abstract set(contexts: string[], key: string, value: SupportedStorageTypes): void; abstract delete(contexts: string[], key: string): void; abstract keys(contexts: string[]): string[]; abstract values(contexts: string[]): Record; abstract contexts(contexts: string[]): string[]; abstract clearAll(contexts: string[]): void; } export type StorageOperationResult = S extends SyncStorage ? T : Promise;