fix(lib/plugins): expose internal metadata via function instead
This commit is contained in:
parent
d44ec8fd6d
commit
2bc2a9fd08
5 changed files with 23 additions and 18 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { FilterFlag } from './constants'
|
||||
import { createFilterGenerator } from './utils'
|
||||
import type { Metro } from '@revenge-mod/modules/types'
|
||||
import type { LogicalAnd } from '@revenge-mod/utils/types'
|
||||
import type { Metro } from '#modules/src/types'
|
||||
import type {
|
||||
Filter,
|
||||
FilterBase,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { getPluginDependencies, pMetadata } from '.'
|
||||
import { getInternalPluginMeta, getPluginDependencies } from '.'
|
||||
import type { Plugin, PluginApiDecorator } from '../types'
|
||||
import type { AnyPlugin, InternalPluginMeta } from '.'
|
||||
|
||||
|
|
@ -51,7 +51,7 @@ export function decoratePluginApi(
|
|||
const decorators = store.get(dep)
|
||||
|
||||
if (decorators) {
|
||||
const { handleError } = pMetadata.get(dep)!
|
||||
const { handleError } = getInternalPluginMeta(dep)!
|
||||
|
||||
try {
|
||||
for (const decorator of decorators)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { getPluginDependencies, pMetadata } from '../_internal'
|
||||
import { getInternalPluginMeta, getPluginDependencies } from '../_internal'
|
||||
import type { AnyPlugin } from '../_internal'
|
||||
|
||||
/// PLUGIN DEPENDENCY GRAPHING
|
||||
|
|
@ -59,7 +59,7 @@ export function resolvePluginGraph(plugin: AnyPlugin) {
|
|||
pRootNodes.add(plugin)
|
||||
|
||||
for (const dep of getPluginDependencies(plugin)) {
|
||||
const depMeta = pMetadata.get(dep)!
|
||||
const depMeta = getInternalPluginMeta(dep)!
|
||||
depMeta.dependents.push(plugin)
|
||||
|
||||
// Not a root node if it has dependencies
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ export const pEmitter = new TypedEventEmitter<{
|
|||
}>()
|
||||
|
||||
export const pList = new Map<PluginManifest['id'], AnyPlugin>()
|
||||
export const pMetadata = new WeakMap<AnyPlugin, InternalPluginMeta>()
|
||||
const pMetadata = new WeakMap<AnyPlugin, InternalPluginMeta>()
|
||||
|
||||
const { flags: PersistedFlags }: PersistedPluginStates = callBridgeMethodSync(
|
||||
'revenge.plugins.states.read',
|
||||
|
|
@ -176,7 +176,7 @@ export function registerPlugin<O extends PluginApiExtensionsOptions>(
|
|||
* Gets dependencies for a plugin.
|
||||
*/
|
||||
export function getPluginDependencies(plugin: AnyPlugin): AnyPlugin[] {
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
if (meta.dependencies) return meta.dependencies
|
||||
|
||||
const { dependencies, id } = plugin.manifest
|
||||
|
|
@ -242,14 +242,15 @@ async function handlePluginError(e: unknown, plugin: AnyPlugin) {
|
|||
plugin.api?.logger?.error('Plugin encountered an error', e)
|
||||
pEmitter.emit('errored', plugin, e)
|
||||
|
||||
if (!isPluginEssential(pMetadata.get(plugin)!)) await plugin.disable()
|
||||
if (!isPluginEssential(getInternalPluginMeta(plugin)!))
|
||||
await plugin.disable()
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the plugin API for the preInit lifecycle.
|
||||
*/
|
||||
function tryPreparePluginPreInit(plugin: AnyPlugin) {
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
if (meta.apiLevel >= PluginApiLevel.PreInit) return
|
||||
|
||||
// Clear errors from previous runs
|
||||
|
|
@ -275,7 +276,7 @@ function tryPreparePluginPreInit(plugin: AnyPlugin) {
|
|||
* Prepares the plugin API for the init lifecycle.
|
||||
*/
|
||||
function tryPreparePluginInit(plugin: AnyPlugin) {
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
if (meta.apiLevel >= PluginApiLevel.Init) return
|
||||
|
||||
const api = plugin.api as InitPluginApi
|
||||
|
|
@ -292,7 +293,7 @@ function tryPreparePluginInit(plugin: AnyPlugin) {
|
|||
* Prepares the plugin API for the start lifecycle.
|
||||
*/
|
||||
function tryPreparePluginStart(plugin: AnyPlugin) {
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
if (meta.apiLevel >= PluginApiLevel.Start) return
|
||||
|
||||
const api = plugin.api as PluginApi
|
||||
|
|
@ -311,7 +312,7 @@ function tryPreparePluginStart(plugin: AnyPlugin) {
|
|||
export async function disablePlugin(plugin: AnyPlugin) {
|
||||
guardPluginEnabled(plugin)
|
||||
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
|
||||
if (isPluginEssential(meta))
|
||||
throw new Error(
|
||||
|
|
@ -395,7 +396,7 @@ export async function preInitPlugin(plugin: AnyPlugin) {
|
|||
tryPreparePluginPreInit(plugin)
|
||||
|
||||
const { lifecycles } = plugin
|
||||
const { promises, handleError } = pMetadata.get(plugin)!
|
||||
const { promises, handleError } = getInternalPluginMeta(plugin)!
|
||||
|
||||
try {
|
||||
if (!lifecycles.preInit) return
|
||||
|
|
@ -429,7 +430,7 @@ export async function initPlugin(plugin: AnyPlugin) {
|
|||
manifest: { id },
|
||||
} = plugin
|
||||
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
|
||||
if (plugin.status & (Status.Initing | Status.Inited))
|
||||
throw new Error(`Plugin init lifecycle for "${id}" is already running`)
|
||||
|
|
@ -480,7 +481,7 @@ export async function startPlugin(plugin: AnyPlugin) {
|
|||
tryPreparePluginStart(plugin)
|
||||
|
||||
const { lifecycles } = plugin
|
||||
const { promises, handleError } = pMetadata.get(plugin)!
|
||||
const { promises, handleError } = getInternalPluginMeta(plugin)!
|
||||
|
||||
try {
|
||||
if (!lifecycles.start) return
|
||||
|
|
@ -514,7 +515,7 @@ export async function stopPlugin(plugin: AnyPlugin) {
|
|||
manifest: { id },
|
||||
} = plugin
|
||||
|
||||
const meta = pMetadata.get(plugin)!
|
||||
const meta = getInternalPluginMeta(plugin)!
|
||||
|
||||
if (isPluginEssential(meta))
|
||||
throw new Error(`Plugin "${id}" is essential and cannot be stopped`)
|
||||
|
|
@ -592,6 +593,10 @@ async function cleanupPlugin(plugin: AnyPlugin, meta: InternalPluginMeta) {
|
|||
await Promise.all(proms)
|
||||
}
|
||||
|
||||
export function getInternalPluginMeta(plugin: AnyPlugin): InternalPluginMeta {
|
||||
return pMetadata.get(plugin)!
|
||||
}
|
||||
|
||||
declare module '@revenge-mod/modules/native' {
|
||||
interface Methods {
|
||||
'revenge.plugins.states.read': [[], PersistedPluginStates | null]
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@ import SearchInput from '@revenge-mod/components/SearchInput'
|
|||
import { ActionSheetActionCreators } from '@revenge-mod/discord/actions'
|
||||
import { Design } from '@revenge-mod/discord/design'
|
||||
import {
|
||||
getInternalPluginMeta,
|
||||
InternalPluginFlags,
|
||||
isPluginEnabled,
|
||||
isPluginEssential,
|
||||
isPluginInternal,
|
||||
pList,
|
||||
pMetadata,
|
||||
} from '@revenge-mod/plugins/_'
|
||||
import { PluginFlags } from '@revenge-mod/plugins/constants'
|
||||
import { debounce } from '@revenge-mod/utils/callback'
|
||||
|
|
@ -119,7 +119,7 @@ function Screen() {
|
|||
const allPlugins = useMemo(
|
||||
() =>
|
||||
[...pList.values()].map(
|
||||
plugin => [plugin, pMetadata.get(plugin)!] as const,
|
||||
plugin => [plugin, getInternalPluginMeta(plugin)!] as const,
|
||||
),
|
||||
[],
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue