Skip to content

Commit f1856b7

Browse files
LittleSoundDrJKL
authored andcommitted
refactor: activation events + contributes
1 parent bb2c997 commit f1856b7

File tree

12 files changed

+298
-15
lines changed

12 files changed

+298
-15
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineComfyExtConfig } from '@/extensions/utils'
22

33
export default defineComfyExtConfig({
4+
name: 'Comfy.Cloud.Badges',
5+
activationEvents: ['*'],
46
comfyCloud: true,
57
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineComfyExtConfig } from '@/extensions/utils'
22

33
export default defineComfyExtConfig({
4+
name: 'Comfy.Cloud.FeedbackButton',
5+
activationEvents: ['*'],
46
comfyCloud: true,
57
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineComfyExtConfig } from '@/extensions/utils'
22

33
export default defineComfyExtConfig({
4+
name: 'Comfy.Cloud.RemoteConfig',
5+
activationEvents: ['*'],
46
comfyCloud: true,
57
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineComfyExtConfig } from '@/extensions/utils'
22

33
export default defineComfyExtConfig({
4+
name: 'Comfy.Cloud.SessionCookie',
5+
activationEvents: ['*'],
46
comfyCloud: true,
57
})

src/extensions/core/extensions/cloudSubscription/comfy.ext.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { defineComfyExtConfig } from '@/extensions/utils'
22

33
export default defineComfyExtConfig({
4+
name: 'Comfy.Cloud.Subscription',
5+
activationEvents: ['*'],
46
comfyCloud: {
57
subscriptionRequired: true,
68
},
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { defineComfyExtConfig } from '@/extensions/utils'
2+
3+
export default defineComfyExtConfig({
4+
name: 'Comfy.Load3D',
5+
activationEvents: ['onWidgets:contributes', 'onCommands:contributes', 'onSettings:contributes'],
6+
contributes: [
7+
{
8+
name: 'Comfy.Preview3D',
9+
widgets: ['PREVIEW_3D'],
10+
},
11+
{
12+
name: 'Comfy.Load3D',
13+
widgets: ['LOAD_3D'],
14+
settings: [
15+
{
16+
id: 'Comfy.Load3D.ShowGrid',
17+
category: ['3D', 'Scene', 'Initial Grid Visibility'],
18+
name: 'Initial Grid Visibility',
19+
tooltip:
20+
'Controls whether the grid is visible by default when a new 3D widget is created. This default can still be toggled individually for each widget after creation.',
21+
type: 'boolean',
22+
defaultValue: true,
23+
experimental: true
24+
},
25+
{
26+
id: 'Comfy.Load3D.BackgroundColor',
27+
category: ['3D', 'Scene', 'Initial Background Color'],
28+
name: 'Initial Background Color',
29+
tooltip:
30+
'Controls the default background color of the 3D scene. This setting determines the background appearance when a new 3D widget is created, but can be adjusted individually for each widget after creation.',
31+
type: 'color',
32+
defaultValue: '282828',
33+
experimental: true
34+
},
35+
{
36+
id: 'Comfy.Load3D.CameraType',
37+
category: ['3D', 'Camera', 'Initial Camera Type'],
38+
name: 'Initial Camera Type',
39+
tooltip:
40+
'Controls whether the camera is perspective or orthographic by default when a new 3D widget is created. This default can still be toggled individually for each widget after creation.',
41+
type: 'combo',
42+
options: ['perspective', 'orthographic'],
43+
defaultValue: 'perspective',
44+
experimental: true
45+
},
46+
{
47+
id: 'Comfy.Load3D.LightIntensity',
48+
category: ['3D', 'Light', 'Initial Light Intensity'],
49+
name: 'Initial Light Intensity',
50+
tooltip:
51+
'Sets the default brightness level of lighting in the 3D scene. This value determines how intensely lights illuminate objects when a new 3D widget is created, but can be adjusted individually for each widget after creation.',
52+
type: 'number',
53+
defaultValue: 3,
54+
experimental: true
55+
},
56+
{
57+
id: 'Comfy.Load3D.LightIntensityMaximum',
58+
category: ['3D', 'Light', 'Light Intensity Maximum'],
59+
name: 'Light Intensity Maximum',
60+
tooltip:
61+
'Sets the maximum allowable light intensity value for 3D scenes. This defines the upper brightness limit that can be set when adjusting lighting in any 3D widget.',
62+
type: 'number',
63+
defaultValue: 10,
64+
experimental: true
65+
},
66+
{
67+
id: 'Comfy.Load3D.LightIntensityMinimum',
68+
category: ['3D', 'Light', 'Light Intensity Minimum'],
69+
name: 'Light Intensity Minimum',
70+
tooltip:
71+
'Sets the minimum allowable light intensity value for 3D scenes. This defines the lower brightness limit that can be set when adjusting lighting in any 3D widget.',
72+
type: 'number',
73+
defaultValue: 1,
74+
experimental: true
75+
},
76+
{
77+
id: 'Comfy.Load3D.LightAdjustmentIncrement',
78+
category: ['3D', 'Light', 'Light Adjustment Increment'],
79+
name: 'Light Adjustment Increment',
80+
tooltip:
81+
'Controls the increment size when adjusting light intensity in 3D scenes. A smaller step value allows for finer control over lighting adjustments, while a larger value results in more noticeable changes per adjustment.',
82+
type: 'slider',
83+
attrs: {
84+
min: 0.1,
85+
max: 1,
86+
step: 0.1
87+
},
88+
defaultValue: 0.5,
89+
experimental: true
90+
},
91+
{
92+
id: 'Comfy.Load3D.3DViewerEnable',
93+
category: ['3D', '3DViewer', 'Enable'],
94+
name: 'Enable 3D Viewer (Beta)',
95+
tooltip:
96+
'Enables the 3D Viewer (Beta) for selected nodes. This feature allows you to visualize and interact with 3D models directly within the full size 3d viewer.',
97+
type: 'boolean',
98+
defaultValue: false,
99+
experimental: true
100+
}
101+
],
102+
commands: [
103+
{
104+
id: 'Comfy.3DViewer.Open3DViewer',
105+
icon: 'pi pi-pencil',
106+
label: 'Open 3D Viewer (Beta) for Selected Node',
107+
}
108+
],
109+
},
110+
],
111+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineComfyExtConfig } from '@/extensions/utils'
2+
3+
export default defineComfyExtConfig({
4+
name: 'Comfy.SaveGLB',
5+
activationEvents: ['onWidgets:contributes', 'onCommands:contributes', 'onSettings:contributes'],
6+
contributes: [
7+
{
8+
name: 'Comfy.SaveGLB',
9+
widgets: ['SAVE_GLB'],
10+
},
11+
],
12+
})

src/extensions/core/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { dispatchComfyExtensions } from '../dispatch'
2+
import type { ComfyExtensionConfigs, ComfyExtensionEntrance } from '../types'
23

3-
export async function importExtensions() {
4+
export async function registerExtensions() {
45
console.log('importExtensions running...')
56

67
const extConfigs = import.meta.glob(`./extensions/*/comfy.ext.config.ts`, {
78
// Since each config is small, we only import the default export and use eager mode for better tree-shaking and performance.
89
import: 'default',
910
eager: true,
10-
})
11-
const extensionEntrance = import.meta.glob(`./extensions/*/index.ts`)
11+
}) as ComfyExtensionConfigs
12+
const extensionEntrance = import.meta.glob(`./extensions/*/index.ts`) as ComfyExtensionEntrance
1213

1314
dispatchComfyExtensions({ configs: extConfigs, entrance: extensionEntrance })
1415
}

src/extensions/dispatch.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
ComfyExtensionEntrance,
55
ComfyExtensionLoadContext
66
} from './types'
7-
import { formatExtensions, shouldLoadExtension } from './utils'
7+
import { formatExtensions, normalizationActivationEvents } from './utils'
88

99
const extLoadContext: ComfyExtensionLoadContext = {
1010
get isCloud() {
@@ -22,11 +22,61 @@ export async function dispatchComfyExtensions(options: {
2222
const { configs, entrance } = options
2323
const extensions = formatExtensions(entrance, configs)
2424
for (const extension of Object.values(extensions)) {
25-
if (shouldLoadExtension(extLoadContext, extension.config)) {
26-
const module = await extension.entry()
27-
console.log('✅ extension', extension.name, 'loaded', extension, module)
25+
// if (shouldLoadExtension(extLoadContext, extension.config)) {
26+
// const module = await extension.entry()
27+
// console.log('✅ extension', extension.name, 'loaded', extension, module)
28+
// } else {
29+
// console.log('❌ extension', extension.name, 'disabled', extension.config)
30+
// }
31+
const activationEvents = normalizationActivationEvents(
32+
extLoadContext,
33+
extension.config
34+
)
35+
if (!activationEvents.length) {
36+
console.log(
37+
'❌ extension',
38+
extension.name,
39+
'has no activation events',
40+
extension.config
41+
)
2842
} else {
29-
console.log('❌ extension', extension.name, 'disabled', extension.config)
43+
console.log(
44+
'🧶 extension',
45+
extension.name,
46+
'has activation events:',
47+
activationEvents
48+
)
3049
}
50+
activationEvents.forEach((event) =>
51+
onceExtImportEvent(event, async ({ event }) => {
52+
console.log(
53+
'✅ extension',
54+
extension.name,
55+
'loaded by',
56+
event,
57+
extension
58+
)
59+
await extension.entry()
60+
})
61+
)
62+
}
63+
}
64+
65+
type EventCallback = (ctx: { event: string }) => void | Promise<void>
66+
67+
const eventMap = new Map<string, Set<EventCallback>>()
68+
69+
export async function importExtensionsByEvent(event: string) {
70+
const callbacks = eventMap.get(event)
71+
if (!callbacks) return
72+
eventMap.delete(event)
73+
await Promise.all([...callbacks].map((cb) => cb({ event })))
74+
}
75+
76+
function onceExtImportEvent(event: string, callback: EventCallback) {
77+
if (eventMap.has(event)) {
78+
eventMap.get(event)!.add(callback)
79+
} else {
80+
eventMap.set(event, new Set([callback]))
3181
}
3282
}

src/extensions/types.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
1+
import type { ComfyExtension } from '@/types'
2+
3+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
4+
type ExcludeFunctions<T> = T extends Function ? never : T
5+
type StaticOnly<T> = {
6+
// eslint-disable-next-line prettier/prettier
7+
[K in keyof T as ExcludeFunctions<T[K]> extends never ? never : K]: ExcludeFunctions<T[K]>
8+
}
9+
10+
type StaticComfyCommand = StaticOnly<
11+
NonNullable<ComfyExtension['commands']>[number]
12+
>
13+
type StaticComfySettingParams = StaticOnly<
14+
NonNullable<ComfyExtension['settings']>[number]
15+
>
16+
type StaticComfyKeybinding = StaticOnly<
17+
NonNullable<ComfyExtension['keybindings']>[number]
18+
>
19+
type StaticComfyMenuCommandGroup = StaticOnly<
20+
NonNullable<ComfyExtension['menuCommands']>[number]
21+
>
22+
23+
export type ComfyExtensionActivationEvent =
24+
| '*'
25+
| 'onWidgets:contributes'
26+
| 'onCommands:contributes'
27+
| 'onSettings:contributes'
28+
29+
type ComfyExtensionContributes = {
30+
name: string
31+
widgets?: string[]
32+
commands?: StaticComfyCommand[]
33+
settings?: StaticComfySettingParams[]
34+
keybindings?: StaticComfyKeybinding[]
35+
}
36+
137
export interface ComfyExtensionConfig {
38+
name?: string
39+
40+
activationEvents: ComfyExtensionActivationEvent[]
41+
42+
contributes?: ComfyExtensionContributes | ComfyExtensionContributes[]
43+
menuCommands?: StaticComfyMenuCommandGroup[]
44+
245
comfyCloud?:
346
| boolean
447
| {

0 commit comments

Comments
 (0)