fix(plugins/settings): clear storage properly on errored plugins
Previous code did not handle API being undefined after plugin is stopped. Code has also been abstracted to be inside the storage API provider.
This commit is contained in:
parent
b2a378a3b2
commit
da8c535544
2 changed files with 19 additions and 12 deletions
|
|
@ -7,8 +7,9 @@ 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 { InitPluginApi, Plugin } from '@revenge-mod/plugins/types'
|
||||
import type { StorageOptions } from '@revenge-mod/storage'
|
||||
import type { AnyObject } from '@revenge-mod/utils/types'
|
||||
|
||||
const storageOptions = new WeakMap<AnyPlugin, StorageOptions>()
|
||||
|
||||
|
|
@ -30,13 +31,10 @@ registerPlugin(
|
|||
},
|
||||
init({ decorate }) {
|
||||
const makePluginStorage = (plugin: Plugin, opts?: StorageOptions) =>
|
||||
getStorage(
|
||||
`${PluginsStorageDirectory}/${plugin.manifest.id}.json`,
|
||||
{
|
||||
...opts,
|
||||
directory: 'documents',
|
||||
},
|
||||
)
|
||||
getStorage(getStoragePathForPlugin(plugin), {
|
||||
...opts,
|
||||
directory: 'documents',
|
||||
})
|
||||
|
||||
decorate(plugin => {
|
||||
const opts = storageOptions.get(plugin)
|
||||
|
|
@ -56,3 +54,14 @@ registerPlugin(
|
|||
InternalPluginFlags.Essential |
|
||||
InternalPluginFlags.API,
|
||||
)
|
||||
|
||||
function getStoragePathForPlugin(plugin: AnyPlugin) {
|
||||
return `${PluginsStorageDirectory}/${plugin.manifest.id}.json`
|
||||
}
|
||||
|
||||
export async function deleteStorageForPlugin(plugin: AnyPlugin) {
|
||||
const api = plugin.api as InitPluginApi<{ storage: AnyObject }> | undefined
|
||||
const storage = api?.storage ?? getStorage(getStoragePathForPlugin(plugin))
|
||||
console.log(api?.storage, storage, getStoragePathForPlugin(plugin))
|
||||
if (await storage.exists()) await storage.delete()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
import { AlertActionCreators } from '@revenge-mod/discord/actions'
|
||||
import { RootNavigationRef } from '@revenge-mod/discord/modules/main_tabs_v2'
|
||||
import { deleteStorageForPlugin } from '~plugins/preinit/api.storage'
|
||||
import PluginClearDataConfirmationAlert from '../components/PluginClearDataConfirmationAlert'
|
||||
import PluginHasDependenciesAlert from '../components/PluginHasDependenciesAlert'
|
||||
import PluginHasDependentsAlert from '../components/PluginHasDependentsAlert'
|
||||
import PluginStatesProvider from '../components/PluginStateProvider'
|
||||
import type { AnyPlugin } from '@revenge-mod/plugins/_'
|
||||
import type { InitPluginApi } from '@revenge-mod/plugins/types'
|
||||
import type { AnyObject } from '@revenge-mod/utils/types'
|
||||
|
||||
export function showPluginClearDataConfirmation(
|
||||
plugin: AnyPlugin,
|
||||
|
|
@ -15,8 +14,7 @@ export function showPluginClearDataConfirmation(
|
|||
const KEY = 'plugin-clear-data-confirmation'
|
||||
|
||||
async function action() {
|
||||
const { storage } = plugin.api as InitPluginApi<{ storage: AnyObject }>
|
||||
if (await storage.exists()) await storage.delete()
|
||||
await deleteStorageForPlugin(plugin)
|
||||
callback()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue