@plasmicapp/react-web 0.2.140 → 0.2.142

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.
Files changed (39) hide show
  1. package/dist/all.d.ts +29 -30
  2. package/dist/auth/PlasmicPageGuard.d.ts +6 -4
  3. package/dist/react-web.cjs.development.js +952 -114
  4. package/dist/react-web.cjs.development.js.map +1 -1
  5. package/dist/react-web.cjs.production.min.js +1 -1
  6. package/dist/react-web.cjs.production.min.js.map +1 -1
  7. package/dist/react-web.esm.js +952 -114
  8. package/dist/react-web.esm.js.map +1 -1
  9. package/dist/states/graph.d.ts +10 -4
  10. package/dist/states/helpers.d.ts +7 -9
  11. package/dist/states/valtio.d.ts +3 -1
  12. package/dist/stories/UseDollarState.stories.d.ts +7 -0
  13. package/package.json +2 -2
  14. package/skinny/dist/auth/PlasmicPageGuard.d.ts +6 -4
  15. package/skinny/dist/{collection-utils-43f97262.js → collection-utils-b5d4be02.js} +4 -4
  16. package/skinny/dist/{collection-utils-43f97262.js.map → collection-utils-b5d4be02.js.map} +1 -1
  17. package/skinny/dist/{common-9ebe374e.js → common-c2744215.js} +40 -2
  18. package/skinny/dist/{common-9ebe374e.js.map → common-c2744215.js.map} +1 -1
  19. package/skinny/dist/index.js +219 -90
  20. package/skinny/dist/index.js.map +1 -1
  21. package/skinny/dist/plume/button/index.js +2 -2
  22. package/skinny/dist/plume/checkbox/index.js +4 -4
  23. package/skinny/dist/plume/menu/index.js +5 -5
  24. package/skinny/dist/plume/menu-button/index.js +4 -4
  25. package/skinny/dist/plume/select/index.js +5 -5
  26. package/skinny/dist/plume/switch/index.js +4 -4
  27. package/skinny/dist/plume/text-input/index.js +2 -2
  28. package/skinny/dist/plume/triggered-overlay/index.js +4 -4
  29. package/skinny/dist/{plume-utils-d9811bf3.js → plume-utils-653f4b8a.js} +2 -2
  30. package/skinny/dist/{plume-utils-d9811bf3.js.map → plume-utils-653f4b8a.js.map} +1 -1
  31. package/skinny/dist/{props-utils-228208e6.js → props-utils-70c2d02a.js} +2 -2
  32. package/skinny/dist/{props-utils-228208e6.js.map → props-utils-70c2d02a.js.map} +1 -1
  33. package/skinny/dist/{react-utils-7bc53870.js → react-utils-76d05959.js} +2 -2
  34. package/skinny/dist/{react-utils-7bc53870.js.map → react-utils-76d05959.js.map} +1 -1
  35. package/skinny/dist/render/PlasmicImg/index.js +2 -2
  36. package/skinny/dist/states/graph.d.ts +10 -4
  37. package/skinny/dist/states/helpers.d.ts +7 -9
  38. package/skinny/dist/states/valtio.d.ts +3 -1
  39. package/skinny/dist/stories/UseDollarState.stories.d.ts +7 -0
@@ -1,11 +1,13 @@
1
1
  import classNames$1 from 'classnames';
2
+ import { usePlasmicDataSourceContext } from '@plasmicapp/data-sources-context';
3
+ export { PlasmicDataSourceContextProvider, useCurrentUser } from '@plasmicapp/data-sources-context';
2
4
  import * as React from 'react';
3
5
  import React__default, { useLayoutEffect, useEffect } from 'react';
4
6
  import get from 'dlv';
5
7
  export { default as get } from 'dlv';
