From d842c169032c51494fcc2ff1dd6f5f3f8762a03e Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Tue, 2 Sep 2025 10:24:15 +0700 Subject: [PATCH] feat(plugins/api.storage): remove gotcha with `load: true` --- lib/storage/src/types.ts | 5 ----- src/plugins/preinit/api.storage/index.ts | 27 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/storage/src/types.ts b/lib/storage/src/types.ts index 82ced9d..458d263 100644 --- a/lib/storage/src/types.ts +++ b/lib/storage/src/types.ts @@ -17,11 +17,6 @@ declare module '@revenge-mod/plugins/types' { export interface InitPluginApi { /** * The plugin storage. - * - * Note that the instance is only created when the plugin accesses the API. - * This is to prevent unnecessary storage instances from being created. - * - * To preload storage, simply call `api.storage.get()`. */ storage: Storage> } diff --git a/src/plugins/preinit/api.storage/index.ts b/src/plugins/preinit/api.storage/index.ts index f5f86cd..5e3de62 100644 --- a/src/plugins/preinit/api.storage/index.ts +++ b/src/plugins/preinit/api.storage/index.ts @@ -7,6 +7,7 @@ import * as storage from '@revenge-mod/storage' import { getStorage } from '@revenge-mod/storage' import { defineLazyProperty } from '@revenge-mod/utils/object' import type { AnyPlugin } from '@revenge-mod/plugins/_' +import type { Plugin } from '@revenge-mod/plugins/types' import type { StorageOptions } from '@revenge-mod/storage' const storageOptions = new WeakMap() @@ -28,16 +29,24 @@ registerPlugin( }) }, init({ decorate }) { - decorate(plugin => { - defineLazyProperty(plugin.api, 'storage', () => - getStorage( - `${PluginsStorageDirectory}/${plugin.manifest.id}.json`, - { - ...storageOptions.get(plugin), - directory: 'documents', - }, - ), + const makePluginStorage = (plugin: Plugin, opts?: StorageOptions) => + getStorage( + `${PluginsStorageDirectory}/${plugin.manifest.id}.json`, + { + ...opts, + directory: 'documents', + }, ) + + decorate(plugin => { + const opts = storageOptions.get(plugin) + + if (opts?.load) + plugin.api.storage = makePluginStorage(plugin, opts) + else + defineLazyProperty(plugin.api, 'storage', () => + makePluginStorage(plugin, opts), + ) }) }, },