@plasmicapp/react-web 0.2.139 → 0.2.141

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.
@@ -6,12 +6,16 @@ export interface StateCell<T> {
6
6
  listeners: (() => void)[];
7
7
  }
8
8
  export declare class StateSpecNode<T> {
9
- private specs;
10
- private edges;
11
- private state;
9
+ private _specs;
10
+ private _edges;
11
+ private _state;
12
12
  constructor(specs: Internal$StateSpec<T>[]);
13
+ setSpecs(specs: Internal$StateSpec<T>[]): void;
14
+ edges(): Map<string | symbol, StateSpecNode<any>>;
15
+ state(): Record<string, StateCell<T>>;
13
16
  hasEdge(key: string | symbol): boolean;
14
17
  addEdge(key: string | symbol, node: StateSpecNode<any>): void;
18
+ clearEdges(): void;
15
19
  children(): IterableIterator<StateSpecNode<any>>;
16
20
  makeTransition(key: string | symbol | number): StateSpecNode<any> | undefined;
17
21
  isLeaf(): boolean;
@@ -19,6 +23,7 @@ export declare class StateSpecNode<T> {
19
23
  getSpec(): Internal$StateSpec<T>;
20
24
  getAllSpecs(): Internal$StateSpec<T>[];
21
25
  getState(path: ObjectPath): StateCell<T>;
26
+ getInitFunc(stateCell: StateCell<any>): InitFunc<any> | undefined;
22
27
  clearStates(): void;
23
28
  states(): StateCell<T>[];
24
29
  hasState(path: ObjectPath): boolean;
@@ -29,7 +34,8 @@ export declare class StateSpecNode<T> {
29
34
  }
30
35
  export declare const transformPathStringToObj: (str: string) => (string | symbol)[];
31
36
  export declare function buildTree(specs: $StateSpec<any>[]): StateSpecNode<any>;
32
- export declare function getLeaves(root: StateSpecNode<any>): StateSpecNode<any>[];
37
+ export declare function updateTree(root: StateSpecNode<any>, specs: $StateSpec<any>[]): StateSpecNode<any>;
38
+ export declare function getStateCells(root: StateSpecNode<any>): StateSpecNode<any>[];
33
39
  export declare function findStateCell(root: StateSpecNode<any>, pathStr: string, repetitionIndex?: number[]): {
34
40
  node: StateSpecNode<any>;
35
41
  realPath: ObjectPath;
@@ -1,16 +1,14 @@
1
1
  import { useLayoutEffect } from "react";
2
- import { $State } from "./types";
3
- export declare function generateStateOnChangeProp($state: $State, stateName: string, dataReps: number[]): (val: any, path: (string | number)[]) => void;
4
- /**
5
- * This function generate the state value prop for repeated states
6
- * Example:
7
- * - parent[][].counter[].count
8
- * We need to pass `parent[index1][index2].counter to the child component
9
- */
10
- export declare function generateStateValueProp($state: $State, path: (string | number)[]): any;
2
+ import { $State, ObjectPath } from "./types";
3
+ export declare function generateStateOnChangeProp($state: $State, path: ObjectPath): (val: any) => void;
4
+ export declare function generateStateValueProp($state: $State, path: ObjectPath): any;
11
5
  export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
12
6
  export declare function isPlasmicStateProxy(obj: any): any;
13
7
  export declare function shallowEqual<T>(a1: T[], a2: T[]): boolean;
8
+ /**
9
+ * Shallow comparison of arrays.
10
+ */
11
+ export declare function arrayEq(xs: ReadonlyArray<any>, ys: ReadonlyArray<any>): boolean;
14
12
  export declare function isNum(value: string | number | symbol): value is number;
15
13
  declare type StringGen = string | (() => string);
16
14
  export declare function assert<T>(cond: T, msg?: StringGen): asserts cond;
@@ -1,3 +1,5 @@
1
1
  import { $State, $StateSpec } from "./types";
2
- export declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>): $State;
2
+ export declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>, opts?: {
3
+ inCanvas: boolean;
4
+ }): $State;
3
5
  export default useDollarState;
@@ -43,6 +43,12 @@ export declare const InitFuncFromRootContextData: import("@storybook/csf").Annot
43
43
  name: string;
44
44
  }[];
45
45
  }>;
46
+ export declare const InitFuncFromInternalContextDataWithDelay: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {
47
+ products: {
48
+ price: number;
49
+ name: string;
50
+ }[];
51
+ }>;
46
52
  export declare const RepeatedImplicitState: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {}>;