6
- import { c as createElementWithChildren, m as mergeProps, e as ensureNotArray, N as NONE, a as mergePropVals, i as isReactNode, b as isBrowser, u as useIsomorphicLayoutEffect$1 } from './react-utils-7bc53870.js';
7
- import { _ as __rest, a as __assign, b as __spreadArray, c as __read, o as omit, p as pick, d as __values, i as isSubset, e as chainSingleArgFuncs, n as notNil } from './common-9ebe374e.js';
8
- export { o as omit, p as pick } from './common-9ebe374e.js';
8
+ import { c as createElementWithChildren, m as mergeProps, e as ensureNotArray, N as NONE, a as mergePropVals, i as isReactNode, b as isBrowser, u as useIsomorphicLayoutEffect$1 } from './react-utils-76d05959.js';
9
+ import { _ as __awaiter, a as __generator, b as __rest, c as __assign, d as __spreadArray, e as __read, o as omit, p as pick, f as __values, i as isSubset, g as chainSingleArgFuncs, n as notNil } from './common-c2744215.js';
10
+ export { o as omit, p as pick } from './common-c2744215.js';
9
11
  export { PlasmicHead, plasmicHeadMeta } from './render/PlasmicHead/index.js';
10
12
  export { PlasmicImg } from './render/PlasmicImg/index.js';
11
13
  import { T as Trans } from './ssr-f6caec06.js';
@@ -15,14 +17,65 @@ import { useFocusRing } from '@react-aria/focus';
15
17
  import clone from 'clone';
16
18
  import deepEqual from 'fast-deep-equal';
17
19
  import { proxy, useSnapshot, ref } from 'valtio';
18
- export { PlasmicDataSourceContextProvider, useCurrentUser } from '@plasmicapp/data-sources-context';
19
20
  import '@react-aria/ssr';
20
21
 
