import { fromUint8Array, hexToUint8Array, toUint8Array } from '../../common'; import { AspectTransientStorageApi } from '../../hostapi'; import { MutableAspectValue } from './aspect-state-interface'; const transientStorageApi = AspectTransientStorageApi.instance(); export class TransientStorage { private static _instance: TransientStorage | null; private constructor() {} public get(key: string, aspectId: string = ''): TransientStorageValue { return new TransientStorageValue(key, hexToUint8Array(aspectId)); } public static instance(): TransientStorage { if (!this._instance) { this._instance = new TransientStorage(); } return this._instance!; } } export class TransientStorageValue implements MutableAspectValue { private val: T; private loaded: boolean = false; constructor( private readonly key: string, private readonly aspectId: Uint8Array = new Uint8Array(0), ) { this.val = fromUint8Array(new Uint8Array(0)); } set(value: T): void { transientStorageApi.set(this.key, toUint8Array(value)); this.loaded = false; } reload(): void { this.val = fromUint8Array(transientStorageApi.get(this.key, this.aspectId)); this.loaded = true; } unwrap(): T { if (!this.loaded) { this.reload(); } return this.val; } }