Skip to content

Commit

Permalink
feat: shoehorn in an updater function on useNodeState
Browse files Browse the repository at this point in the history
  • Loading branch information
danlannan777 committed May 23, 2024
1 parent d8690c3 commit 44f7ccc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"neo4j-driver": "^5.19.0",
"next": "^14.2.0-canary.62",
"zod": "^3.22.5",
"zustand": "^4.5.2"
"zustand": "^4.5.2",
"react": "^18"
},
"devDependencies": {
"@babel/preset-env": "^7.24.4",
Expand Down
13 changes: 11 additions & 2 deletions src/layers/ReactCache/defineReactCacheLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { enableMapSet, Draft, produce } from 'immer'
import { immer } from "zustand/middleware/immer";
import { useImmer } from "@thinairthings/use-immer";
import { OmitNodeConstants } from "@/src/base/defineBaseGraph";
import { useEffect } from "react";



Expand Down Expand Up @@ -35,7 +36,8 @@ export const defineReactCacheLayer = <
Node extends UixNode<T, TypeOf<(N[number] & { nodeType: T })['stateDefinition']>> | undefined = undefined
>(
nodeType: T,
node?: Node
node?: Node,
updateAction?: (...args: any[]) => Promise<any>
) => ReturnType<typeof useImmer<Node extends UixNode<T, TypeOf<(N[number] & { nodeType: T })['stateDefinition']>>
? (Omit<TypeOf<(N[number] & { nodeType: T })['stateDefinition']>, keyof TypeOf<(N[number] & { nodeType: T })['stateDefaults']>>
& TypeOf<(N[number] & { nodeType: T })['stateDefaults']>)
Expand Down Expand Up @@ -76,8 +78,15 @@ export const defineReactCacheLayer = <
// )
return {
...graph,
useNodeState: (nodeType, node) => {
useNodeState: (nodeType, node, updater) => {
const [nodeState, updateNodeState] = useImmer(graph.getNodeDefinition(nodeType).stateDefaults.parse(node ?? {}))
useEffect(() => {
return () => {
(async () => {
await updater?.(nodeState)
})()
}
}, [nodeState])
return [nodeState, updateNodeState] as any
},
}
Expand Down

0 comments on commit 44f7ccc

Please sign in to comment.