47
53
  interface Person {
48
54
  firstName: string;
@@ -62,3 +68,4 @@ export declare const IsOnChangePropImmediatelyFired: import("@storybook/csf").An
62
68
  export declare const ImmutableStateCells: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {
63
69
  people: Person[];
64
70
  }>;
71
+ export declare const InCanvasDollarState: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {}>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/react-web",
3
- "version": "0.2.139",
3
+ "version": "0.2.141",
4
4
  "description": "plasmic library for rendering in the presentational style",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,10 +40,10 @@
40
40
  },
41
41
  "prettier": {},
42
42
  "dependencies": {
43
- "@plasmicapp/data-sources": "0.1.23",
44
- "@plasmicapp/data-sources-context": "0.1.3",
45
- "@plasmicapp/host": "1.0.93",
46
- "@plasmicapp/query": "0.1.58",
43
+ "@plasmicapp/data-sources": "0.1.24",
44
+ "@plasmicapp/data-sources-context": "0.1.4",
45
+ "@plasmicapp/host": "1.0.94",
46
+ "@plasmicapp/query": "0.1.59",
47
47
  "@react-aria/checkbox": "^3.5.0",
48
48
  "@react-aria/focus": "^3.7.0",
49
49
  "@react-aria/interactions": "^3.10.0",
@@ -109,5 +109,5 @@
109
109
  "react": ">=16.8.0",
110
110
  "react-dom": ">=16.8.0"
111
111
  },
112
- "gitHead": "113724512ddcaec7e02a83efbfadf6826f374ed9"
112
+ "gitHead": "412d7cef82888017b8a4fad4acc87e3e6be13562"
113
113
  }
@@ -494,10 +494,17 @@ function renderPlasmicSlot(opts) {
494
494
  }
