mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-22 15:54:09 +00:00
refactor: activation events + contributes
This commit is contained in:
committed by
Alexander Brown
parent
bb2c99749a
commit
f1856b7a17
@@ -4,7 +4,7 @@ import type {
|
||||
ComfyExtensionEntrance,
|
||||
ComfyExtensionLoadContext
|
||||
} from './types'
|
||||
import { formatExtensions, shouldLoadExtension } from './utils'
|
||||
import { formatExtensions, normalizationActivationEvents } from './utils'
|
||||
|
||||
const extLoadContext: ComfyExtensionLoadContext = {
|
||||
get isCloud() {
|
||||
@@ -22,11 +22,61 @@ export async function dispatchComfyExtensions(options: {
|
||||
const { configs, entrance } = options
|
||||
const extensions = formatExtensions(entrance, configs)
|
||||
for (const extension of Object.values(extensions)) {
|
||||
if (shouldLoadExtension(extLoadContext, extension.config)) {
|
||||
const module = await extension.entry()
|
||||
console.log('✅ extension', extension.name, 'loaded', extension, module)
|
||||
// if (shouldLoadExtension(extLoadContext, extension.config)) {
|
||||
// const module = await extension.entry()
|
||||
// console.log('✅ extension', extension.name, 'loaded', extension, module)
|
||||
// } else {
|
||||
// console.log('❌ extension', extension.name, 'disabled', extension.config)
|
||||
// }
|
||||
const activationEvents = normalizationActivationEvents(
|
||||
extLoadContext,
|
||||
extension.config
|
||||
)
|
||||
if (!activationEvents.length) {
|
||||
console.log(
|
||||
'❌ extension',
|
||||
extension.name,
|
||||
'has no activation events',
|
||||
extension.config
|
||||
)
|
||||
} else {
|
||||
console.log('❌ extension', extension.name, 'disabled', extension.config)
|
||||
console.log(
|
||||
'🧶 extension',
|
||||
extension.name,
|
||||
'has activation events:',
|
||||
activationEvents
|
||||
)
|
||||
}
|
||||
activationEvents.forEach((event) =>
|
||||
onceExtImportEvent(event, async ({ event }) => {
|
||||
console.log(
|
||||
'✅ extension',
|
||||
extension.name,
|
||||
'loaded by',
|
||||
event,
|
||||
extension
|
||||
)
|
||||
await extension.entry()
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
type EventCallback = (ctx: { event: string }) => void | Promise<void>
|
||||
|
||||
const eventMap = new Map<string, Set<EventCallback>>()
|
||||
|
||||
export async function importExtensionsByEvent(event: string) {
|
||||
const callbacks = eventMap.get(event)
|
||||
if (!callbacks) return
|
||||
eventMap.delete(event)
|
||||
await Promise.all([...callbacks].map((cb) => cb({ event })))
|
||||
}
|
||||
|
||||
function onceExtImportEvent(event: string, callback: EventCallback) {
|
||||
if (eventMap.has(event)) {
|
||||
eventMap.get(event)!.add(callback)
|
||||
} else {
|
||||
eventMap.set(event, new Set([callback]))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user