Skip to content

Commit

Permalink
Add finally handler for rename tree node action (#1403)
Browse files Browse the repository at this point in the history
* Add finally handler for rename tree node action

* nit
  • Loading branch information
huchenlei authored Nov 2, 2024
1 parent caa3ac2 commit cc420b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/components/common/TreeExplorerTreeNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ const errorHandling = useErrorHandling()
const handleRename = errorHandling.wrapWithErrorHandlingAsync(
async (newName: string) => {
await props.node.handleRename(props.node, newName)
renameEditingNode.value = null
},
props.node.handleError
props.node.handleError,
() => {
renameEditingNode.value = null
}
)
const container = ref<HTMLElement | null>(null)
const canDrop = ref(false)
Expand Down
4 changes: 2 additions & 2 deletions src/components/sidebar/tabs/WorkflowsSidebarTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ const renderTreeNode = (node: TreeNode): TreeExplorerNode<ComfyWorkflow> => {
const actions = node.leaf
? {
handleClick,
handleRename: (
handleRename: async (
node: TreeExplorerNode<ComfyWorkflow>,
newName: string
) => {
const workflow = node.data
workflow.rename(newName)
await workflow.rename(newName)
},
handleDelete: workflow.isTemporary
? undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { useI18n } from 'vue-i18n'
import { useTreeExpansion } from '@/hooks/treeHooks'
import { app } from '@/scripts/app'
import { findNodeByKey } from '@/utils/treeUtil'
import { useErrorHandling } from '@/hooks/errorHooks'

const props = defineProps<{
filteredNodeDefs: ComfyNodeDefImpl[]
Expand Down Expand Up @@ -212,13 +211,11 @@ defineExpose({
addNewBookmarkFolder
})

const handleRename = useErrorHandling().wrapWithErrorHandling(
(node: TreeNode, newName: string) => {
if (node.data && node.data.isDummyFolder) {
nodeBookmarkStore.renameBookmarkFolder(node.data, newName)
}
const handleRename = (node: TreeNode, newName: string) => {
if (node.data && node.data.isDummyFolder) {
nodeBookmarkStore.renameBookmarkFolder(node.data, newName)
}
)
}

const showCustomizationDialog = ref(false)
const initialIcon = ref(nodeBookmarkStore.defaultBookmarkIcon)
Expand Down
13 changes: 11 additions & 2 deletions src/hooks/errorHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,34 @@ export function useErrorHandling() {
}

const wrapWithErrorHandling =
(action: (...args: any[]) => any, errorHandler?: (error: any) => void) =>
(
action: (...args: any[]) => any,
errorHandler?: (error: any) => void,
finallyHandler?: () => void
) =>
(...args: any[]) => {
try {
return action(...args)
} catch (e) {
;(errorHandler ?? toastErrorHandler)(e)
} finally {
finallyHandler?.()
}
}

const wrapWithErrorHandlingAsync =
(
action: ((...args: any[]) => Promise<any>) | ((...args: any[]) => any),
errorHandler?: (error: any) => void
errorHandler?: (error: any) => void,
finallyHandler?: () => void
) =>
async (...args: any[]) => {
try {
return await action(...args)
} catch (e) {
;(errorHandler ?? toastErrorHandler)(e)
} finally {
finallyHandler?.()
}
}

Expand Down

0 comments on commit cc420b7

Please sign in to comment.