chore(useNodesData): cleanup return type

This commit is contained in:
moklick
2024-02-26 14:13:12 +01:00
parent 89a90cb848
commit 3fc3b7e603
6 changed files with 24 additions and 43 deletions

View File

@@ -1,15 +1,13 @@
import { memo } from 'react';
import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react';
import { TextNode, isTextNode, type MyNode } from '.';
import { isTextNode, type MyNode } from '.';
function ResultNode() {
const connections = useHandleConnections({
type: 'target',
});
const nodesData = useNodesData<MyNode>(connections.map((connection) => connection.source));
const textNodes = nodesData.filter(isTextNode) as TextNode[];
textNodes[0].data;
const textNodes = nodesData.filter(isTextNode);
return (
<div style={{ background: '#eee', color: '#222', padding: 10, fontSize: 12, borderRadius: 10 }}>

View File

@@ -20,8 +20,8 @@ export type ResultNode = Node<{}, 'result'>;
export type UppercaseNode = Node<{ text: string }, 'uppercase'>;
export type MyNode = TextNode | ResultNode | UppercaseNode;
export function isTextNode(node: any): node is TextNode | UppercaseNode {
return node.type === 'text' || node.type === 'uppercase';
export function isTextNode(node: any): node is TextNode {
return node.type === 'text';
}
const nodeTypes = {

View File

@@ -1,13 +1,15 @@
<script context="module" lang="ts">
import TextNode from './TextNode.svelte';
import UppercaseNode from './UppercaseNode.svelte';
import ResultNode from './ResultNode.svelte';
import type { Node } from '@xyflow/svelte';
export function isTextNode(node: any): node is TextNode | UppercaseNode {
return node.type === 'text' || node.type === 'uppercase';
type TextNodeType = Node<{ text: string }, 'text'>;
type UppercaseNodeType = Node<{ text: string }, 'uppercase'>;
type ResultNodeType = Node<{}, 'result'>;
export function isTextNode(node: any): node is TextNodeType {
return node.type === 'text';
}
export type MyNode = TextNode | UppercaseNode | ResultNode;
export type MyNode = TextNodeType | UppercaseNodeType | ResultNodeType;
</script>
<script lang="ts">
@@ -18,19 +20,22 @@
Background,
BackgroundVariant,
MiniMap,
type Node,
type NodeTypes,
type Edge
} from '@xyflow/svelte';
import '@xyflow/svelte/dist/style.css';
import TextNode from './TextNode.svelte';
import UppercaseNode from './UppercaseNode.svelte';
import ResultNode from './ResultNode.svelte';
const nodeTypes: NodeTypes = {
text: TextNode,
uppercase: UppercaseNode,
result: ResultNode
};
const nodes = writable<Node[]>([
const nodes = writable<MyNode[]>([
{
id: '1',
type: 'text',

View File

@@ -6,7 +6,7 @@
useNodesData,
type NodeProps
} from '@xyflow/svelte';
import { isTextNode } from './+page.svelte';
import { isTextNode, type MyNode } from './+page.svelte';
type $$Props = NodeProps;
@@ -17,7 +17,7 @@
type: 'target'
});
$: nodeData = useNodesData($connections.map((connection) => connection.source));
$: nodeData = useNodesData<MyNode>($connections.map((connection) => connection.source));
$: textNodes = $nodeData.filter(isTextNode);
</script>

View File

@@ -20,18 +20,8 @@ export interface NodeDataReturn<NodeType extends Node> {
*/
export function useNodesData<NodeType extends Node = Node>(
nodeId: string
): {
id: string;
type: NodeType['type'];
data: NodeType['data'];
} | null;
export function useNodesData<NodeType extends Node = Node>(
nodeIds: string[]
): {
id: string;
type: NodeType['type'];
data: NodeType['data'];
}[];
): Pick<NodeType, 'id' | 'type' | 'data'> | null;
export function useNodesData<NodeType extends Node = Node>(nodeIds: string[]): Pick<NodeType, 'id' | 'type' | 'data'>[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useNodesData(nodeIds: any): any {
const nodesData = useStore(

View File

@@ -22,33 +22,21 @@ function areNodesDataEqual(a: any[], b: any[]) {
*
* @public
* @param nodeId - The id (or ids) of the node to get the data from
* @param guard - Optional guard function to narrow down the node type
* @returns A readable store with an array of data objects
*/
export function useNodesData<NodeType extends Node = Node>(
nodeId: string
): Readable<{
id: string;
type: NodeType['type'];
data: NodeType['data'];
} | null>;
): Readable<Pick<NodeType, 'id' | 'data' | 'type'> | null>;
export function useNodesData<NodeType extends Node = Node>(
nodeIds: string[]
): Readable<
{
id: string;
type: NodeType['type'];
data: NodeType['data'];
}[]
>;
): Readable<Pick<NodeType, 'id' | 'data' | 'type'>[]>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useNodesData(nodeIds: any): any {
const { nodes, nodeLookup } = useStore();
let prevNodesData: any = [];
return derived([nodes, nodeLookup], ([, nodeLookup], set) => {
let nextNodesData = [];
const nextNodesData = [];
const isArrayOfIds = Array.isArray(nodeIds);
const _nodeIds = isArrayOfIds ? nodeIds : [nodeIds];