495
495
  function maybeAsString(node) {
496
496
  // Unwrap fragments
497
- if (React.isValidElement(node) &&
498
- // Fragment and Trans don't render DOM elements
499
- (node.type === React.Fragment || node.type === Trans)) {
500
- return maybeAsString(node.props.children);
497
+ if (React.isValidElement(node)) {
498
+ // Fragment doesn't render DOM elements
499
+ if (node.type === React.Fragment) {
500
+ return maybeAsString(node.props.children);
501
+ }
502
+ else if (node.type === Trans) {
503
+ // Trans also doesn't render DOM elements. But we don't want to just render
504
+ // its content string, because we want to keep the <Trans/> for the localization.
505
+ // So we render the same node, to be wrapped into __wab_slot-string-wrapper.
506
+ return node;
507
+ }
501
508
  }
502
509
  if (typeof node === "string") {
503
510
  return node;
@@ -663,17 +670,10 @@ function useTrigger(trigger, opts) {
663
670
  var ARRAY_SYMBOL = Symbol("[]");
664
671
  var PLASMIC_STATE_PROXY_SYMBOL = Symbol("plasmic.state.proxy");
665
672
 
666
- function generateStateOnChangeProp($state, stateName, dataReps) {
667
- return function (val, path) { return set($state, __spreadArray(__spreadArray([stateName], __read(dataReps), false), __read(path), false), val); };
673
+ function generateStateOnChangeProp($state, path) {
674
+ return function (val) { return set($state, path, val); };
668
675
  }
669
- /**
670
- * This function generate the state value prop for repeated states
671
- * Example:
672
- * - parent[][].counter[].count
673
- * We need to pass `parent[index1][index2].counter to the child component
674
- */
675
- function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
676
- ) {
676
+ function generateStateValueProp($state, path) {
677
677
  return get($state, path);
678
678
  }
679
679
  var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
@@ -691,6 +691,12 @@ function shallowEqual(a1, a2) {
691
691
  }
692
692
  return true;
693
693
  }
694
+ /**
695
+ * Shallow comparison of arrays.
696
+ */
697
+ function arrayEq(xs, ys) {
698
+ return (xs.length === ys.length && xs.every(function (_, index) { return xs[index] === ys[index]; }));
699
+ }
694
700
  function isNum$1(value) {
695
701
  return typeof value === "symbol" ? false : !isNaN(+value);
696
702
  }
@@ -756,68 +762,83 @@ function assignValue(object, key, value) {
756
762
  var UNINITIALIZED = Symbol("plasmic.unitialized");
757
763
  var StateSpecNode = /** @class */ (function () {
758
764
  function StateSpecNode(specs) {
759
- this.specs = specs;
760
- this.edges = new Map();
761
- this.state = {};
765
+ this._specs = specs;
766
+ this._edges = new Map();
767
+ this._state = {};
762
768
  }
769
+ StateSpecNode.prototype.setSpecs = function (specs) {
770
+ this._specs = specs;
771
+ };
772
+ StateSpecNode.prototype.edges = function () {
773
+ return this._edges;
774
+ };
775
+ StateSpecNode.prototype.state = function () {
776
+ return this._state;
777
+ };
763
778
  StateSpecNode.prototype.hasEdge = function (key) {
764
- return this.edges.has(key);
779
+ return this._edges.has(key);
765
780
  };
766
781
  StateSpecNode.prototype.addEdge = function (key, node) {
767
- this.edges.set(key, node);
782
+ this._edges.set(key, node);
783
+ };
784
+ StateSpecNode.prototype.clearEdges = function () {
785
+ this._edges = new Map();
768
786
  };
769
787
  StateSpecNode.prototype.children = function () {
770
- return this.edges.values();
788
+ return this._edges.values();
771
789
  };
772
790
  StateSpecNode.prototype.makeTransition = function (key) {
773
791
  key = isNum$1(key) ? ARRAY_SYMBOL : key;
774
- return this.edges.get(key);
792
+ return this._edges.get(key);
775
793
  };
776
794
  StateSpecNode.prototype.isLeaf = function () {
777
- return this.edges.size === 0;
795
+ return this._edges.size === 0;
778
796
  };
779
797
  StateSpecNode.prototype.hasArrayTransition = function () {
780
- return this.edges.has(ARRAY_SYMBOL);
798
+ return this._edges.has(ARRAY_SYMBOL);
781
799
  };
782
800
  StateSpecNode.prototype.getSpec = function () {
783
- return this.specs[0];
801
+ return this._specs[0];
784
802
  };
785
803
  StateSpecNode.prototype.getAllSpecs = function () {
786
- return this.specs;
804
+ return this._specs;
787
805
  };
788
806
  StateSpecNode.prototype.getState = function (path) {
789
- return this.state[JSON.stringify(path)];
807
+ return this._state[JSON.stringify(path)];
808
+ };
809
+ StateSpecNode.prototype.getInitFunc = function (stateCell) {
810
+ var _a;
811
+ return (_a = stateCell.registeredInitFunc) !== null && _a !== void 0 ? _a : this.getSpec().initFunc;
790
812
  };
791
813
  StateSpecNode.prototype.clearStates = function () {
792
- this.state = {};
814
+ this._state = {};
793
815
  };
794
816
  StateSpecNode.prototype.states = function () {
795
- return Object.values(this.state);
817
+ return Object.values(this._state);
796
818
  };
797
819
  StateSpecNode.prototype.hasState = function (path) {
798
820
  var key = JSON.stringify(path);
799
- return key in this.state;
821
+ return key in this._state;
800
822
  };
801
823
  StateSpecNode.prototype.createStateCell = function (path) {
802
824
  var key = JSON.stringify(path);
803
- this.state[key] = {
825
+ this._state[key] = {
804
826
  listeners: [],
805
827
  initialValue: UNINITIALIZED,
806
- registeredInitFunc: this.getSpec().initFunc,
807
828
  path: path
808
829
  };
809
830
  };
810
831
  StateSpecNode.prototype.setInitialValue = function (path, value) {
811
832
  var key = JSON.stringify(path);
812
- this.state[key].initialValue = value;
833
+ this._state[key].initialValue = value;
813
834
  };
814
835
  StateSpecNode.prototype.getInitialValue = function (path) {
815
836
  var key = JSON.stringify(path);
816
- return this.state[key].initialValue;
837
+ return this._state[key].initialValue;
817
838
  };
818
839
  StateSpecNode.prototype.addListener = function (path, f) {
819
840
  var key = JSON.stringify(path);
820
- this.state[key].listeners.push(f);
841
+ this._state[key].listeners.push(f);
821
842
  };
822
843
  return StateSpecNode;
823
844
  }());
@@ -848,7 +869,31 @@ function buildTree(specs) {
848
869
  };
849
870
  return rec([]);
850
871
  }
851
- function getLeaves(root) {
872
+ function updateTree(root, specs) {
873
+ var internalSpec = specs.map(function (spec) {
874
+ return (__assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path.split(".").some(function (part) { return part.endsWith("[]"); }) }));
875
+ });
876
+ var rec = function (oldNode, currentPath) {
877
+ var nodeSpecs = internalSpec.filter(function (spec) {
878
+ return shallowEqual(currentPath, spec.pathObj.slice(0, currentPath.length));
879
+ });
880
+ var node = oldNode !== null && oldNode !== void 0 ? oldNode : new StateSpecNode(nodeSpecs);
881
+ node.setSpecs(nodeSpecs);
882
+ var oldEdges = oldNode === null || oldNode === void 0 ? void 0 : oldNode.edges();
883
+ node.clearEdges();
884
+ node.getAllSpecs().forEach(function (spec) {
885
+ if (spec.pathObj.length > currentPath.length) {
886
+ var nextKey = spec.pathObj[currentPath.length];
887
+ if (!node.hasEdge(nextKey)) {
888
+ node.addEdge(nextKey, rec(oldEdges === null || oldEdges === void 0 ? void 0 : oldEdges.get(nextKey), __spreadArray(__spreadArray([], __read(currentPath), false), [nextKey], false)));
889
+ }
890
+ }
891
+ });
892
+ return node;
893
+ };
894
+ return rec(root, []);
895
+ }
896
+ function getStateCells(root) {
852
897
  var leaves = [];
853
898
  var rec = function (node) {
854
899
  var e_1, _a;
@@ -865,7 +910,7 @@ function getLeaves(root) {
865
910
  }
866
911
  finally { if (e_1) throw e_1.error; }
867
912
  }
868
- if (node.isLeaf()) {
913
+ if (node.isLeaf() && node.getAllSpecs().length > 0) {
869
914
  leaves.push(node);
870
915
  }
871
916
  };
@@ -884,7 +929,6 @@ function findStateCell(root, pathStr, repetitionIndex) {
884
929
  if (!root.hasArrayTransition() ||
885
930
  !repetitionIndex ||
886
931
  currRepIndex > repetitionIndex.length) {
887
- console.log(root);
888
932
  throw new Error("transition not found: pathStr ".concat(pathStr, " part ").concat(typeof part === "symbol" ? "[]" : part));
889
933
  }
890
934
  realPath.push(repetitionIndex[currRepIndex++]);
@@ -944,8 +988,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
944
988
  set(proxyRoot, initialStatePath, newValue);
945
989
  });
946
990
  });
947
- var initialValue = initialSpecNode.getState(initialStatePath)
948
- .registeredInitFunc($$state.props, $state, $$state.ctx);
991
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
949
992
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
950
993
  var initialSpec = initialSpecNode.getSpec();
951
994
  var value = initialSpec.isImmutable
@@ -959,7 +1002,8 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
959
1002
  return initialValue;
960
1003
  }
961
1004
  function create$StateProxy($$state, leafHandlers) {
962
- var rec = function (currPath, currNode, isOutside, proxyRoot, initialObject) {
1005
+ var proxyRoot;
1006
+ var rec = function (currPath, currNode, isOutside, initialObject) {
963
1007
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
964
1008
  isNum(property) ? +property : property,
965
1009
  ], false); };
@@ -978,7 +1022,7 @@ function create$StateProxy($$state, leafHandlers) {
978
1022
  //we are always in a leaf, since we only have two cases:
979
1023
  // 1 - delete properties outside the state tree
980
1024
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
981
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get($$state.stateValues, currPath.slice(spec.pathObj.length)));
1025
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
982
1026
  }
