refactor(useNodesData): improve selector equality function (#3976)

* improved selector equality function for useNodesData

* fixed selector & useNodesData example

* added shallow-node-data as system function
This commit is contained in:
peterkogo
2024-03-05 14:46:25 +01:00
committed by GitHub
parent 3cd386c62b
commit 69e0be6d32
6 changed files with 29 additions and 27 deletions
+2 -15
View File
@@ -1,22 +1,9 @@
import { derived, type Readable } from 'svelte/store';
import { shallowNodeData } from '@xyflow/system';
import type { Node } from '$lib/types';
import { useStore } from '$lib/store';
function areNodesDataEqual(a: any[], b: any[]) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i].data !== b[i].data) {
return false;
}
}
return true;
}
/**
* Hook for receiving data of one or multiple nodes
*
@@ -51,7 +38,7 @@ export function useNodesData(nodeIds: any): any {
}
}
if (!areNodesDataEqual(nextNodesData, prevNodesData)) {
if (!shallowNodeData(nextNodesData, prevNodesData)) {
prevNodesData = nextNodesData;
set(isArrayOfIds ? nextNodesData : nextNodesData[0] ?? null);
}