-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor Dynamic Node Callbacks #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,26 +6,31 @@ import type { | |
|
|
||
| import type { Annotations } from "./annotations"; | ||
|
|
||
| export type TaskType = "task" | "input" | "output"; | ||
|
|
||
| export interface NodeData extends Record<string, unknown> { | ||
| readOnly?: boolean; | ||
| connectable?: boolean; | ||
|
Comment on lines
+12
to
+13
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future: do we have any instance of Graph to be readonly, BUT connectable? I feel that this is single property "readOnly".
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more the opposite scenario that's the concern: the graph is not readonly and node connection is disabled - specifically this lock button I've been growing increasingly discontent with "readOnly" and how it's used specifically for the |
||
| nodeCallbacks?: NodeCallbacks; | ||
| } | ||
|
|
||
| export interface TaskNodeData extends Record<string, unknown> { | ||
| taskSpec?: TaskSpec; | ||
| taskId?: string; | ||
| readOnly?: boolean; | ||
| isGhost?: boolean; | ||
| connectable?: boolean; | ||
| highlighted?: boolean; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most likely the node overlay or highlighter you made would solve this but we haven't gotten around to looking into it. |
||
| callbacks?: TaskNodeCallbacks; | ||
| nodeCallbacks?: NodeCallbacks; | ||
| callbacks?: TaskCallbacks; | ||
| } | ||
|
|
||
| export type NodeAndTaskId = { | ||
| taskId: string; | ||
| nodeId: string; | ||
| }; | ||
|
|
||
| export type TaskType = "task" | "input" | "output"; | ||
|
|
||
| /* Note: Optional callbacks will cause TypeScript to break when applying the callbacks to the Nodes. */ | ||
| interface TaskNodeCallbacks { | ||
| export interface TaskCallbacks { | ||
| setArguments: (args: Record<string, ArgumentType>) => void; | ||
| setAnnotations: (annotations: Annotations) => void; | ||
| setCacheStaleness: (cacheStaleness: string | undefined) => void; | ||
|
|
@@ -35,13 +40,14 @@ interface TaskNodeCallbacks { | |
| } | ||
|
|
||
| // Dynamic Node Callback types - every callback has a version with the node & task id added to it as an input parameter | ||
| export type CallbackWithIds<K extends keyof TaskNodeCallbacks> = | ||
| TaskNodeCallbacks[K] extends (...args: infer A) => infer R | ||
| ? (ids: NodeAndTaskId, ...args: A) => R | ||
| : never; | ||
| type CallbackWithIds<K extends keyof TaskCallbacks> = TaskCallbacks[K] extends ( | ||
| ...args: infer A | ||
| ) => infer R | ||
| ? (ids: NodeAndTaskId, ...args: A) => R | ||
| : never; | ||
|
|
||
| export type NodeCallbacks = { | ||
| [K in keyof TaskNodeCallbacks]: CallbackWithIds<K>; | ||
| [K in keyof TaskCallbacks]: CallbackWithIds<K>; | ||
| }; | ||
|
|
||
| export type TaskNodeDimensions = { w: number; h: number | undefined }; | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { | ||
| NodeAndTaskId, | ||
| NodeCallbacks, | ||
| TaskCallbacks, | ||
| } from "@/types/taskNode"; | ||
|
|
||
| // Sync TaskCallbacks with NodeCallbacks by injecting nodeId and taskId | ||
| export const convertNodeCallbacksToTaskCallbacks = ( | ||
| ids: NodeAndTaskId, | ||
| nodeCallbacks?: NodeCallbacks, | ||
| ): TaskCallbacks => { | ||
| if (!nodeCallbacks) { | ||
| return createEmptyTaskCallbacks(); | ||
| } | ||
| return { | ||
| setArguments: (args) => nodeCallbacks.setArguments?.(ids, args), | ||
| setAnnotations: (annotations) => | ||
| nodeCallbacks.setAnnotations?.(ids, annotations), | ||
| setCacheStaleness: (cacheStaleness) => | ||
| nodeCallbacks.setCacheStaleness?.(ids, cacheStaleness), | ||
| onDelete: () => nodeCallbacks.onDelete?.(ids), | ||
| onDuplicate: (selected) => nodeCallbacks.onDuplicate?.(ids, selected), | ||
| onUpgrade: (newComponentRef) => | ||
| nodeCallbacks.onUpgrade?.(ids, newComponentRef), | ||
| }; | ||
| }; | ||
|
|
||
| const createEmptyTaskCallbacks = (): TaskCallbacks => ({ | ||
| setArguments: () => {}, | ||
| setAnnotations: () => {}, | ||
| setCacheStaleness: () => {}, | ||
| onDelete: () => {}, | ||
| onDuplicate: () => {}, | ||
| onUpgrade: () => {}, | ||
| }); |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future: Do we need to have more arbitrary properties on this object? extending it from
Recordmakes it harder to type causing weird code to appear down the road, e.g.TaskNode:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vaguely recall trying to not extend and ReactFlow threw up a huge amount of type errors. There may be some larger changes need to our type structure to make this happen, but I do agree with the idea.