983
1027
  return Reflect.deleteProperty(target, property);
984
1028
  },
@@ -987,23 +1031,21 @@ function create$StateProxy($$state, leafHandlers) {
987
1031
  if (property === PLASMIC_STATE_PROXY_SYMBOL) {
988
1032
  return true;
989
1033
  }
990
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
991
1034
  var nextPath = getNextPath(property);
992
1035
  if (isOutside || currNode.isLeaf()) {
993
1036
  return Reflect.get(target, property, receiver);
994
1037
  }
995
1038
  var nextNode = currNode.makeTransition(property);
996
1039
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
997
- return (_b = (_a = leafHandlers(nextNode, nextPath, proxyRoot)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1040
+ return (_b = (_a = leafHandlers(nextNode, nextPath)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
998
1041
  }
999
1042
  else if (nextNode && !(property in target)) {
1000
- target[property] = rec(nextPath, nextNode, false, proxyRoot, undefined);
1043
+ target[property] = rec(nextPath, nextNode, false, undefined);
1001
1044
  }
1002
1045
  return Reflect.get(target, property, receiver);
1003
1046
  },
1004
1047
  set: function (target, property, value, receiver) {
1005
1048
  var _a, _b;
1006
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1007
1049
  var nextPath = getNextPath(property);
1008
1050
  var nextNode = currNode.makeTransition(property);
1009
1051
  if (property === "registerInitFunc" && currPath.length === 0) {
@@ -1015,7 +1057,7 @@ function create$StateProxy($$state, leafHandlers) {
1015
1057
  return Reflect.set(target, property, value, receiver);
1016
1058
  }
1017
1059
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1018
- (_b = (_a = leafHandlers(nextNode, nextPath, proxyRoot)).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1060
+ (_b = (_a = leafHandlers(nextNode, nextPath)).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1019
1061
  }
1020
1062
  if (!isOutside && !currNode.isLeaf() && !nextNode) {
1021
1063
  // can't set an unknown field in $state
@@ -1027,7 +1069,7 @@ function create$StateProxy($$state, leafHandlers) {
1027
1069
  nextNode = currNode;
1028
1070
  }
1029
1071
  if (canProxy(value)) {
1030
- target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), proxyRoot, value);
1072
+ target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
1031
1073
  }
1032
1074
  else if (!isOutside && !currNode.isLeaf() && !(nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf())) {
1033
1075
  throw new Error("inserting a primitive value into a non-leaf");
@@ -1054,8 +1096,11 @@ function create$StateProxy($$state, leafHandlers) {
1054
1096
  : {}
1055
1097
  : Array.isArray(initialObject)
1056
1098
  ? []
1057
- : Object.create(Object.getPrototypeOf(initialObject));
1099
+ : Object.create(Object.getPrototypeOf(initialObject !== null && initialObject !== void 0 ? initialObject : {}));
1058
1100
  var proxyObj = new Proxy(baseObject, handlers);
1101
+ if (currPath.length === 0) {
1102
+ proxyRoot = proxyObj;
1103
+ }
1059
1104
  if (initialObject) {
1060
1105
  Reflect.ownKeys(initialObject).forEach(function (key) {
1061
1106
  var desc = Object.getOwnPropertyDescriptor(initialObject, key);
@@ -1069,35 +1114,37 @@ function create$StateProxy($$state, leafHandlers) {
1069
1114
  }
1070
1115
  return proxyObj;
1071
1116
  };
1072
- return rec([], $$state.rootSpecTree, false, undefined, undefined);
1117
+ return rec([], $$state.rootSpecTree, false, undefined);
1073
1118
  }
1074
1119
  var mkUntrackedValue = function (o) {
1075
1120
  return o != null && typeof o === "object" ? ref(o) : o;
1076
1121
  };
1077
- function useDollarState(specs, props, $ctx) {
1122
+ function useDollarState(specs, props, $ctx, opts) {
1078
1123
  var $$state = React__default.useRef((function () {
1079
1124
  var rootSpecTree = buildTree(specs);
1080
1125
  return {
1081
1126
  rootSpecTree: rootSpecTree,
1082
- specTreeLeaves: getLeaves(rootSpecTree),
1127
+ specTreeLeaves: getStateCells(rootSpecTree),
1083
1128
  stateValues: proxy({}),
1084
1129
  props: {},
1085
1130
  ctx: {},
1086
- registrationsQueue: []
1131
+ specs: [],
1132
+ registrationsQueue: proxy([])
1087
1133
  };
1088
1134
  })()).current;
1089
1135
  $$state.props = props;
1090
1136
  $$state.ctx = $ctx !== null && $ctx !== void 0 ? $ctx : {};
1091
- var $state = React__default.useRef((function () {
1092
- var useRef$state = Object.assign(create$StateProxy($$state, function (node, path, proxyRoot) {
1137
+ $$state.specs = specs;
1138
+ var create$State = function () {
1139
+ var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1093
1140
  if (!node.hasState(path)) {
1094
1141
  node.createStateCell(path);
1095
1142
  var spec = node.getSpec();
1096
1143
  if (spec.initFunc) {
1097
- initializeStateValue($$state, node, path, proxyRoot);
1144
+ initializeStateValue($$state, node, path, $state);
1098
1145
  }
1099
1146
  else if (!spec.valueProp) {
1100
- set(proxyRoot, path, spec.initVal);
1147
+ set($state, path, spec.initVal);
1101
1148
  }
1102
1149
  }
1103
1150
  return {
@@ -1117,22 +1164,50 @@ function useDollarState(specs, props, $ctx) {
1117
1164
  if (!node.hasState(realPath)) {
1118
1165
  node.createStateCell(realPath);
1119
1166
  }
1120
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, useRef$state, $$state.ctx))) {
1121
- $$state.registrationsQueue.push({ node: node, path: realPath, f: f });
1167
+ if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1168
+ $$state.registrationsQueue.push(mkUntrackedValue({ node: node, path: realPath, f: f }));
1122
1169
  }
1123
1170
  }
1124
1171
  });
1125
- return useRef$state;
1126
- })()).current;
1172
+ return $state;
1173
+ };
1174
+ var ref = React__default.useRef(undefined);
1175
+ if (!ref.current) {
1176
+ ref.current = create$State();
1177
+ }
1178
+ var $state = ref.current;
1179
+ if (opts === null || opts === void 0 ? void 0 : opts.inCanvas) {
1180
+ $$state.rootSpecTree = updateTree($$state.rootSpecTree, specs);
1181
+ var newLeaves = getStateCells($$state.rootSpecTree);
1182
+ if (!arrayEq(newLeaves, $$state.specTreeLeaves)) {
1183
+ $state = ref.current = create$State();
1184
+ $$state.specTreeLeaves = newLeaves;
1185
+ }
1186
+ // we need to eager initialize all states in canvas to populate the data picker
1187
+ $$state.specTreeLeaves.forEach(function (node) {
1188
+ var spec = node.getSpec();
1189
+ if (spec.isRepeated || node.hasState(spec.pathObj)) {
1190
+ return;
1191
+ }
1192
+ node.createStateCell(spec.pathObj);
1193
+ var init = spec.valueProp
1194
+ ? $$state.props[spec.valueProp]
1195
+ : spec.initFunc
1196
+ ? initializeStateValue($$state, node, spec.pathObj, $state)
1197
+ : spec.initVal;
1198
+ set($state, spec.pathObj, init);
1199
+ });
1200
+ }
1127
1201
  // For each spec with an initFunc, evaluate it and see if
1128
1202
  // the init value has changed. If so, reset its state.
1129
1203
  var resetSpecs = [];
1130
1204
  $$state.specTreeLeaves
1131
1205
  .flatMap(function (node) { return node.states().map(function (stateCell) { return ({ stateCell: stateCell, node: node }); }); })
1132
1206
  .forEach(function (_a) {
1133
- var stateCell = _a.stateCell, node = _a.node;
1134
- if (stateCell.registeredInitFunc) {
1135
- var newInit = stateCell.registeredInitFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1207
+ var node = _a.node, stateCell = _a.stateCell;
1208
+ var initFunc = node.getInitFunc(stateCell);
1209
+ if (initFunc) {
1210
+ var newInit = initFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1136
1211
  if (!deepEqual(newInit, stateCell.initialValue)) {
1137
1212
  resetSpecs.push({ stateCell: stateCell, node: node });
1138
1213
  }
@@ -1153,14 +1228,13 @@ function useDollarState(specs, props, $ctx) {
1153
1228
  });
1154
1229
  }, [props, resetSpecs]);
1155
1230
  useIsomorphicLayoutEffect(function () {
1156
- $$state.registrationsQueue.forEach(function (_a) {
1157
- var node = _a.node, path = _a.path, f = _a.f;
1231
+ while ($$state.registrationsQueue.length) {
1232
+ var _a = $$state.registrationsQueue.shift(), node = _a.node, path = _a.path, f = _a.f;
1158
1233
  var stateCell = node.getState(path);
1159
1234
  stateCell.registeredInitFunc = f;
1160
1235
  reInitializeState(node, stateCell);
1161
- });
1162
- $$state.registrationsQueue = [];
1163
- }, [$$state.registrationsQueue]);
1236
+ }
1237
+ }, [$$state.registrationsQueue.length]);
1164
1238
  // immediately initialize exposed non-private states
1165
1239
  useIsomorphicLayoutEffect(function () {
1166
1240
  $$state.specTreeLeaves.forEach(function (node) {
@@ -1173,6 +1247,7 @@ function useDollarState(specs, props, $ctx) {
1173
1247
  }, []);
1174
1248
  // Re-render if any value changed in one of these objects
1175
1249
  useSnapshot($$state.stateValues, { sync: true });
1250
+ useSnapshot($$state.registrationsQueue);
1176
1251
  return $state;
1177
1252
  }
1178
1253