Skip to content

Commit

Permalink
Add workflows store to manage workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei committed Sep 15, 2024
1 parent 347563a commit f55842b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
4 changes: 4 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type { ToastMessageOptions } from 'primevue/toast'
import { useToast } from 'primevue/usetoast'
import { i18n } from './i18n'
import { useExecutionStore } from './stores/executionStore'
import { useWorkflowStore } from './stores/workflowStore'
const isLoading = computed<boolean>(() => useWorkspaceStore().spinner)
const theme = computed<string>(() =>
Expand Down Expand Up @@ -130,6 +131,9 @@ watchEffect(() => {
app.menu.workflows.buttonProgress.style.width = `${executionStore.executionProgress}%`
})
const workflowStore = useWorkflowStore()
app.workflowManager.workflowStore = workflowStore
onMounted(() => {
api.addEventListener('status', onStatus)
api.addEventListener('reconnecting', onReconnecting)
Expand Down
63 changes: 25 additions & 38 deletions src/scripts/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import { appendJsonExt, trimJsonExt } from '@/utils/formatUtil'

export class ComfyWorkflowManager extends EventTarget {
executionStore: any = null
workflowStore: any = null

#unsavedCount = 0
#activeWorkflow: ComfyWorkflow

workflowLookup: Record<string, ComfyWorkflow> = {}
workflows: Array<ComfyWorkflow> = []
openWorkflows: Array<ComfyWorkflow> = []
app: ComfyApp

Expand All @@ -29,6 +28,14 @@ export class ComfyWorkflowManager extends EventTarget {
return this.executionStore?.activePrompt
}

get workflows(): ComfyWorkflow[] {
return this.workflowStore?.workflows || []
}

get workflowLookup(): Record<string, ComfyWorkflow> {
return this.workflowStore?.workflowLookup || {}
}

constructor(app: ComfyApp) {
super()
this.app = app
Expand All @@ -47,8 +54,8 @@ export class ComfyWorkflowManager extends EventTarget {
favorites = new Set()
}

const workflows = (await api.listUserData('workflows', true, true)).map(
(w) => {
;(await api.listUserData('workflows', true, true)).forEach(
(w: string[]) => {
let workflow = this.workflowLookup[w[0]]
if (!workflow) {
workflow = new ComfyWorkflow(
Expand All @@ -59,14 +66,10 @@ export class ComfyWorkflowManager extends EventTarget {
)
this.workflowLookup[workflow.path] = workflow
}
return workflow
}
)

this.workflows = workflows
} catch (error) {
alert('Error loading workflows: ' + (error.message ?? error))
this.workflows = []
}
}

Expand Down Expand Up @@ -167,30 +170,14 @@ export class ComfyWorkflowManager extends EventTarget {
}

export class ComfyWorkflow {
#name
#path
#pathParts
#isFavorite = false
name: string | null = null
path: string | null = null
pathParts: string[] | null = null
isFavorite: boolean = false
changeTracker: ChangeTracker | null = null
unsaved = false
unsaved: boolean = false
manager: ComfyWorkflowManager

get name() {
return this.#name
}

get path() {
return this.#path
}

get pathParts() {
return this.#pathParts
}

get isFavorite() {
return this.#isFavorite
}

get isOpen() {
return !!this.changeTracker
}
Expand All @@ -204,15 +191,15 @@ export class ComfyWorkflow {
this.manager = manager
if (pathParts) {
this.#updatePath(path, pathParts)
this.#isFavorite = isFavorite
this.isFavorite = isFavorite
} else {
this.#name = path
this.name = path
this.unsaved = true
}
}

#updatePath(path: string, pathParts: string[]) {
this.#path = path
this.path = path

if (!pathParts) {
if (!path.includes('\\')) {
Expand All @@ -222,8 +209,8 @@ export class ComfyWorkflow {
}
}

this.#pathParts = pathParts
this.#name = trimJsonExt(pathParts[pathParts.length - 1])
this.pathParts = pathParts
this.name = trimJsonExt(pathParts[pathParts.length - 1])
}

async getWorkflowData() {
Expand Down Expand Up @@ -266,8 +253,8 @@ export class ComfyWorkflow {

async favorite(value: boolean) {
try {
if (this.#isFavorite === value) return
this.#isFavorite = value
if (this.isFavorite === value) return
this.isFavorite = value
await this.manager.saveWorkflowMetadata()
this.manager.dispatchEvent(new CustomEvent('favorite', { detail: this }))
} catch (error) {
Expand Down Expand Up @@ -352,8 +339,8 @@ export class ComfyWorkflow {
}

this.unsaved = true
this.#path = null
this.#pathParts = null
this.path = null
this.pathParts = null
this.manager.workflows.splice(this.manager.workflows.indexOf(this), 1)
this.manager.dispatchEvent(new CustomEvent('delete', { detail: this }))
}
Expand Down
13 changes: 13 additions & 0 deletions src/stores/workflowStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { ComfyWorkflow } from '@/scripts/workflows'

export const useWorkflowStore = defineStore('workflow', () => {
const workflowLookup = ref<Record<string, ComfyWorkflow>>({})
const workflows = computed(() => Object.values(workflowLookup.value))

return {
workflows,
workflowLookup
}
})
4 changes: 2 additions & 2 deletions src/utils/formatUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export function appendJsonExt(path: string) {
return path
}

export function trimJsonExt(path: string) {
return path.replace(/\.json$/, '')
export function trimJsonExt(path?: string) {
return path?.replace(/\.json$/, '')
}

0 comments on commit f55842b

Please sign in to comment.