22
+ function triggerLogin(appId, authorizeEndpoint) {
23
+ return __awaiter(this, void 0, void 0, function () {
24
+ function sha256(text) {
25
+ return __awaiter(this, void 0, void 0, function () {
26
+ var encoder, data, hashBuffer, hashArray, hashHex;
27
+ return __generator(this, function (_a) {
28
+ switch (_a.label) {
29
+ case 0:
30
+ encoder = new TextEncoder();
31
+ data = encoder.encode(text);
32
+ return [4 /*yield*/, crypto.subtle.digest("SHA-256", data)];
33
+ case 1:
34
+ hashBuffer = _a.sent();
35
+ hashArray = Array.from(new Uint8Array(hashBuffer));
36
+ hashHex = hashArray
37
+ .map(function (b) { return b.toString(16).padStart(2, "0"); })
38
+ .join("");
39
+ return [2 /*return*/, hashHex];
40
+ }
41
+ });
42
+ });
43
+ }
44
+ var continueTo, state, code_verifier, code_challenge, params, url;
45
+ return __generator(this, function (_a) {
46
+ switch (_a.label) {
47
+ case 0:
48
+ continueTo = window.location.href;
49
+ state = JSON.stringify({ continueTo: continueTo });
50
+ code_verifier = crypto.randomUUID();
51
+ localStorage.setItem("code_verifier", code_verifier);
52
+ return [4 /*yield*/, sha256(code_verifier)];
53
+ case 1:
54
+ code_challenge = _a.sent();
55
+ params = new URLSearchParams();
56
+ params.set("client_id", appId);
57
+ params.set("state", state);
58
+ params.set("response_type", "code");
59
+ params.set("code_challenge", code_challenge);
60
+ params.set("code_challenge_method", "S256");
61
+ url = "".concat(authorizeEndpoint, "?").concat(params.toString());
62
+ window.location.href = url;
63
+ return [2 /*return*/];
64
+ }
65
+ });
66
+ });
67
+ }
21
68
  function PlasmicPageGuard(props) {
22
- var children = props.children, dataSourceCtxValue = props.dataSourceCtxValue, validRoles = props.validRoles;
23
- if (!dataSourceCtxValue || dataSourceCtxValue.isUserLoading) {
24
- return null;
25
- }
69
+ var appId = props.appId, authorizeEndpoint = props.authorizeEndpoint, validRoles = props.validRoles, children = props.children;
70
+ var dataSourceCtxValue = usePlasmicDataSourceContext();
71
+ React__default.useEffect(function () {
72
+ if (dataSourceCtxValue &&
73
+ "isUserLoading" in dataSourceCtxValue &&
74
+ !dataSourceCtxValue.isUserLoading &&
75
+ !dataSourceCtxValue.user) {
76
+ triggerLogin(appId, authorizeEndpoint);
77
+ }
78
+ }, [dataSourceCtxValue, appId, authorizeEndpoint]);
26
79
  function canUserViewPage() {
27
80
  if (!dataSourceCtxValue) {
28
81
  return false;
@@ -35,6 +88,11 @@ function PlasmicPageGuard(props) {
35
88
  }
36
89
  return validRoles.includes(dataSourceCtxValue.user.roleId);
37
90
  }
91
+ if (!dataSourceCtxValue ||
92
+ dataSourceCtxValue.isUserLoading ||
93
+ !dataSourceCtxValue.user) {
94
+ return null;
95
+ }
38
96
  if (!canUserViewPage()) {
39
97
  return React__default.createElement("div", null, "You don't have access to this page");
40
98
  }
@@ -670,17 +728,10 @@ function useTrigger(trigger, opts) {
670
728
  var ARRAY_SYMBOL = Symbol("[]");
671
729
  var PLASMIC_STATE_PROXY_SYMBOL = Symbol("plasmic.state.proxy");
672
730
 
673
- function generateStateOnChangeProp($state, stateName, dataReps) {
674
- return function (val, path) { return set($state, __spreadArray(__spreadArray([stateName], __read(dataReps), false), __read(path), false), val); };
731
+ function generateStateOnChangeProp($state, path) {
732
+ return function (val) { return set($state, path, val); };
675
733
  }
676
- /**
677
- * This function generate the state value prop for repeated states
678
- * Example:
679
- * - parent[][].counter[].count
680
- * We need to pass `parent[index1][index2].counter to the child component
681
- */
682
- function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
683
- ) {
734
+ function generateStateValueProp($state, path) {
684
735
  return get($state, path);
685
736
  }
686
737
  var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
@@ -698,6 +749,12 @@ function shallowEqual(a1, a2) {
698
749
  }
699
750
  return true;
700
751
  }
752
+ /**
753
+ * Shallow comparison of arrays.
754
+ */
755
+ function arrayEq(xs, ys) {
756
+ return (xs.length === ys.length && xs.every(function (_, index) { return xs[index] === ys[index]; }));
757
+ }
701
758
  function isNum$1(value) {
702
759
  return typeof value === "symbol" ? false : !isNaN(+value);
703
760
  }
@@ -763,68 +820,83 @@ function assignValue(object, key, value) {
763
820
  var UNINITIALIZED = Symbol("plasmic.unitialized");
764
821
  var StateSpecNode = /** @class */ (function () {
765
822
  function StateSpecNode(specs) {
766
- this.specs = specs;
767
- this.edges = new Map();
768
- this.state = {};
823
+ this._specs = specs;
824
+ this._edges = new Map();
825
+ this._state = {};
769
826
  }
827
+ StateSpecNode.prototype.setSpecs = function (specs) {
828
+ this._specs = specs;
829
+ };
830
+ StateSpecNode.prototype.edges = function () {
831
+ return this._edges;
832
+ };
833
+ StateSpecNode.prototype.state = function () {
834
+ return this._state;
835
+ };
770
836
  StateSpecNode.prototype.hasEdge = function (key) {
771
- return this.edges.has(key);
837
+ return this._edges.has(key);
772
838
  };
773
839
  StateSpecNode.prototype.addEdge = function (key, node) {
774
- this.edges.set(key, node);
840
+ this._edges.set(key, node);
841
+ };
842
+ StateSpecNode.prototype.clearEdges = function () {
843
+ this._edges = new Map();
775
844
  };
776
845
  StateSpecNode.prototype.children = function () {
777
- return this.edges.values();
846
+ return this._edges.values();
778
847
  };
779
848
  StateSpecNode.prototype.makeTransition = function (key) {
780
849
  key = isNum$1(key) ? ARRAY_SYMBOL : key;
781
- return this.edges.get(key);
850
+ return this._edges.get(key);
782
851
  };
783
852
  StateSpecNode.prototype.isLeaf = function () {
784
- return this.edges.size === 0;
853
+ return this._edges.size === 0;
785
854
  };
786
855
  StateSpecNode.prototype.hasArrayTransition = function () {
787
- return this.edges.has(ARRAY_SYMBOL);
856
+ return this._edges.has(ARRAY_SYMBOL);
788
857
  };
789
858
  StateSpecNode.prototype.getSpec = function () {
790
- return this.specs[0];
859
+ return this._specs[0];
791
860
  };
792
861
  StateSpecNode.prototype.getAllSpecs = function () {
793
- return this.specs;
862
+ return this._specs;
794
863
  };
795
864
  StateSpecNode.prototype.getState = function (path) {
796
- return this.state[JSON.stringify(path)];
865
+ return this._state[JSON.stringify(path)];
866
+ };
867
+ StateSpecNode.prototype.getInitFunc = function (stateCell) {
868
+ var _a;
869
+ return (_a = stateCell.registeredInitFunc) !== null && _a !== void 0 ? _a : this.getSpec().initFunc;
797
870
  };
798
871
  StateSpecNode.prototype.clearStates = function () {
799
- this.state = {};
872
+ this._state = {};
800
873
  };
801
874
  StateSpecNode.prototype.states = function () {
802
- return Object.values(this.state);
875
+ return Object.values(this._state);
803
876
  };
804
877
  StateSpecNode.prototype.hasState = function (path) {
805
878
  var key = JSON.stringify(path);
806
- return key in this.state;
879
+ return key in this._state;
807
880
  };
808
881
  StateSpecNode.prototype.createStateCell = function (path) {
809
882
  var key = JSON.stringify(path);
810
- this.state[key] = {
883
+ this._state[key] = {
811
884
  listeners: [],
812
885
  initialValue: UNINITIALIZED,
813
- registeredInitFunc: this.getSpec().initFunc,
814
886
  path: path
815
887
  };
816
888
  };
817
889
  StateSpecNode.prototype.setInitialValue = function (path, value) {
818
890
  var key = JSON.stringify(path);
819
- this.state[key].initialValue = value;
891
+ this._state[key].initialValue = value;
820
892
  };
821
893
  StateSpecNode.prototype.getInitialValue = function (path) {
822
894
  var key = JSON.stringify(path);
823
- return this.state[key].initialValue;
895
+ return this._state[key].initialValue;
824
896
  };
825
897
  StateSpecNode.prototype.addListener = function (path, f) {
826
898
  var key = JSON.stringify(path);
827
- this.state[key].listeners.push(f);
899
+ this._state[key].listeners.push(f);
828
900
  };
829
901
  return StateSpecNode;
830
902
  }());
@@ -855,7 +927,31 @@ function buildTree(specs) {
855
927
  };
856
928
  return rec([]);
857
929
  }
858
- function getLeaves(root) {
930
+ function updateTree(root, specs) {
931
+ var internalSpec = specs.map(function (spec) {
932
+ return (__assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path.split(".").some(function (part) { return part.endsWith("[]"); }) }));
933
+ });
934
+ var rec = function (oldNode, currentPath) {
935
+ var nodeSpecs = internalSpec.filter(function (spec) {
936
+ return shallowEqual(currentPath, spec.pathObj.slice(0, currentPath.length));
937
+ });
938
+ var node = oldNode !== null && oldNode !== void 0 ? oldNode : new StateSpecNode(nodeSpecs);
939
+ node.setSpecs(nodeSpecs);
940
+ var oldEdges = oldNode === null || oldNode === void 0 ? void 0 : oldNode.edges();
941
+ node.clearEdges();
942
+ node.getAllSpecs().forEach(function (spec) {
943
+ if (spec.pathObj.length > currentPath.length) {
944
+ var nextKey = spec.pathObj[currentPath.length];
945
+ if (!node.hasEdge(nextKey)) {
946
+ node.addEdge(nextKey, rec(oldEdges === null || oldEdges === void 0 ? void 0 : oldEdges.get(nextKey), __spreadArray(__spreadArray([], __read(currentPath), false), [nextKey], false)));
947
+ }
948
+ }
949
+ });
950
+ return node;
951
+ };
952
+ return rec(root, []);
953
+ }
954
+ function getStateCells(root) {
859
955
  var leaves = [];
860
956
  var rec = function (node) {
861
957
  var e_1, _a;
@@ -872,7 +968,7 @@ function getLeaves(root) {
872
968
  }
873
969
  finally { if (e_1) throw e_1.error; }
874
970
  }
875
- if (node.isLeaf()) {
971
+ if (node.isLeaf() && node.getAllSpecs().length > 0) {
876
972
  leaves.push(node);
877
973
  }
878
974
  };
@@ -891,7 +987,6 @@ function findStateCell(root, pathStr, repetitionIndex) {
891
987
  if (!root.hasArrayTransition() ||
892
988
  !repetitionIndex ||
893
989
  currRepIndex > repetitionIndex.length) {
894
- console.log(root);
895
990
  throw new Error("transition not found: pathStr ".concat(pathStr, " part ").concat(typeof part === "symbol" ? "[]" : part));
896
991
  }
897
992
  realPath.push(repetitionIndex[currRepIndex++]);
@@ -951,8 +1046,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
951
1046
  set(proxyRoot, initialStatePath, newValue);
952
1047
  });
953
1048
  });
954
- var initialValue = initialSpecNode.getState(initialStatePath)
955
- .registeredInitFunc($$state.props, $state, $$state.ctx);
1049
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
956
1050
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
957
1051
  var initialSpec = initialSpecNode.getSpec();
958
1052
  var value = initialSpec.isImmutable
@@ -966,7 +1060,8 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
966
1060
  return initialValue;
967
1061
  }
