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