@rkmodules/rules 0.0.176 → 0.0.177

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.
@@ -7,8 +7,9 @@ import classNames from 'classnames';
7
7
  import { useInner } from '@rkmodules/use-inner';
8
8
  import rcin from 'rc-input-number';
9
9
  import { useLongPress } from 'use-long-press';
10
- import { create as create$1 } from 'zustand';
10
+ import { create as create$1, useStore } from 'zustand';
11
11
  import { persist } from 'zustand/middleware';
12
+ import { createStore as createStore$1 } from 'zustand/vanilla';
12
13
  import { jsx } from 'react/jsx-runtime';
13
14
 
14
15
  /******************************************************************************
@@ -3867,6 +3868,7 @@ var Engine = /** @class */ (function () {
3867
3868
  // register the primitive
3868
3869
  this.fnIndex[primitive.name] = primitive;
3869
3870
  this.graphDefinitions.set(primitive, node);
3871
+ this.fireEvent("functionBuilt", { name: primitive.name, fn: primitive });
3870
3872
  return primitive;
3871
3873
  };
3872
3874
  Engine.prototype.mount = function (node, inputs, options) {
@@ -8005,7 +8007,7 @@ function addOutputToNode(nodes, ref, targetId) {
8005
8007
  * flow state stores nodes and edges for the flow graph these can be updated when the function changes
8006
8008
  * using setFn
8007
8009
  */
8008
- var useFlowState = create$1(function (set, get) { return ({
8010
+ var createFlowStore = function () { return createStore$1(function (set, get) { return ({
8009
8011
  engine: null,
8010
8012
  fn: null,
8011
8013
  options: {},
@@ -8121,25 +8123,48 @@ var useFlowState = create$1(function (set, get) { return ({
8121
8123
  });
8122
8124
  // console.log("final graph", nodes, edges);
8123
8125
  // update the function, nodes and edges
8124
- set({ fn: fn, nodes: nodes, edges: edges });
8126
+ set({ fn: fn, nodes: nodes, edges: edges, options: options });
8127
+ },
8128
+ refreshFunctions: function (name) {
8129
+ var _a = get(), fn = _a.fn, engine = _a.engine, options = _a.options, nodes = _a.nodes;
8130
+ if (!fn || !engine)
8131
+ return;
8132
+ var matching = Object.entries(fn.body || {}).filter(function (_a) {
8133
+ var _b = __read(_a, 2), id = _b[0], call = _b[1];
8134
+ return nodes[id] && (name === undefined || call.name === name);
8135
+ });
8136
+ if (!matching.length)
8137
+ return;
8138
+ var nextNodes = __assign({}, nodes);
8139
+ matching.forEach(function (_a) {
8140
+ var _b = __read(_a, 2), id = _b[0], call = _b[1];
8141
+ var node = nodes[id];
8142
+ var refreshed = createNode(engine, id, call, options);
8143
+ nextNodes[id] = __assign(__assign({}, node), { type: refreshed.type, dragHandle: refreshed.dragHandle, data: __assign(__assign({}, refreshed.data), { outputs: node.data.outputs }) });
8144
+ });
8145
+ set({ nodes: nextNodes });
8125
8146
  },
8126
8147
  init: function (fn, engine, options) {
8127
8148
  // console.log("initing");
8128
8149
  set({ engine: engine, options: options });
8129
8150
  get().setFn(fn, options);
8151
+ // Reconcile builds that happened before the effect subscribed.
8152
+ get().refreshFunctions();
8130
8153
  },
8131
- }); });
8132
- /**
8133
- * creates a flow state and provides handlers to update nodes and edges
8134
- */
8154
+ }); }); };
8155
+
8156
+ /** Creates isolated editor state and observes definitions used by its nodes. */
8135
8157
  function useFlow(fn, engine, options) {
8136
- var state = useFlowState();
8158
+ var store = React.useMemo(function () { return createFlowStore(); }, [engine]);
8159
+ var state = useStore(store);
8137
8160
  React.useEffect(function () {
8138
- state.init(fn, engine, options);
8139
- }, [engine]);
8140
- React.useEffect(function () {
8141
- state.setFn(fn, options);
8142
- }, [fn, options]);
8161
+ var unsubscribe = engine.subscribe("functionBuilt", function (_a) {
8162
+ var name = _a.name;
8163
+ store.getState().refreshFunctions(name);
8164
+ });
8165
+ store.getState().init(fn, engine, options);
8166
+ return unsubscribe;
8167
+ }, [store, engine, fn, options]);
8143
8168
  return React.useMemo(function () { return ({
8144
8169
  nodes: Object.values(state.nodes),
8145
8170
  edges: state.edges,