968
1062
  function create$StateProxy($$state, leafHandlers) {
969
- var rec = function (currPath, currNode, isOutside, proxyRoot, initialObject) {
1063
+ var proxyRoot;
1064
+ var rec = function (currPath, currNode, isOutside, initialObject) {
970
1065
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
971
1066
  isNum(property) ? +property : property,
972
1067
  ], false); };
@@ -985,7 +1080,7 @@ function create$StateProxy($$state, leafHandlers) {
985
1080
  //we are always in a leaf, since we only have two cases:
986
1081
  // 1 - delete properties outside the state tree
987
1082
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
988
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get($$state.stateValues, currPath.slice(spec.pathObj.length)));
1083
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
989
1084
  }
990
1085
  return Reflect.deleteProperty(target, property);
991
1086
  },
@@ -994,23 +1089,21 @@ function create$StateProxy($$state, leafHandlers) {
994
1089
  if (property === PLASMIC_STATE_PROXY_SYMBOL) {
995
1090
  return true;
996
1091
  }
997
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
998
1092
  var nextPath = getNextPath(property);
999
1093
  if (isOutside || currNode.isLeaf()) {
1000
1094
  return Reflect.get(target, property, receiver);
1001
1095
  }
1002
1096
  var nextNode = currNode.makeTransition(property);
1003
1097
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1004
- return (_b = (_a = leafHandlers(nextNode, nextPath, proxyRoot)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1098
+ return (_b = (_a = leafHandlers(nextNode, nextPath)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1005
1099
  }
1006
1100
  else if (nextNode && !(property in target)) {
1007
- target[property] = rec(nextPath, nextNode, false, proxyRoot, undefined);
1101
+ target[property] = rec(nextPath, nextNode, false, undefined);
1008
1102
  }
1009
1103
  return Reflect.get(target, property, receiver);
1010
1104
  },
1011
1105
  set: function (target, property, value, receiver) {
1012
1106
  var _a, _b;
1013
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1014
1107
  var nextPath = getNextPath(property);
1015
1108
  var nextNode = currNode.makeTransition(property);
1016
1109
  if (property === "registerInitFunc" && currPath.length === 0) {
@@ -1022,7 +1115,7 @@ function create$StateProxy($$state, leafHandlers) {
1022
1115
  return Reflect.set(target, property, value, receiver);
1023
1116
  }
1024
1117
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1025
- (_b = (_a = leafHandlers(nextNode, nextPath, proxyRoot)).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1118
+ (_b = (_a = leafHandlers(nextNode, nextPath)).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1026
1119
  }
1027
1120
  if (!isOutside && !currNode.isLeaf() && !nextNode) {
1028
1121
  // can't set an unknown field in $state
@@ -1034,7 +1127,7 @@ function create$StateProxy($$state, leafHandlers) {
1034
1127
  nextNode = currNode;
1035
1128
  }
1036
1129
  if (canProxy(value)) {
1037
- target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), proxyRoot, value);
1130
+ target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
1038
1131
  }
1039
1132
  else if (!isOutside && !currNode.isLeaf() && !(nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf())) {
1040
1133
  throw new Error("inserting a primitive value into a non-leaf");
@@ -1063,6 +1156,9 @@ function create$StateProxy($$state, leafHandlers) {
1063
1156
  ? []
1064
1157
  : Object.create(Object.getPrototypeOf(initialObject !== null && initialObject !== void 0 ? initialObject : {}));
1065
1158
  var proxyObj = new Proxy(baseObject, handlers);
1159
+ if (currPath.length === 0) {
1160
+ proxyRoot = proxyObj;
1161
+ }
1066
1162
  if (initialObject) {
1067
1163
  Reflect.ownKeys(initialObject).forEach(function (key) {
1068
1164
  var desc = Object.getOwnPropertyDescriptor(initialObject, key);
@@ -1076,67 +1172,100 @@ function create$StateProxy($$state, leafHandlers) {
1076
1172
  }
1077
1173
  return proxyObj;
1078
1174
  };
1079
- return rec([], $$state.rootSpecTree, false, undefined, undefined);
1175
+ return rec([], $$state.rootSpecTree, false, undefined);
1080
1176
  }
1081
1177
  var mkUntrackedValue = function (o) {
1082
1178
  return o != null && typeof o === "object" ? ref(o) : o;
1083
1179
  };
1084
- function useDollarState(specs, props, $ctx) {
1180
+ function useDollarState(specs, props, $ctx, opts) {
1085
1181
  var $$state = React__default.useRef((function () {
1086
1182
  var rootSpecTree = buildTree(specs);
1087
1183
  return {
1088
1184
  rootSpecTree: rootSpecTree,
1089
- specTreeLeaves: getLeaves(rootSpecTree),
1185
+ specTreeLeaves: getStateCells(rootSpecTree),
1090
1186
  stateValues: proxy({}),
1091
1187
  props: {},
1092
1188
  ctx: {},
1093
- registrationsQueue: []
1189
+ specs: [],
1190
+ registrationsQueue: proxy([])
1094
1191
  };
1095
1192
  })()).current;
1096
1193
  $$state.props = props;
1097
1194
  $$state.ctx = $ctx !== null && $ctx !== void 0 ? $ctx : {};
1098
- var $state = React__default.useRef(Object.assign(create$StateProxy($$state, function (node, path, proxyRoot) {
1099
- if (!node.hasState(path)) {
1100
- node.createStateCell(path);
1101
- var spec = node.getSpec();
1102
- if (spec.initFunc) {
1103
- initializeStateValue($$state, node, path, proxyRoot);
1104
- }
1105
- else if (!spec.valueProp) {
1106
- set(proxyRoot, path, spec.initVal);
1107
- }
1108
- }
1109
- return {
1110
- get: function (target, property, receiver) {
1195
+ $$state.specs = specs;
1196
+ var create$State = function () {
1197
+ var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1198
+ if (!node.hasState(path)) {
1199
+ node.createStateCell(path);
1111
1200
  var spec = node.getSpec();
1112
- if (spec.valueProp) {
1113
- return $$state.props[spec.valueProp];
1201
+ if (spec.initFunc) {
1202
+ initializeStateValue($$state, node, path, $state);
1114
1203
  }
1115
- else {
1116
- return Reflect.get(target, property, receiver);
1204
+ else if (!spec.valueProp) {
1205
+ set($state, path, spec.initVal);
1117
1206
  }
1118
1207
  }
1119
- };
1120
- }), {
1121
- registerInitFunc: function (pathStr, f, repetitionIndex) {
1122
- var _a = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex), node = _a.node, realPath = _a.realPath;
1123
- if (!node.hasState(realPath)) {
1124
- node.createStateCell(realPath);
1125
- }
1126
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1127
- $$state.registrationsQueue.push({ node: node, path: realPath, f: f });
1208
+ return {
1209
+ get: function (target, property, receiver) {
1210
+ var spec = node.getSpec();
1211
+ if (spec.valueProp) {
1212
+ return $$state.props[spec.valueProp];
1213
+ }
1214
+ else {
1215
+ return Reflect.get(target, property, receiver);
1216
+ }
1217
+ }
1218
+ };
1219
+ }), {
1220
+ registerInitFunc: function (pathStr, f, repetitionIndex) {
1221
+ var _a = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex), node = _a.node, realPath = _a.realPath;
1222
+ if (!node.hasState(realPath)) {
1223
+ node.createStateCell(realPath);
1224
+ }
1225
+ if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1226
+ $$state.registrationsQueue.push(mkUntrackedValue({ node: node, path: realPath, f: f }));
1227
+ }
1128
1228
  }
1229
+ });
1230
+ return $state;
1231
+ };
1232
+ var ref = React__default.useRef(undefined);
1233
+ if (!ref.current) {
1234
+ ref.current = create$State();
1235
+ }
1236
+ var $state = ref.current;
1237
+ if (opts === null || opts === void 0 ? void 0 : opts.inCanvas) {
1238
+ $$state.rootSpecTree = updateTree($$state.rootSpecTree, specs);
1239
+ var newLeaves = getStateCells($$state.rootSpecTree);
1240
+ if (!arrayEq(newLeaves, $$state.specTreeLeaves)) {
1241
+ $state = ref.current = create$State();
1242
+ $$state.specTreeLeaves = newLeaves;
1129
1243
  }
1130
- })).current;
1244
+ // we need to eager initialize all states in canvas to populate the data picker
1245
+ $$state.specTreeLeaves.forEach(function (node) {
1246
+ var spec = node.getSpec();
1247
+ if (spec.isRepeated || node.hasState(spec.pathObj)) {
1248
+ return;
1249
+ }
1250
+ node.createStateCell(spec.pathObj);
1251
+ var init = spec.valueProp
1252
+ ? $$state.props[spec.valueProp]
1253
+ : spec.initFunc
1254
+ ? initializeStateValue($$state, node, spec.pathObj, $state)
1255
+ : spec.initVal;
1256
+ set($state, spec.pathObj, init);
1257
+ });
1258
+ }
1131
1259
  // For each spec with an initFunc, evaluate it and see if
1132
1260
  // the init value has changed. If so, reset its state.
1133
1261
  var resetSpecs = [];
1134
1262
  $$state.specTreeLeaves
1135
1263
  .flatMap(function (node) { return node.states().map(function (stateCell) { return ({ stateCell: stateCell, node: node }); }); })
1136
1264
  .forEach(function (_a) {
1137
- var stateCell = _a.stateCell, node = _a.node;
1138
- if (stateCell.registeredInitFunc) {
1139
- var newInit = stateCell.registeredInitFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1265
+ var node = _a.node, stateCell = _a.stateCell;
1266
+ var initFunc = node.getInitFunc(stateCell);
1267
+ if (initFunc) {
1268
+ var newInit = initFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1140
1269
  if (!deepEqual(newInit, stateCell.initialValue)) {
1141
1270
  resetSpecs.push({ stateCell: stateCell, node: node });
1142
1271
  }
@@ -1157,14 +1286,13 @@ function useDollarState(specs, props, $ctx) {
1157
1286
  });
1158
1287
  }, [props, resetSpecs]);
1159
1288
  useIsomorphicLayoutEffect(function () {
1160
- $$state.registrationsQueue.forEach(function (_a) {
1161
- var node = _a.node, path = _a.path, f = _a.f;
1289
+ while ($$state.registrationsQueue.length) {
1290
+ var _a = $$state.registrationsQueue.shift(), node = _a.node, path = _a.path, f = _a.f;
1162
1291
  var stateCell = node.getState(path);
1163
1292
  stateCell.registeredInitFunc = f;
1164
1293
  reInitializeState(node, stateCell);
1165
- });
1166
- $$state.registrationsQueue = [];
1167
- }, [$$state.registrationsQueue]);
1294
+ }
1295
+ }, [$$state.registrationsQueue.length]);
1168
1296
  // immediately initialize exposed non-private states
1169
1297
  useIsomorphicLayoutEffect(function () {
1170
1298
  $$state.specTreeLeaves.forEach(function (node) {
@@ -1177,6 +1305,7 @@ function useDollarState(specs, props, $ctx) {
1177
1305
  }, []);
1178
1306
  // Re-render if any value changed in one of these objects
1179
1307
  useSnapshot($$state.stateValues, { sync: true });
1308
+ useSnapshot($$state.registrationsQueue);
1180
1309
  return $state;
1181
1310
  }
1182
1311