@plasmicapp/react-web 0.2.137 → 0.2.139

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.
@@ -16,7 +16,6 @@ var focus = require('@react-aria/focus');
16
16
  var clone = _interopDefault(require('clone'));
17
17
  var deepEqual = _interopDefault(require('fast-deep-equal'));
18
18
  var valtio = require('valtio');
19
- var utils = require('valtio/utils');
20
19
  var checkbox = require('@react-aria/checkbox');
21
20
  var visuallyHidden = require('@react-aria/visually-hidden');
22
21
  var toggle = require('@react-stately/toggle');
@@ -1509,6 +1508,9 @@ function useTrigger(trigger, opts) {
1509
1508
  return TRIGGER_TO_HOOK[trigger](opts);
1510
1509
  }
1511
1510
 
1511
+ var ARRAY_SYMBOL = /*#__PURE__*/Symbol("[]");
1512
+ var PLASMIC_STATE_PROXY_SYMBOL = /*#__PURE__*/Symbol("plasmic.state.proxy");
1513
+
1512
1514
  function generateStateOnChangeProp($state, stateName, dataReps) {
1513
1515
  return function (val, path) {
1514
1516
  return set($state, [stateName].concat(dataReps, path), val);
@@ -1524,6 +1526,36 @@ function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
1524
1526
  ) {
1525
1527
  return _get($state, path);
1526
1528
  }
1529
+ var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
1530
+ function isPlasmicStateProxy(obj) {
1531
+ return obj != null && typeof obj === "object" && obj[PLASMIC_STATE_PROXY_SYMBOL];
1532
+ }
1533
+ function shallowEqual(a1, a2) {
1534
+ if (a1.length !== a2.length) {
1535
+ return false;
1536
+ }
1537
+ for (var i = 0; i < a1.length; i++) {
1538
+ if (a1[i] !== a2[i]) {
1539
+ return false;
1540
+ }
1541
+ }
1542
+ return true;
1543
+ }
1544
+ function isNum(value) {
1545
+ return typeof value === "symbol" ? false : !isNaN(+value);
1546
+ }
1547
+ function assert(cond, msg) {
1548
+ if (msg === void 0) {
1549
+ msg = "Assertion failed";
1550
+ }
1551
+ if (!cond) {
1552
+ // We always generate an non empty message so that it doesn't get swallowed
1553
+ // by the async library.
1554
+ msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
1555
+ debugger;
1556
+ throw new Error(msg);
1557
+ }
1558
+ }
1527
1559
  /**
1528
1560
  * Forked from https://github.com/lukeed/dset
1529
1561
  * Changes: fixed setting a deep value to a proxy object
@@ -1538,44 +1570,42 @@ function set(obj, keys, val) {
1538
1570
  while (i < l) {
1539
1571
  k = keys[i++];
1540
1572
  if (k === "__proto__" || k === "constructor" || k === "prototype") break;
1541
- if (i === l) {
1542
- t[k] = val;
1543
- t = t[k];
1544
- } else {
1545
- if (typeof (x = t[k]) === typeof keys) {
1546
- t = t[k] = x;
1547
- } else if (keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".")) {
1548
- t[k] = {};
1549
- t = t[k];
1550
- } else {
1551
- t[k] = [];
1552
- t = t[k];
1553
- }
1554
- }
1573
+ var newValue = i === l ? val : typeof (x = t[k]) === typeof keys ? x : keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".") ? {} : [];
1574
+ assignValue(t, k, newValue);
1575
+ t = t[k];
1555
1576
  }
1556
1577
  }
1557
- var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
1558
- function shallowEqual(a1, a2) {
1559
- if (a1.length !== a2.length) {
1560
- return false;
1561
- }
1562
- for (var i = 0; i < a1.length; i++) {
1563
- if (a1[i] !== a2[i]) {
1564
- return false;
1565
- }
1578
+ /**
1579
+ * Forked from lodash
1580
+ */
1581
+ function baseAssignValue(object, key, value) {
1582
+ if (key == "__proto__") {
1583
+ Object.defineProperty(object, key, {
1584
+ configurable: true,
1585
+ enumerable: true,
1586
+ value: value,
1587
+ writable: true
1588
+ });
1589
+ } else {
1590
+ object[key] = value;
1566
1591
  }
1567
- return true;
1568
1592
  }
1569
- function isNum(value) {
1570
- return typeof value === "symbol" ? false : !isNaN(+value);
1593
+ function eq(value, other) {
1594
+ return value === other || value !== value && other !== other;
1595
+ }
1596
+ function assignValue(object, key, value) {
1597
+ var objValue = object[key];
1598
+ if (!(Object.prototype.hasOwnProperty.call(object, key) && eq(objValue, value)) || value === undefined && !(key in object)) {
1599
+ baseAssignValue(object, key, value);
1600
+ }
1571
1601
  }
1572
1602
 
1573
- var ARRAY_SYMBOL = /*#__PURE__*/Symbol("[]");
1574
-
1603
+ var UNINITIALIZED = /*#__PURE__*/Symbol("plasmic.unitialized");
1575
1604
  var StateSpecNode = /*#__PURE__*/function () {
1576
1605
  function StateSpecNode(specs) {
1577
1606
  this.specs = specs;
1578
1607
  this.edges = new Map();
1608
+ this.state = {};
1579
1609
  }
1580
1610
  var _proto = StateSpecNode.prototype;
1581
1611
  _proto.hasEdge = function hasEdge(key) {
@@ -1584,6 +1614,9 @@ var StateSpecNode = /*#__PURE__*/function () {
1584
1614
  _proto.addEdge = function addEdge(key, node) {
1585
1615
  this.edges.set(key, node);
1586
1616
  };
1617
+ _proto.children = function children() {
1618
+ return this.edges.values();
1619
+ };
1587
1620
  _proto.makeTransition = function makeTransition(key) {
1588
1621
  key = isNum(key) ? ARRAY_SYMBOL : key;
1589
1622
  return this.edges.get(key);
@@ -1600,6 +1633,40 @@ var StateSpecNode = /*#__PURE__*/function () {
1600
1633
  _proto.getAllSpecs = function getAllSpecs() {
1601
1634
  return this.specs;
1602
1635
  };
1636
+ _proto.getState = function getState(path) {
1637
+ return this.state[JSON.stringify(path)];
1638
+ };
1639
+ _proto.clearStates = function clearStates() {
1640
+ this.state = {};
1641
+ };
1642
+ _proto.states = function states() {
1643
+ return Object.values(this.state);
1644
+ };
1645
+ _proto.hasState = function hasState(path) {
1646
+ var key = JSON.stringify(path);
1647
+ return key in this.state;
1648
+ };
1649
+ _proto.createStateCell = function createStateCell(path) {
1650
+ var key = JSON.stringify(path);
1651
+ this.state[key] = {
1652
+ listeners: [],
1653
+ initialValue: UNINITIALIZED,
1654
+ registeredInitFunc: this.getSpec().initFunc,
1655
+ path: path
1656
+ };
1657
+ };
1658
+ _proto.setInitialValue = function setInitialValue(path, value) {
1659
+ var key = JSON.stringify(path);
1660
+ this.state[key].initialValue = value;
1661
+ };
1662
+ _proto.getInitialValue = function getInitialValue(path) {
1663
+ var key = JSON.stringify(path);
1664
+ return this.state[key].initialValue;
1665
+ };
1666
+ _proto.addListener = function addListener(path, f) {
1667
+ var key = JSON.stringify(path);
1668
+ this.state[key].listeners.push(f);
1669
+ };
1603
1670
  return StateSpecNode;
1604
1671
  }();
1605
1672
  var transformPathStringToObj = function transformPathStringToObj(str) {
@@ -1608,7 +1675,7 @@ var transformPathStringToObj = function transformPathStringToObj(str) {
1608
1675
  };
1609
1676
  return str.split(".").flatMap(splitStatePathPart);
1610
1677
  };
1611
- function buildGraph(specs) {
1678
+ function buildTree(specs) {
1612
1679
  var internalSpec = specs.map(function (spec) {
1613
1680
  return _extends({}, spec, {
1614
1681
  pathObj: transformPathStringToObj(spec.path),
@@ -1633,339 +1700,314 @@ function buildGraph(specs) {
1633
1700
  };
1634
1701
  return rec([]);
1635
1702
  }
1636
-
1637
- var mkUntrackedValue = function mkUntrackedValue(o) {
1638
- return o != null && typeof o === "object" ? valtio.ref(o) : o;
1639
- };
1640
- function shallowEqual$1(a1, a2) {
1641
- if (a1.length !== a2.length) {
1642
- return false;
1643
- }
1644
- for (var i = 0; i < a1.length; i++) {
1645
- if (a1[i] !== a2[i]) {
1646
- return false;
1703
+ function getLeaves(root) {
1704
+ var leaves = [];
1705
+ var rec = function rec(node) {
1706
+ for (var _iterator = _createForOfIteratorHelperLoose(node.children()), _step; !(_step = _iterator()).done;) {
1707
+ var child = _step.value;
1708
+ rec(child);
1709
+ }
1710
+ if (node.isLeaf()) {
1711
+ leaves.push(node);
1712
+ }
1713
+ };
1714
+ rec(root);
1715
+ return leaves;
1716
+ }
1717
+ function findStateCell(root, pathStr, repetitionIndex) {
1718
+ var realPath = [];
1719
+ var pathObj = transformPathStringToObj(pathStr);
1720
+ var currRepIndex = 0;
1721
+ for (var _iterator2 = _createForOfIteratorHelperLoose(pathObj), _step2; !(_step2 = _iterator2()).done;) {
1722
+ var part = _step2.value;
1723
+ if (typeof part === "symbol") {
1724
+ if (!root.hasArrayTransition() || !repetitionIndex || currRepIndex > repetitionIndex.length) {
1725
+ console.log(root);
1726
+ throw new Error("transition not found: pathStr " + pathStr + " part " + (typeof part === "symbol" ? "[]" : part));
1727
+ }
1728
+ realPath.push(repetitionIndex[currRepIndex++]);
1729
+ root = root.makeTransition(ARRAY_SYMBOL);
1730
+ } else {
1731
+ if (!root.hasEdge(part)) {
1732
+ throw new Error("transition not found: pathStr " + pathStr + " part " + (typeof part === "symbol" ? "[]" : part));
1733
+ }
1734
+ realPath.push(part);
1735
+ root = root.makeTransition(part);
1647
1736
  }
1648
1737
  }
1649
- return true;
1738
+ return {
1739
+ node: root,
1740
+ realPath: realPath
1741
+ };
1650
1742
  }
1743
+
1651
1744
  function isNum$1(value) {
1652
1745
  return typeof value === "symbol" ? false : !isNaN(+value);
1653
1746
  }
1654
- function saveNewState($$state, path, spec) {
1655
- var key = JSON.stringify(path);
1656
- $$state.existingStates.set(key, {
1657
- path: path,
1658
- specKey: spec.path
1747
+ function canProxy(value) {
1748
+ return typeof value === "object" && value != null;
1749
+ }
1750
+ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyRoot) {
1751
+ var stateAccess = new Set();
1752
+ var $state = create$StateProxy($$state, function (node, path) {
1753
+ return {
1754
+ get: function get() {
1755
+ stateAccess.add({
1756
+ path: path,
1757
+ node: node
1758
+ });
1759
+ var spec = node.getSpec();
1760
+ if (spec.valueProp) {
1761
+ return $$state.props[spec.valueProp];
1762
+ } else if (!node.hasState(path) && spec.initFunc) {
1763
+ return initializeStateValue($$state, node, path, proxyRoot);
1764
+ }
1765
+ return _get(proxyRoot, path);
1766
+ },
1767
+ set: function set() {
1768
+ throw new Error("Cannot update state values during initialization");
1769
+ }
1770
+ };
1659
1771
  });
1660
- if (!$$state.statesInstanceBySpec.has(spec.path)) {
1661
- $$state.statesInstanceBySpec.set(spec.path, []);
1662
- }
1663
- $$state.statesInstanceBySpec.get(spec.path).push({
1664
- path: path,
1665
- specKey: spec.path
1772
+ stateAccess.forEach(function (_ref) {
1773
+ var node = _ref.node,
1774
+ path = _ref.path;
1775
+ node.addListener(path, function () {
1776
+ var newValue = initialSpecNode.getSpec().initFunc($$state.props, $state, $$state.ctx);
1777
+ set(proxyRoot, initialStatePath, newValue);
1778
+ });
1666
1779
  });
1780
+ var initialValue = initialSpecNode.getState(initialStatePath).registeredInitFunc($$state.props, $state, $$state.ctx);
1781
+ initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
1782
+ var initialSpec = initialSpecNode.getSpec();
1783
+ var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
1784
+ set(proxyRoot, initialStatePath, value);
1785
+ //immediately fire onChange
1786
+ if (initialSpec.onChangeProp) {
1787
+ var _$$state$props$initia, _$$state$props;
1788
+ (_$$state$props$initia = (_$$state$props = $$state.props)[initialSpec.onChangeProp]) == null ? void 0 : _$$state$props$initia.call(_$$state$props, initialValue);
1789
+ }
1790
+ return initialValue;
1667
1791
  }
1668
- function create$StateProxy($$state, handlers) {
1669
- var rec = function rec(currPath, currNode) {
1792
+ function create$StateProxy($$state, leafHandlers) {
1793
+ var rec = function rec(currPath, currNode, isOutside, proxyRoot, initialObject) {
1670
1794
  var getNextPath = function getNextPath(property) {
1671
1795
  return [].concat(currPath, [isNum$1(property) ? +property : property]);
1672
1796
  };
1673
- return new Proxy(currNode.hasArrayTransition() ? [] : {}, {
1797
+ var spec = currNode.getSpec();
1798
+ var handlers = {
1674
1799
  deleteProperty: function deleteProperty(target, property) {
1675
- var prefixPath = getNextPath(property);
1676
- var specKeysToUpdate = new Set();
1677
- $$state.existingStates.forEach(function (_ref) {
1678
- var path = _ref.path,
1679
- specKey = _ref.specKey;
1680
- if (path.length >= prefixPath.length && shallowEqual$1(path.slice(0, prefixPath.length), prefixPath)) {
1681
- deleteState($$state, path);
1682
- specKeysToUpdate.add(specKey);
1683
- }
1684
- });
1685
- specKeysToUpdate.forEach(function (specKey) {
1686
- var spec = $$state.specsByKey[specKey];
1687
- if (spec.onChangeProp) {
1688
- var _$$state$props$spec$o, _$$state$props;
1689
- (_$$state$props$spec$o = (_$$state$props = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props, _get($$state.stateValues, currPath), currPath);
1690
- }
1691
- });
1800
+ if (!isOutside && !currNode.isLeaf() && !currNode.hasArrayTransition() && !isNum$1(property)) {
1801
+ throw new Error("Can't delete a property in the middle of the state spec");
1802
+ }
1803
+ delete _get($$state.stateValues, currPath)[property];
1804
+ if (spec.onChangeProp) {
1805
+ var _$$state$props$spec$o, _$$state$props2;
1806
+ //we are always in a leaf, since we only have two cases:
1807
+ // 1 - delete properties outside the state tree
1808
+ // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
1809
+ (_$$state$props$spec$o = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props2, _get($$state.stateValues, currPath.slice(spec.pathObj.length)));
1810
+ }
1692
1811
  return Reflect.deleteProperty(target, property);
1693
1812
  },
1694
1813
  get: function get(target, property, receiver) {
1814
+ if (property === PLASMIC_STATE_PROXY_SYMBOL) {
1815
+ return true;
1816
+ }
1817
+ proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1695
1818
  var nextPath = getNextPath(property);
1819
+ if (isOutside || currNode.isLeaf()) {
1820
+ return Reflect.get(target, property, receiver);
1821
+ }
1696
1822
  var nextNode = currNode.makeTransition(property);
1697
- if (nextNode) {
1698
- if (nextNode.isLeaf()) {
1699
- var _handlers$get, _handlers;
1700
- target[property] = (_handlers$get = (_handlers = handlers(nextPath, nextNode.getSpec())).get) == null ? void 0 : _handlers$get.call(_handlers, target, property, receiver);
1701
- } else if (!(property in target)) {
1702
- target[property] = rec(nextPath, nextNode);
1703
- }
1823
+ if (nextNode != null && nextNode.isLeaf()) {
1824
+ var _leafHandlers$get, _leafHandlers;
1825
+ return (_leafHandlers$get = (_leafHandlers = leafHandlers(nextNode, nextPath, proxyRoot)).get) == null ? void 0 : _leafHandlers$get.call(_leafHandlers, target, property, receiver);
1826
+ } else if (nextNode && !(property in target)) {
1827
+ target[property] = rec(nextPath, nextNode, false, proxyRoot, undefined);
1704
1828
  }
1705
1829
  return Reflect.get(target, property, receiver);
1706
1830
  },
1707
1831
  set: function set$1(target, property, value, receiver) {
1832
+ var _nextNode, _nextNode2;
1833
+ proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1708
1834
  var nextPath = getNextPath(property);
1709
1835
  var nextNode = currNode.makeTransition(property);
1710
- if (nextNode && typeof property !== "symbol") {
1711
- if (nextNode.isLeaf()) {
1712
- var _handlers$set, _handlers2;
1713
- // reached the end of the spec
1714
- target[property] = (_handlers$set = (_handlers2 = handlers(nextPath, nextNode.getSpec())).set) == null ? void 0 : _handlers$set.call(_handlers2, target, property, value, receiver);
1715
- return Reflect.set(target, property, value, receiver);
1716
- } else if (typeof value === "object") {
1717
- target[property] = rec(nextPath, nextNode);
1718
- for (var _i = 0, _Object$keys = Object.keys(value); _i < _Object$keys.length; _i++) {
1719
- var key = _Object$keys[_i];
1720
- target[property][key] = value[key];
1721
- }
1722
- return true;
1723
- }
1724
- }
1725
1836
  if (property === "registerInitFunc" && currPath.length === 0) {
1726
1837
  return Reflect.set(target, property, value, receiver);
1727
1838
  }
1728
- if (currNode.hasArrayTransition()) {
1729
- var _currNode$makeTransit, _currNode$makeTransit2;
1839
+ if (!nextNode && currNode.hasArrayTransition()) {
1730
1840
  set($$state.stateValues, nextPath, value);
1731
- (_currNode$makeTransit = currNode.makeTransition(ARRAY_SYMBOL)) == null ? void 0 : (_currNode$makeTransit2 = _currNode$makeTransit.getAllSpecs()) == null ? void 0 : _currNode$makeTransit2.forEach(function (spec) {
1732
- if (spec.onChangeProp) {
1733
- var _$$state$props$spec$o2, _$$state$props2;
1734
- (_$$state$props$spec$o2 = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o2.call(_$$state$props2, value, nextPath);
1735
- }
1736
- });
1841
+ //array can set his own properties such as length, map, ...
1737
1842
  return Reflect.set(target, property, value, receiver);
1738
1843
  }
1739
- // invalid setting a value that doesn't make part of the spec
1740
- return false;
1741
- }
1742
- });
1743
- };
1744
- return rec([], $$state.rootStateSpec);
1745
- }
1746
- var deleteState = function deleteState($$state, path) {
1747
- var _$$state$unsubscripti;
1748
- var key = JSON.stringify(path);
1749
- (_$$state$unsubscripti = $$state.unsubscriptionsByState[key]) == null ? void 0 : _$$state$unsubscripti.forEach(function (f) {
1750
- return f();
1751
- });
1752
- delete $$state.unsubscriptionsByState[key];
1753
- $$state.existingStates["delete"](key);
1754
- // delete get($$state.stateValues, path.slice(-1))[path.slice(-1)[0]];
1755
- // delete get($$state.initStateValues, path.slice(-1))[path.slice(-1)[0]];
1756
- };
1757
-
1758
- var getIndexes = function getIndexes(path, spec) {
1759
- var indexes = [];
1760
- if (path.length !== spec.pathObj.length) {
1761
- throw new Error("Unexpected error: state path and spec path have different lengths");
1762
- }
1763
- for (var i = 0; i < spec.pathObj.length; i++) {
1764
- if (spec.pathObj[i] === "[]") {
1765
- indexes.push(path[i]);
1766
- }
1767
- }
1768
- return indexes;
1769
- };
1770
- function initializeStateValue($$state, initialStatePath, initialSpec) {
1771
- var _$$state$unsubscripti2;
1772
- var initialStateKey = JSON.stringify(initialStatePath);
1773
- var stateAccess = new Set();
1774
- var $state = create$StateProxy($$state, function (path, spec) {
1775
- return {
1776
- get: function get() {
1777
- var key = JSON.stringify(path);
1778
- stateAccess.add({
1779
- path: path,
1780
- spec: spec
1781
- });
1782
- if (spec.valueProp) {
1783
- return !spec.isRepeated ? $$state.props[spec.valueProp] : _get($$state.props[spec.valueProp], path.slice(1));
1844
+ if ((_nextNode = nextNode) != null && _nextNode.isLeaf()) {
1845
+ var _leafHandlers$set, _leafHandlers2;
1846
+ (_leafHandlers$set = (_leafHandlers2 = leafHandlers(nextNode, nextPath, proxyRoot)).set) == null ? void 0 : _leafHandlers$set.call(_leafHandlers2, target, property, value, receiver);
1784
1847
  }
1785
- if ($$state.existingStates.has(key)) {
1786
- // is already initialized
1787
- return _get($$state.stateValues, path);
1788
- } else if (spec.initFunc) {
1789
- initializeStateValue($$state, path, spec);
1848
+ if (!isOutside && !currNode.isLeaf() && !nextNode) {
1849
+ // can't set an unknown field in $state
1850
+ return false;
1790
1851
  }
1791
- return _get($$state.stateValues, path);
1792
- },
1793
- set: function set() {
1794
- throw new Error("Cannot update state values during initialization");
1852
+ // we keep pointing to the leaf
1853
+ if (!nextNode) {
1854
+ assert(isOutside || currNode.isLeaf, "unexpected update in nextNode");
1855
+ nextNode = currNode;
1856
+ }
1857
+ if (canProxy(value)) {
1858
+ target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), proxyRoot, value);
1859
+ } else if (!isOutside && !currNode.isLeaf() && !((_nextNode2 = nextNode) != null && _nextNode2.isLeaf())) {
1860
+ throw new Error("inserting a primitive value into a non-leaf");
1861
+ } else {
1862
+ Reflect.set(target, property, value, receiver);
1863
+ }
1864
+ nextNode.getAllSpecs().forEach(function (spec) {
1865
+ if (spec.onChangeProp) {
1866
+ var _$$state$props$spec$o2, _$$state$props3;
1867
+ (_$$state$props$spec$o2 = (_$$state$props3 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o2.call(_$$state$props3, value);
1868
+ }
1869
+ });
1870
+ var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable ? mkUntrackedValue(value) : value;
1871
+ set($$state.stateValues, nextPath, newValue);
1872
+ return true;
1795
1873
  }
1796
1874
  };
1797
- });
1798
- (_$$state$unsubscripti2 = $$state.unsubscriptionsByState[initialStateKey]) == null ? void 0 : _$$state$unsubscripti2.forEach(function (f) {
1799
- return f();
1800
- });
1801
- $$state.unsubscriptionsByState[initialStateKey] = [];
1802
- stateAccess.forEach(function (_ref2) {
1803
- var path = _ref2.path,
1804
- spec = _ref2.spec;
1805
- var unsubscribe = utils.subscribeKey(_get($$state.stateValues, path.slice(-1)), path.slice(-1)[0], function () {
1806
- return saveValue($$state, initialStatePath, initialSpec, initialSpec.initFunc($$state.props, $state, $$state.ctx, getIndexes(path, spec)));
1807
- });
1808
- $$state.unsubscriptionsByState[initialStateKey].push(unsubscribe);
1809
- });
1810
- var initialValue = initialSpec.initFunc($$state.props, $state, $$state.ctx, getIndexes(initialStatePath, initialSpec));
1811
- saveStateInitialValue($$state, initialStatePath, initialSpec, initialValue);
1812
- return initialValue;
1813
- }
1814
- function saveValue($$state, path, spec, value) {
1815
- if (spec.isImmutable) {
1816
- set($$state.stateValues, path, mkUntrackedValue(value));
1817
- } else {
1818
- set($$state.stateValues, path, value);
1819
- }
1820
- }
1821
- function saveStateInitialValue($$state, path, spec, initialValue) {
1822
- if (spec.isImmutable) {
1823
- var untrackedValue = mkUntrackedValue(initialValue);
1824
- set($$state.stateValues, path, untrackedValue);
1825
- set($$state.initStateValues, path, clone(untrackedValue));
1826
- } else {
1827
- set($$state.stateValues, path, clone(initialValue));
1828
- set($$state.initStateValues, path, clone(initialValue));
1829
- }
1875
+ var baseObject = !isOutside && !currNode.isLeaf() ? currNode.hasArrayTransition() ? [] : {} : Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
1876
+ var proxyObj = new Proxy(baseObject, handlers);
1877
+ if (initialObject) {
1878
+ Reflect.ownKeys(initialObject).forEach(function (key) {
1879
+ var desc = Object.getOwnPropertyDescriptor(initialObject, key);
1880
+ if (desc.get || desc.set) {
1881
+ Object.defineProperty(baseObject, key, desc);
1882
+ } else {
1883
+ proxyObj[key] = initialObject[key];
1884
+ }
1885
+ });
1886
+ }
1887
+ return proxyObj;
1888
+ };
1889
+ return rec([], $$state.rootSpecTree, false, undefined, undefined);
1830
1890
  }
1891
+ var mkUntrackedValue = function mkUntrackedValue(o) {
1892
+ return o != null && typeof o === "object" ? valtio.ref(o) : o;
1893
+ };
1831
1894
  function useDollarState(specs, props, $ctx) {
1832
- var $$state = React__default.useRef(valtio.proxy({
1833
- stateValues: {},
1834
- initStateValues: {},
1835
- specsByKey: Object.fromEntries(specs.map(function (spec) {
1836
- return [spec.path, _extends({}, spec, {
1837
- pathObj: transformPathStringToObj(spec.path),
1838
- isRepeated: spec.path.split(".").some(function (part) {
1839
- return part.endsWith("[]");
1840
- })
1841
- })];
1842
- })),
1843
- statesInstanceBySpec: new Map(),
1844
- existingStates: new Map(),
1845
- unsubscriptionsByState: {},
1846
- props: {},
1847
- ctx: {},
1848
- registrationsQueue: [],
1849
- rootStateSpec: buildGraph(specs)
1850
- })).current;
1851
- $$state.props = mkUntrackedValue(props);
1852
- $$state.ctx = mkUntrackedValue($ctx != null ? $ctx : {});
1853
- var $state = React__default.useRef(Object.assign(create$StateProxy($$state, function (path, spec) {
1854
- var key = JSON.stringify(path);
1855
- if (!$$state.existingStates.has(key)) {
1856
- var _spec$initVal;
1857
- saveNewState($$state, path, spec);
1858
- var initialValue = !spec.initFunc ? (_spec$initVal = spec.initVal) != null ? _spec$initVal : undefined : initializeStateValue($$state, path, spec);
1859
- saveStateInitialValue($$state, path, spec, initialValue);
1860
- }
1895
+ var $$state = React__default.useRef(function () {
1896
+ var rootSpecTree = buildTree(specs);
1861
1897
  return {
1862
- get: function get() {
1863
- if (spec.valueProp) {
1864
- var value = !spec.isRepeated ? $$state.props[spec.valueProp] : _get($$state.props[spec.valueProp], path.slice(1));
1865
- return value;
1866
- } else {
1867
- return _get($$state.stateValues, path);
1868
- }
1869
- },
1870
- set: function set(_t, _p, value) {
1871
- saveValue($$state, path, spec, value);
1872
- if (spec.onChangeProp) {
1873
- var _$$state$props$spec$o3, _$$state$props3;
1874
- (_$$state$props$spec$o3 = (_$$state$props3 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o3.call(_$$state$props3, value, path);
1898
+ rootSpecTree: rootSpecTree,
1899
+ specTreeLeaves: getLeaves(rootSpecTree),
1900
+ stateValues: valtio.proxy({}),
1901
+ props: {},
1902
+ ctx: {},
1903
+ registrationsQueue: []
1904
+ };
1905
+ }()).current;
1906
+ $$state.props = props;
1907
+ $$state.ctx = $ctx != null ? $ctx : {};
1908
+ var $state = React__default.useRef(function () {
1909
+ var useRef$state = Object.assign(create$StateProxy($$state, function (node, path, proxyRoot) {
1910
+ if (!node.hasState(path)) {
1911
+ node.createStateCell(path);
1912
+ var spec = node.getSpec();
1913
+ if (spec.initFunc) {
1914
+ initializeStateValue($$state, node, path, proxyRoot);
1915
+ } else if (!spec.valueProp) {
1916
+ set(proxyRoot, path, spec.initVal);
1875
1917
  }
1876
- return true;
1877
1918
  }
1878
- };
1879
- }), {
1880
- registerInitFunc: function registerInitFunc(pathStr, f) {
1881
- var _$$state$statesInstan;
1882
- if ((_$$state$statesInstan = $$state.statesInstanceBySpec.get(pathStr)) != null && _$$state$statesInstan.some(function (_ref3) {
1883
- var path = _ref3.path,
1884
- specKey = _ref3.specKey;
1885
- return !deepEqual(_get($$state.initStateValues, path), f(props, $state, $ctx != null ? $ctx : {}, getIndexes(path, $$state.specsByKey[specKey])));
1886
- })) {
1887
- $$state.registrationsQueue.push({
1888
- pathStr: pathStr,
1889
- f: f
1890
- });
1919
+ return {
1920
+ get: function get(target, property, receiver) {
1921
+ var spec = node.getSpec();
1922
+ if (spec.valueProp) {
1923
+ return $$state.props[spec.valueProp];
1924
+ } else {
1925
+ return Reflect.get(target, property, receiver);
1926
+ }
1927
+ }
1928
+ };
1929
+ }), {
1930
+ registerInitFunc: function registerInitFunc(pathStr, f, repetitionIndex) {
1931
+ var _findStateCell = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex),
1932
+ node = _findStateCell.node,
1933
+ realPath = _findStateCell.realPath;
1934
+ if (!node.hasState(realPath)) {
1935
+ node.createStateCell(realPath);
1936
+ }
1937
+ if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, useRef$state, $$state.ctx))) {
1938
+ $$state.registrationsQueue.push({
1939
+ node: node,
1940
+ path: realPath,
1941
+ f: f
1942
+ });
1943
+ }
1891
1944
  }
1892
- }
1893
- })).current;
1945
+ });
1946
+ return useRef$state;
1947
+ }()).current;
1894
1948
  // For each spec with an initFunc, evaluate it and see if
1895
1949
  // the init value has changed. If so, reset its state.
1896
1950
  var resetSpecs = [];
1897
- $$state.existingStates.forEach(function (_ref4) {
1898
- var path = _ref4.path,
1899
- specKey = _ref4.specKey;
1900
- var spec = $$state.specsByKey[specKey];
1901
- if (spec.initFunc) {
1902
- var newInit = spec.initFunc(props, $state, $ctx != null ? $ctx : {}, getIndexes(path, spec));
1903
- if (!deepEqual(newInit, _get($$state.initStateValues, path))) {
1951
+ $$state.specTreeLeaves.flatMap(function (node) {
1952
+ return node.states().map(function (stateCell) {
1953
+ return {
1954
+ stateCell: stateCell,
1955
+ node: node
1956
+ };
1957
+ });
1958
+ }).forEach(function (_ref2) {
1959
+ var stateCell = _ref2.stateCell,
1960
+ node = _ref2.node;
1961
+ if (stateCell.registeredInitFunc) {
1962
+ var newInit = stateCell.registeredInitFunc(props, $state, $ctx != null ? $ctx : {});
1963
+ if (!deepEqual(newInit, stateCell.initialValue)) {
1904
1964
  resetSpecs.push({
1905
- path: path,
1906
- spec: spec
1965
+ stateCell: stateCell,
1966
+ node: node
1907
1967
  });
1908
1968
  }
1909
1969
  }
1910
1970
  });
1971
+ var reInitializeState = function reInitializeState(node, stateCell) {
1972
+ var newInit = initializeStateValue($$state, node, stateCell.path, $state);
1973
+ var spec = node.getSpec();
1974
+ if (spec.onChangeProp) {
1975
+ var _$$state$props$spec$o3, _$$state$props4;
1976
+ (_$$state$props$spec$o3 = (_$$state$props4 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o3.call(_$$state$props4, newInit);
1977
+ }
1978
+ };
1911
1979
  useIsomorphicLayoutEffect$1(function () {
1912
- resetSpecs.forEach(function (_ref5) {
1913
- var path = _ref5.path,
1914
- spec = _ref5.spec;
1915
- var newInit = initializeStateValue($$state, path, spec);
1916
- if (spec.onChangeProp) {
1917
- var _$$state$props$spec$o4, _$$state$props4;
1918
- (_$$state$props$spec$o4 = (_$$state$props4 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o4.call(_$$state$props4, newInit, path);
1919
- }
1980
+ resetSpecs.forEach(function (_ref3) {
1981
+ var stateCell = _ref3.stateCell,
1982
+ node = _ref3.node;
1983
+ reInitializeState(node, stateCell);
1920
1984
  });
1921
1985
  }, [props, resetSpecs]);
1922
1986
  useIsomorphicLayoutEffect$1(function () {
1923
- $$state.registrationsQueue.forEach(function (_ref6) {
1924
- var f = _ref6.f,
1925
- pathStr = _ref6.pathStr;
1926
- $$state.specsByKey[pathStr].initFunc = f;
1987
+ $$state.registrationsQueue.forEach(function (_ref4) {
1988
+ var node = _ref4.node,
1989
+ path = _ref4.path,
1990
+ f = _ref4.f;
1991
+ var stateCell = node.getState(path);
1992
+ stateCell.registeredInitFunc = f;
1993
+ reInitializeState(node, stateCell);
1927
1994
  });
1928
1995
  $$state.registrationsQueue = [];
1929
1996
  }, [$$state.registrationsQueue]);
1997
+ // immediately initialize exposed non-private states
1998
+ useIsomorphicLayoutEffect$1(function () {
1999
+ $$state.specTreeLeaves.forEach(function (node) {
2000
+ var spec = node.getSpec();
2001
+ if (!spec.isRepeated && spec.type !== "private" && spec.initFunc) {
2002
+ node.createStateCell(spec.pathObj);
2003
+ initializeStateValue($$state, node, spec.pathObj, $state);
2004
+ }
2005
+ });
2006
+ }, []);
1930
2007
  // Re-render if any value changed in one of these objects
1931
2008
  valtio.useSnapshot($$state.stateValues, {
1932
2009
  sync: true
1933
2010
  });
1934
- valtio.useSnapshot($$state.specsByKey, {
1935
- sync: true
1936
- });
1937
- return $state;
1938
- }
1939
- // Simple version of $state useDollarState for read-only
1940
- function useCanvasDollarState(specs, props, $ctx) {
1941
- var $$state = valtio.proxy({
1942
- stateValues: {},
1943
- initStateValues: {},
1944
- specsByKey: Object.fromEntries(specs.map(function (spec) {
1945
- return [spec.path, _extends({}, spec, {
1946
- pathObj: transformPathStringToObj(spec.path),
1947
- isRepeated: spec.path.split(".").some(function (part) {
1948
- return part.endsWith("[]");
1949
- })
1950
- })];
1951
- })),
1952
- statesInstanceBySpec: new Map(),
1953
- existingStates: new Map(),
1954
- unsubscriptionsByState: {},
1955
- props: {},
1956
- ctx: {},
1957
- registrationsQueue: [],
1958
- rootStateSpec: buildGraph(specs)
1959
- });
1960
- $$state.props = mkUntrackedValue(props);
1961
- $$state.ctx = mkUntrackedValue($ctx);
1962
- var $state = {};
1963
- for (var _iterator = _createForOfIteratorHelperLoose(specs), _step; !(_step = _iterator()).done;) {
1964
- var spec = _step.value;
1965
- var path = transformPathStringToObj(spec.path);
1966
- var init = spec.valueProp ? $$state.props[spec.valueProp] : spec.initVal ? spec.initVal : spec.initFunc ? initializeStateValue($$state, path, $$state.specsByKey[spec.path]) : undefined;
1967
- set($state, path, init);
1968
- }
1969
2011
  return $state;
1970
2012
  }
1971
2013
 
@@ -3400,7 +3442,7 @@ exports.generateStateOnChangeProp = generateStateOnChangeProp;
3400
3442
  exports.generateStateValueProp = generateStateValueProp;
3401
3443
  exports.getDataProps = getDataProps;
3402
3444
  exports.hasVariant = hasVariant;
3403
- exports.isNum = isNum;
3445
+ exports.isPlasmicStateProxy = isPlasmicStateProxy;
3404
3446
  exports.makeFragment = makeFragment;
3405
3447
  exports.mergeVariantsWithStates = mergeVariantsWithStates;
3406
3448
  exports.omit = omit;
@@ -3409,13 +3451,10 @@ exports.plasmicHeadMeta = plasmicHeadMeta;
3409
3451
  exports.renderPlasmicSlot = renderPlasmicSlot;
3410
3452
  exports.set = set;
3411
3453
  exports.setPlumeStrictMode = setPlumeStrictMode;
3412
- exports.shallowEqual = shallowEqual;
3413
3454
  exports.useButton = useButton;
3414
- exports.useCanvasDollarState = useCanvasDollarState;
3415
3455
  exports.useCheckbox = useCheckbox;
3416
3456
  exports.useDollarState = useDollarState;
3417
3457
  exports.useIsSSR = useIsSSR;
3418
- exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect$1;
3419
3458
  exports.useMenu = useMenu;
3420
3459
  exports.useMenuButton = useMenuButton;
3421
3460
  exports.useMenuGroup = useMenuGroup;