use untrack in useSvelteFlow hook

This commit is contained in:
peterkogo
2025-01-20 16:39:37 +01:00
parent 547f59293b
commit 925edfd91e
3 changed files with 48 additions and 98 deletions

View File

@@ -9,9 +9,8 @@
type NodeProps
} from '@xyflow/svelte';
import { isTextNode, type MyNode } from './+page.svelte';
import { untrack } from 'svelte';
let { id, data }: NodeProps<Node<{ text: string }>> = $props();
let { id }: NodeProps<Node<{ text: string }>> = $props();
const { updateNodeData } = useSvelteFlow();
const connections = useNodeConnections({
@@ -20,21 +19,19 @@
});
let nodeData = $derived(useNodesData<MyNode>(connections.current[0]?.source));
let textNodeData = $derived(isTextNode(nodeData.current) ? nodeData.current.data : null);
let textNodeData = $derived(isTextNode(nodeData.current) ? nodeData.current.data.text : null);
$effect(() => {
console.log('textNodeData', textNodeData);
});
// For some reason adding an inspect here also prevents the inifinte loop!?
// $inspect(textNodeData);
$effect.pre(() => {
const nodeData = textNodeData;
const input = nodeData?.text.toUpperCase() ?? '';
// TODO: We need to add this check to prevent infinite loop
// I don't understand why?
if (input === untrack(() => data.text)) return;
$effect(() => {
console.log('running');
textNodeData;
const input = textNodeData?.toUpperCase() ?? '';
updateNodeData(id, { text: input });
console.log('updatedNodeData with', input);
});
</script>