@rkmodules/rules 0.0.25 → 0.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import { Tree } from "../DataTree";
2
2
  import { GraphedFunction, NormalizedVarDef, PrimitiveFunction, AnyFunction, VarDef, VarRef, FunctionCall } from "./types";
3
3
  export * from "./types";
4
4
  export * from "./util";
5
- interface Ref {
5
+ export interface Ref {
6
6
  name: string;
7
7
  path: string;
8
8
  parts: string[];
@@ -47,8 +47,8 @@ export declare class Engine {
47
47
  fireEvent(eventName: string, event: EngineEvent): void;
48
48
  run(node: AnyFunction, inputs?: VarRef): any;
49
49
  getFunction(name: string): PrimitiveFunction | undefined;
50
- applyNodeDelete(fn: GraphedFunction, nodeIds: string[]): GraphedFunction;
51
- applyEdgeDelete(fn: GraphedFunction, fromNode: string, toNode: string, fromField: string, toField: string): GraphedFunction;
52
- applyNodeAdd(fn: GraphedFunction, nodeName: string, callback?: (newNode: FunctionCall, id: string) => void): GraphedFunction;
53
- applyNodeConnect(fn: GraphedFunction, fromNode: string, toNode: string, fromField: string, toField: string): GraphedFunction;
50
+ applyNodeDelete(oldFn: GraphedFunction, nodeIds: string[]): GraphedFunction;
51
+ applyEdgeDelete(oldFn: GraphedFunction, fromNode: string, toNode: string, fromField: string, toField: string): GraphedFunction;
52
+ applyNodeAdd(oldFn: GraphedFunction, nodeName: string, callback?: (newNode: FunctionCall, id: string) => void): GraphedFunction;
53
+ applyNodeConnect(oldFn: GraphedFunction, fromNode: string, toNode: string, fromField: string, toField: string): GraphedFunction;
54
54
  }
@@ -2,12 +2,17 @@ import React from "react";
2
2
  import { XYPosition } from "@xyflow/react";
3
3
  import { GraphedFunction } from "../Engine/types";
4
4
  import { Engine } from "../Engine";
5
+ import { Updater } from "./types";
5
6
  import "./style/xyflow.css";
6
7
  export * from "./types";
8
+ /**
9
+ * goal: this component should only be responsible for updating the inner node graph, it should call the onChange handler
10
+ * on any change, but not rerender when the given function changes, as it slows down the interface drastically
11
+ */
7
12
  interface FlowProps {
8
13
  function: GraphedFunction;
9
14
  engine: Engine;
10
- onChange?: (fn: GraphedFunction) => void;
15
+ onChange?: (updater: Updater<GraphedFunction>) => void;
11
16
  onClick?: (event: React.MouseEvent, position: XYPosition) => void;
12
17
  onSelect?: (ids: string[]) => void;
13
18
  }
@@ -1,8 +1,7 @@
1
1
  import { Node } from "@xyflow/react";
2
- import { GraphedFunction, PrimitiveFunction, VarDef, VarRef } from "../Engine";
2
+ import { PrimitiveFunction, VarDef, VarRef } from "../Engine";
3
3
  export type FunctionNode = Node<{
4
4
  name: string;
5
- caller: GraphedFunction;
6
5
  inputs?: VarRef;
7
6
  params?: VarRef;
8
7
  inputDefs: Record<string, VarDef>;
@@ -14,3 +13,4 @@ export type NodeDropItem = {
14
13
  name: string;
15
14
  fn: PrimitiveFunction;
16
15
  };
16
+ export type Updater<T> = (oldValue: T) => T;
@@ -1,4 +1,3 @@
1
- import React from "react";
2
1
  import { Edge } from "@xyflow/react";
3
2
  import { Engine, GraphedFunction } from "../Engine";
4
3
  import { FunctionNode } from "../Flow";
@@ -6,15 +5,26 @@ export type PositionData = Record<string, {
6
5
  x: number;
7
6
  y: number;
8
7
  }>;
9
- type CreateGraphOptions = {
8
+ /**
9
+ * TODO: replace this whole thing with a zustand state
10
+ * - whenever the outer function changes, update the inner node graph
11
+ * - but do so useing shallow comparison, so only update changed nodes
12
+ * - also store the node positions in the zustand state, so they are not lost on rerender
13
+ *
14
+ * - state is not persisted in local storage, that is the job of the outer function variable in the page
15
+ */
16
+ interface FlowOptions {
10
17
  dragHandle?: string;
11
- setNodes: React.Dispatch<React.SetStateAction<FunctionNode[]>>;
12
- setEdges: React.Dispatch<React.SetStateAction<Edge[]>>;
13
- onChange?: (fn: GraphedFunction) => void;
14
- };
15
- export declare function createGraph(fn: GraphedFunction, engine: Engine, positions?: PositionData, options?: CreateGraphOptions): {
18
+ positions?: PositionData;
19
+ onDataChange?: (id: string) => (newData: Partial<FunctionNode["data"]>) => void;
20
+ }
21
+ /**
22
+ * creates a flow state and provides handlers to update nodes and edges
23
+ */
24
+ export declare function useFlow(fn: GraphedFunction, engine: Engine, options: FlowOptions): {
16
25
  nodes: FunctionNode[];
17
26
  edges: Edge[];
27
+ setNodes: (updater: (nodes: FunctionNode[]) => FunctionNode[]) => void;
28
+ setEdges: (updater: (edges: Edge[]) => Edge[]) => void;
18
29
  };
19
- export declare function useFlow(fn: GraphedFunction, engine: Engine, options: CreateGraphOptions): void;
20
30
  export {};
@@ -3,6 +3,6 @@ type Point = {
3
3
  x: number;
4
4
  y: number;
5
5
  };
6
- export declare function usePositions(fn: GraphedFunction): [Record<string, Point>, (t: Record<string, Point>) => void];
6
+ export declare function usePositions(fn: GraphedFunction): [Record<string, Point>, (t: Record<string, Point> | ((oldValue: Record<string, Point>) => Record<string, Point>)) => void];
7
7
  export declare function useUpdatePositions(fn: GraphedFunction): (id: string, position: Point) => void;
8
8
  export {};
@@ -1,6 +1,8 @@
1
+ type Updater<T> = (oldValue: T) => T;
1
2
  /**
2
3
  * scope restricts the variable scope, for example to a view id
3
4
  * @param scope
4
5
  * @param name
5
6
  */
6
- export declare const useVariable: <T = any>(scope: string, name: string, initial: T, persist?: boolean) => [T, (t: T) => void];
7
+ export declare const useVariable: <T = any>(scope: string, name: string, initial: T, persist?: boolean) => [T, (t: T | Updater<T>) => void];
8
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rkmodules/rules",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
4
4
  "main": "dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [