@plasmicapp/react-web 0.2.140 → 0.2.141
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/all.d.ts +6 -9
- package/dist/react-web.cjs.development.js +166 -90
- package/dist/react-web.cjs.development.js.map +1 -1
- package/dist/react-web.cjs.production.min.js +1 -1
- package/dist/react-web.cjs.production.min.js.map +1 -1
- package/dist/react-web.esm.js +166 -90
- package/dist/react-web.esm.js.map +1 -1
- package/dist/states/graph.d.ts +10 -4
- package/dist/states/helpers.d.ts +7 -9
- package/dist/states/valtio.d.ts +3 -1
- package/dist/stories/UseDollarState.stories.d.ts +7 -0
- package/package.json +2 -2
- package/skinny/dist/index.js +153 -82
- package/skinny/dist/index.js.map +1 -1
- package/skinny/dist/states/graph.d.ts +10 -4
- package/skinny/dist/states/helpers.d.ts +7 -9
- package/skinny/dist/states/valtio.d.ts +3 -1
- package/skinny/dist/stories/UseDollarState.stories.d.ts +7 -0
package/dist/all.d.ts
CHANGED
|
@@ -52,6 +52,7 @@ declare type HTMLElementRefOf<T extends keyof JSX.IntrinsicElements> = Exclude<R
|
|
|
52
52
|
declare function dlv(object: object, key: string | Array<string | number>, defaultValue?: any): any;
|
|
53
53
|
|
|
54
54
|
declare type InitFunc<T> = ($props: Record<string, any>, $state: $State, $ctx: Record<string, any>, indexes?: number[]) => T;
|
|
55
|
+
declare type ObjectPath = (string | number)[];
|
|
55
56
|
interface $StateSpec<T> {
|
|
56
57
|
path: string;
|
|
57
58
|
initFunc?: InitFunc<T>;
|
|
@@ -66,14 +67,8 @@ interface $State {
|
|
|
66
67
|
registerInitFunc?: (path: string, f: InitFunc<any>, repetitonIndex?: number[]) => any;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
declare function generateStateOnChangeProp($state: $State,
|
|
70
|
-
|
|
71
|
-
* This function generate the state value prop for repeated states
|
|
72
|
-
* Example:
|
|
73
|
-
* - parent[][].counter[].count
|
|
74
|
-
* We need to pass `parent[index1][index2].counter to the child component
|
|
75
|
-
*/
|
|
76
|
-
declare function generateStateValueProp($state: $State, path: (string | number)[]): any;
|
|
70
|
+
declare function generateStateOnChangeProp($state: $State, path: ObjectPath): (val: any) => void;
|
|
71
|
+
declare function generateStateValueProp($state: $State, path: ObjectPath): any;
|
|
77
72
|
declare function isPlasmicStateProxy(obj: any): any;
|
|
78
73
|
/**
|
|
79
74
|
* Forked from https://github.com/lukeed/dset
|
|
@@ -81,7 +76,9 @@ declare function isPlasmicStateProxy(obj: any): any;
|
|
|
81
76
|
*/
|
|
82
77
|
declare function set(obj: any, keys: any, val: any): void;
|
|
83
78
|
|
|
84
|
-
declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any
|
|
79
|
+
declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>, opts?: {
|
|
80
|
+
inCanvas: boolean;
|
|
81
|
+
}): $State;
|
|
85
82
|
|
|
86
83
|
interface Variants {
|
|
87
84
|
[vg: string]: any;
|
|
@@ -1517,19 +1517,12 @@ function useTrigger(trigger, opts) {
|
|
|
1517
1517
|
var ARRAY_SYMBOL = /*#__PURE__*/Symbol("[]");
|
|
1518
1518
|
var PLASMIC_STATE_PROXY_SYMBOL = /*#__PURE__*/Symbol("plasmic.state.proxy");
|
|
1519
1519
|
|
|
1520
|
-
function generateStateOnChangeProp($state,
|
|
1521
|
-
return function (val
|
|
1522
|
-
return set($state,
|
|
1520
|
+
function generateStateOnChangeProp($state, path) {
|
|
1521
|
+
return function (val) {
|
|
1522
|
+
return set($state, path, val);
|
|
1523
1523
|
};
|
|
1524
1524
|
}
|
|
1525
|
-
|
|
1526
|
-
* This function generate the state value prop for repeated states
|
|
1527
|
-
* Example:
|
|
1528
|
-
* - parent[][].counter[].count
|
|
1529
|
-
* We need to pass `parent[index1][index2].counter to the child component
|
|
1530
|
-
*/
|
|
1531
|
-
function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
|
|
1532
|
-
) {
|
|
1525
|
+
function generateStateValueProp($state, path) {
|
|
1533
1526
|
return _get($state, path);
|
|
1534
1527
|
}
|
|
1535
1528
|
var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
|
|
@@ -1547,6 +1540,14 @@ function shallowEqual(a1, a2) {
|
|
|
1547
1540
|
}
|
|
1548
1541
|
return true;
|
|
1549
1542
|
}
|
|
1543
|
+
/**
|
|
1544
|
+
* Shallow comparison of arrays.
|
|
1545
|
+
*/
|
|
1546
|
+
function arrayEq(xs, ys) {
|
|
1547
|
+
return xs.length === ys.length && xs.every(function (_, index) {
|
|
1548
|
+
return xs[index] === ys[index];
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1550
1551
|
function isNum(value) {
|
|
1551
1552
|
return typeof value === "symbol" ? false : !isNaN(+value);
|
|
1552
1553
|
}
|
|
@@ -1609,69 +1610,84 @@ function assignValue(object, key, value) {
|
|
|
1609
1610
|
var UNINITIALIZED = /*#__PURE__*/Symbol("plasmic.unitialized");
|
|
1610
1611
|
var StateSpecNode = /*#__PURE__*/function () {
|
|
1611
1612
|
function StateSpecNode(specs) {
|
|
1612
|
-
this.
|
|
1613
|
-
this.
|
|
1614
|
-
this.
|
|
1613
|
+
this._specs = specs;
|
|
1614
|
+
this._edges = new Map();
|
|
1615
|
+
this._state = {};
|
|
1615
1616
|
}
|
|
1616
1617
|
var _proto = StateSpecNode.prototype;
|
|
1618
|
+
_proto.setSpecs = function setSpecs(specs) {
|
|
1619
|
+
this._specs = specs;
|
|
1620
|
+
};
|
|
1621
|
+
_proto.edges = function edges() {
|
|
1622
|
+
return this._edges;
|
|
1623
|
+
};
|
|
1624
|
+
_proto.state = function state() {
|
|
1625
|
+
return this._state;
|
|
1626
|
+
};
|
|
1617
1627
|
_proto.hasEdge = function hasEdge(key) {
|
|
1618
|
-
return this.
|
|
1628
|
+
return this._edges.has(key);
|
|
1619
1629
|
};
|
|
1620
1630
|
_proto.addEdge = function addEdge(key, node) {
|
|
1621
|
-
this.
|
|
1631
|
+
this._edges.set(key, node);
|
|
1632
|
+
};
|
|
1633
|
+
_proto.clearEdges = function clearEdges() {
|
|
1634
|
+
this._edges = new Map();
|
|
1622
1635
|
};
|
|
1623
1636
|
_proto.children = function children() {
|
|
1624
|
-
return this.
|
|
1637
|
+
return this._edges.values();
|
|
1625
1638
|
};
|
|
1626
1639
|
_proto.makeTransition = function makeTransition(key) {
|
|
1627
1640
|
key = isNum(key) ? ARRAY_SYMBOL : key;
|
|
1628
|
-
return this.
|
|
1641
|
+
return this._edges.get(key);
|
|
1629
1642
|
};
|
|
1630
1643
|
_proto.isLeaf = function isLeaf() {
|
|
1631
|
-
return this.
|
|
1644
|
+
return this._edges.size === 0;
|
|
1632
1645
|
};
|
|
1633
1646
|
_proto.hasArrayTransition = function hasArrayTransition() {
|
|
1634
|
-
return this.
|
|
1647
|
+
return this._edges.has(ARRAY_SYMBOL);
|
|
1635
1648
|
};
|
|
1636
1649
|
_proto.getSpec = function getSpec() {
|
|
1637
|
-
return this.
|
|
1650
|
+
return this._specs[0];
|
|
1638
1651
|
};
|
|
1639
1652
|
_proto.getAllSpecs = function getAllSpecs() {
|
|
1640
|
-
return this.
|
|
1653
|
+
return this._specs;
|
|
1641
1654
|
};
|
|
1642
1655
|
_proto.getState = function getState(path) {
|
|
1643
|
-
return this.
|
|
1656
|
+
return this._state[JSON.stringify(path)];
|
|
1657
|
+
};
|
|
1658
|
+
_proto.getInitFunc = function getInitFunc(stateCell) {
|
|
1659
|
+
var _stateCell$registered;
|
|
1660
|
+
return (_stateCell$registered = stateCell.registeredInitFunc) != null ? _stateCell$registered : this.getSpec().initFunc;
|
|
1644
1661
|
};
|
|
1645
1662
|
_proto.clearStates = function clearStates() {
|
|
1646
|
-
this.
|
|
1663
|
+
this._state = {};
|
|
1647
1664
|
};
|
|
1648
1665
|
_proto.states = function states() {
|
|
1649
|
-
return Object.values(this.
|
|
1666
|
+
return Object.values(this._state);
|
|
1650
1667
|
};
|
|
1651
1668
|
_proto.hasState = function hasState(path) {
|
|
1652
1669
|
var key = JSON.stringify(path);
|
|
1653
|
-
return key in this.
|
|
1670
|
+
return key in this._state;
|
|
1654
1671
|
};
|
|
1655
1672
|
_proto.createStateCell = function createStateCell(path) {
|
|
1656
1673
|
var key = JSON.stringify(path);
|
|
1657
|
-
this.
|
|
1674
|
+
this._state[key] = {
|
|
1658
1675
|
listeners: [],
|
|
1659
1676
|
initialValue: UNINITIALIZED,
|
|
1660
|
-
registeredInitFunc: this.getSpec().initFunc,
|
|
1661
1677
|
path: path
|
|
1662
1678
|
};
|
|
1663
1679
|
};
|
|
1664
1680
|
_proto.setInitialValue = function setInitialValue(path, value) {
|
|
1665
1681
|
var key = JSON.stringify(path);
|
|
1666
|
-
this.
|
|
1682
|
+
this._state[key].initialValue = value;
|
|
1667
1683
|
};
|
|
1668
1684
|
_proto.getInitialValue = function getInitialValue(path) {
|
|
1669
1685
|
var key = JSON.stringify(path);
|
|
1670
|
-
return this.
|
|
1686
|
+
return this._state[key].initialValue;
|
|
1671
1687
|
};
|
|
1672
1688
|
_proto.addListener = function addListener(path, f) {
|
|
1673
1689
|
var key = JSON.stringify(path);
|
|
1674
|
-
this.
|
|
1690
|
+
this._state[key].listeners.push(f);
|
|
1675
1691
|
};
|
|
1676
1692
|
return StateSpecNode;
|
|
1677
1693
|
}();
|
|
@@ -1706,14 +1722,43 @@ function buildTree(specs) {
|
|
|
1706
1722
|
};
|
|
1707
1723
|
return rec([]);
|
|
1708
1724
|
}
|
|
1709
|
-
function
|
|
1725
|
+
function updateTree(root, specs) {
|
|
1726
|
+
var internalSpec = specs.map(function (spec) {
|
|
1727
|
+
return _extends({}, spec, {
|
|
1728
|
+
pathObj: transformPathStringToObj(spec.path),
|
|
1729
|
+
isRepeated: spec.path.split(".").some(function (part) {
|
|
1730
|
+
return part.endsWith("[]");
|
|
1731
|
+
})
|
|
1732
|
+
});
|
|
1733
|
+
});
|
|
1734
|
+
var rec = function rec(oldNode, currentPath) {
|
|
1735
|
+
var nodeSpecs = internalSpec.filter(function (spec) {
|
|
1736
|
+
return shallowEqual(currentPath, spec.pathObj.slice(0, currentPath.length));
|
|
1737
|
+
});
|
|
1738
|
+
var node = oldNode != null ? oldNode : new StateSpecNode(nodeSpecs);
|
|
1739
|
+
node.setSpecs(nodeSpecs);
|
|
1740
|
+
var oldEdges = oldNode == null ? void 0 : oldNode.edges();
|
|
1741
|
+
node.clearEdges();
|
|
1742
|
+
node.getAllSpecs().forEach(function (spec) {
|
|
1743
|
+
if (spec.pathObj.length > currentPath.length) {
|
|
1744
|
+
var nextKey = spec.pathObj[currentPath.length];
|
|
1745
|
+
if (!node.hasEdge(nextKey)) {
|
|
1746
|
+
node.addEdge(nextKey, rec(oldEdges == null ? void 0 : oldEdges.get(nextKey), [].concat(currentPath, [nextKey])));
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
});
|
|
1750
|
+
return node;
|
|
1751
|
+
};
|
|
1752
|
+
return rec(root, []);
|
|
1753
|
+
}
|
|
1754
|
+
function getStateCells(root) {
|
|
1710
1755
|
var leaves = [];
|
|
1711
1756
|
var rec = function rec(node) {
|
|
1712
1757
|
for (var _iterator = _createForOfIteratorHelperLoose(node.children()), _step; !(_step = _iterator()).done;) {
|
|
1713
1758
|
var child = _step.value;
|
|
1714
1759
|
rec(child);
|
|
1715
1760
|
}
|
|
1716
|
-
if (node.isLeaf()) {
|
|
1761
|
+
if (node.isLeaf() && node.getAllSpecs().length > 0) {
|
|
1717
1762
|
leaves.push(node);
|
|
1718
1763
|
}
|
|
1719
1764
|
};
|
|
@@ -1728,7 +1773,6 @@ function findStateCell(root, pathStr, repetitionIndex) {
|
|
|
1728
1773
|
var part = _step2.value;
|
|
1729
1774
|
if (typeof part === "symbol") {
|
|
1730
1775
|
if (!root.hasArrayTransition() || !repetitionIndex || currRepIndex > repetitionIndex.length) {
|
|
1731
|
-
console.log(root);
|
|
1732
1776
|
throw new Error("transition not found: pathStr " + pathStr + " part " + (typeof part === "symbol" ? "[]" : part));
|
|
1733
1777
|
}
|
|
1734
1778
|
realPath.push(repetitionIndex[currRepIndex++]);
|
|
@@ -1783,7 +1827,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
|
|
|
1783
1827
|
set(proxyRoot, initialStatePath, newValue);
|
|
1784
1828
|
});
|
|
1785
1829
|
});
|
|
1786
|
-
var initialValue = initialSpecNode.getState(initialStatePath)
|
|
1830
|
+
var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
|
|
1787
1831
|
initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
|
|
1788
1832
|
var initialSpec = initialSpecNode.getSpec();
|
|
1789
1833
|
var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
|
|
@@ -1796,7 +1840,8 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
|
|
|
1796
1840
|
return initialValue;
|
|
1797
1841
|
}
|
|
1798
1842
|
function create$StateProxy($$state, leafHandlers) {
|
|
1799
|
-
var
|
|
1843
|
+
var proxyRoot;
|
|
1844
|
+
var rec = function rec(currPath, currNode, isOutside, initialObject) {
|
|
1800
1845
|
var getNextPath = function getNextPath(property) {
|
|
1801
1846
|
return [].concat(currPath, [isNum$1(property) ? +property : property]);
|
|
1802
1847
|
};
|
|
@@ -1812,7 +1857,7 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1812
1857
|
//we are always in a leaf, since we only have two cases:
|
|
1813
1858
|
// 1 - delete properties outside the state tree
|
|
1814
1859
|
// 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
|
|
1815
|
-
(_$$state$props$spec$o = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props2, _get(
|
|
1860
|
+
(_$$state$props$spec$o = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props2, _get(proxyRoot, currPath.slice(spec.pathObj.length)));
|
|
1816
1861
|
}
|
|
1817
1862
|
return Reflect.deleteProperty(target, property);
|
|
1818
1863
|
},
|
|
@@ -1820,7 +1865,6 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1820
1865
|
if (property === PLASMIC_STATE_PROXY_SYMBOL) {
|
|
1821
1866
|
return true;
|
|
1822
1867
|
}
|
|
1823
|
-
proxyRoot = proxyRoot == null ? receiver : proxyRoot;
|
|
1824
1868
|
var nextPath = getNextPath(property);
|
|
1825
1869
|
if (isOutside || currNode.isLeaf()) {
|
|
1826
1870
|
return Reflect.get(target, property, receiver);
|
|
@@ -1828,15 +1872,14 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1828
1872
|
var nextNode = currNode.makeTransition(property);
|
|
1829
1873
|
if (nextNode != null && nextNode.isLeaf()) {
|
|
1830
1874
|
var _leafHandlers$get, _leafHandlers;
|
|
1831
|
-
return (_leafHandlers$get = (_leafHandlers = leafHandlers(nextNode, nextPath
|
|
1875
|
+
return (_leafHandlers$get = (_leafHandlers = leafHandlers(nextNode, nextPath)).get) == null ? void 0 : _leafHandlers$get.call(_leafHandlers, target, property, receiver);
|
|
1832
1876
|
} else if (nextNode && !(property in target)) {
|
|
1833
|
-
target[property] = rec(nextPath, nextNode, false,
|
|
1877
|
+
target[property] = rec(nextPath, nextNode, false, undefined);
|
|
1834
1878
|
}
|
|
1835
1879
|
return Reflect.get(target, property, receiver);
|
|
1836
1880
|
},
|
|
1837
1881
|
set: function set$1(target, property, value, receiver) {
|
|
1838
1882
|
var _nextNode, _nextNode2;
|
|
1839
|
-
proxyRoot = proxyRoot == null ? receiver : proxyRoot;
|
|
1840
1883
|
var nextPath = getNextPath(property);
|
|
1841
1884
|
var nextNode = currNode.makeTransition(property);
|
|
1842
1885
|
if (property === "registerInitFunc" && currPath.length === 0) {
|
|
@@ -1849,7 +1892,7 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1849
1892
|
}
|
|
1850
1893
|
if ((_nextNode = nextNode) != null && _nextNode.isLeaf()) {
|
|
1851
1894
|
var _leafHandlers$set, _leafHandlers2;
|
|
1852
|
-
(_leafHandlers$set = (_leafHandlers2 = leafHandlers(nextNode, nextPath
|
|
1895
|
+
(_leafHandlers$set = (_leafHandlers2 = leafHandlers(nextNode, nextPath)).set) == null ? void 0 : _leafHandlers$set.call(_leafHandlers2, target, property, value, receiver);
|
|
1853
1896
|
}
|
|
1854
1897
|
if (!isOutside && !currNode.isLeaf() && !nextNode) {
|
|
1855
1898
|
// can't set an unknown field in $state
|
|
@@ -1861,7 +1904,7 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1861
1904
|
nextNode = currNode;
|
|
1862
1905
|
}
|
|
1863
1906
|
if (canProxy(value)) {
|
|
1864
|
-
target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(),
|
|
1907
|
+
target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
|
|
1865
1908
|
} else if (!isOutside && !currNode.isLeaf() && !((_nextNode2 = nextNode) != null && _nextNode2.isLeaf())) {
|
|
1866
1909
|
throw new Error("inserting a primitive value into a non-leaf");
|
|
1867
1910
|
} else {
|
|
@@ -1880,6 +1923,9 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1880
1923
|
};
|
|
1881
1924
|
var baseObject = !isOutside && !currNode.isLeaf() ? currNode.hasArrayTransition() ? [] : {} : Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject != null ? initialObject : {}));
|
|
1882
1925
|
var proxyObj = new Proxy(baseObject, handlers);
|
|
1926
|
+
if (currPath.length === 0) {
|
|
1927
|
+
proxyRoot = proxyObj;
|
|
1928
|
+
}
|
|
1883
1929
|
if (initialObject) {
|
|
1884
1930
|
Reflect.ownKeys(initialObject).forEach(function (key) {
|
|
1885
1931
|
var desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
@@ -1892,62 +1938,90 @@ function create$StateProxy($$state, leafHandlers) {
|
|
|
1892
1938
|
}
|
|
1893
1939
|
return proxyObj;
|
|
1894
1940
|
};
|
|
1895
|
-
return rec([], $$state.rootSpecTree, false, undefined
|
|
1941
|
+
return rec([], $$state.rootSpecTree, false, undefined);
|
|
1896
1942
|
}
|
|
1897
1943
|
var mkUntrackedValue = function mkUntrackedValue(o) {
|
|
1898
1944
|
return o != null && typeof o === "object" ? valtio.ref(o) : o;
|
|
1899
1945
|
};
|
|
1900
|
-
function useDollarState(specs, props, $ctx) {
|
|
1946
|
+
function useDollarState(specs, props, $ctx, opts) {
|
|
1901
1947
|
var $$state = React__default.useRef(function () {
|
|
1902
1948
|
var rootSpecTree = buildTree(specs);
|
|
1903
1949
|
return {
|
|
1904
1950
|
rootSpecTree: rootSpecTree,
|
|
1905
|
-
specTreeLeaves:
|
|
1951
|
+
specTreeLeaves: getStateCells(rootSpecTree),
|
|
1906
1952
|
stateValues: valtio.proxy({}),
|
|
1907
1953
|
props: {},
|
|
1908
1954
|
ctx: {},
|
|
1909
|
-
|
|
1955
|
+
specs: [],
|
|
1956
|
+
registrationsQueue: valtio.proxy([])
|
|
1910
1957
|
};
|
|
1911
1958
|
}()).current;
|
|
1912
1959
|
$$state.props = props;
|
|
1913
1960
|
$$state.ctx = $ctx != null ? $ctx : {};
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
initializeStateValue($$state, node, path, proxyRoot);
|
|
1920
|
-
} else if (!spec.valueProp) {
|
|
1921
|
-
set(proxyRoot, path, spec.initVal);
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
return {
|
|
1925
|
-
get: function get(target, property, receiver) {
|
|
1961
|
+
$$state.specs = specs;
|
|
1962
|
+
var create$State = function create$State() {
|
|
1963
|
+
var $state = Object.assign(create$StateProxy($$state, function (node, path) {
|
|
1964
|
+
if (!node.hasState(path)) {
|
|
1965
|
+
node.createStateCell(path);
|
|
1926
1966
|
var spec = node.getSpec();
|
|
1927
|
-
if (spec.
|
|
1928
|
-
|
|
1929
|
-
} else {
|
|
1930
|
-
|
|
1967
|
+
if (spec.initFunc) {
|
|
1968
|
+
initializeStateValue($$state, node, path, $state);
|
|
1969
|
+
} else if (!spec.valueProp) {
|
|
1970
|
+
set($state, path, spec.initVal);
|
|
1931
1971
|
}
|
|
1932
1972
|
}
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1973
|
+
return {
|
|
1974
|
+
get: function get(target, property, receiver) {
|
|
1975
|
+
var spec = node.getSpec();
|
|
1976
|
+
if (spec.valueProp) {
|
|
1977
|
+
return $$state.props[spec.valueProp];
|
|
1978
|
+
} else {
|
|
1979
|
+
return Reflect.get(target, property, receiver);
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
};
|
|
1983
|
+
}), {
|
|
1984
|
+
registerInitFunc: function registerInitFunc(pathStr, f, repetitionIndex) {
|
|
1985
|
+
var _findStateCell = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex),
|
|
1986
|
+
node = _findStateCell.node,
|
|
1987
|
+
realPath = _findStateCell.realPath;
|
|
1988
|
+
if (!node.hasState(realPath)) {
|
|
1989
|
+
node.createStateCell(realPath);
|
|
1990
|
+
}
|
|
1991
|
+
if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
|
|
1992
|
+
$$state.registrationsQueue.push(mkUntrackedValue({
|
|
1993
|
+
node: node,
|
|
1994
|
+
path: realPath,
|
|
1995
|
+
f: f
|
|
1996
|
+
}));
|
|
1997
|
+
}
|
|
1941
1998
|
}
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1999
|
+
});
|
|
2000
|
+
return $state;
|
|
2001
|
+
};
|
|
2002
|
+
var ref = React__default.useRef(undefined);
|
|
2003
|
+
if (!ref.current) {
|
|
2004
|
+
ref.current = create$State();
|
|
2005
|
+
}
|
|
2006
|
+
var $state = ref.current;
|
|
2007
|
+
if (opts != null && opts.inCanvas) {
|
|
2008
|
+
$$state.rootSpecTree = updateTree($$state.rootSpecTree, specs);
|
|
2009
|
+
var newLeaves = getStateCells($$state.rootSpecTree);
|
|
2010
|
+
if (!arrayEq(newLeaves, $$state.specTreeLeaves)) {
|
|
2011
|
+
$state = ref.current = create$State();
|
|
2012
|
+
$$state.specTreeLeaves = newLeaves;
|
|
2013
|
+
}
|
|
2014
|
+
// we need to eager initialize all states in canvas to populate the data picker
|
|
2015
|
+
$$state.specTreeLeaves.forEach(function (node) {
|
|
2016
|
+
var spec = node.getSpec();
|
|
2017
|
+
if (spec.isRepeated || node.hasState(spec.pathObj)) {
|
|
2018
|
+
return;
|
|
1948
2019
|
}
|
|
1949
|
-
|
|
1950
|
-
|
|
2020
|
+
node.createStateCell(spec.pathObj);
|
|
2021
|
+
var init = spec.valueProp ? $$state.props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
|
|
2022
|
+
set($state, spec.pathObj, init);
|
|
2023
|
+
});
|
|
2024
|
+
}
|
|
1951
2025
|
// For each spec with an initFunc, evaluate it and see if
|
|
1952
2026
|
// the init value has changed. If so, reset its state.
|
|
1953
2027
|
var resetSpecs = [];
|
|
@@ -1959,10 +2033,11 @@ function useDollarState(specs, props, $ctx) {
|
|
|
1959
2033
|
};
|
|
1960
2034
|
});
|
|
1961
2035
|
}).forEach(function (_ref2) {
|
|
1962
|
-
var
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
2036
|
+
var node = _ref2.node,
|
|
2037
|
+
stateCell = _ref2.stateCell;
|
|
2038
|
+
var initFunc = node.getInitFunc(stateCell);
|
|
2039
|
+
if (initFunc) {
|
|
2040
|
+
var newInit = initFunc(props, $state, $ctx != null ? $ctx : {});
|
|
1966
2041
|
if (!deepEqual(newInit, stateCell.initialValue)) {
|
|
1967
2042
|
resetSpecs.push({
|
|
1968
2043
|
stateCell: stateCell,
|
|
@@ -1987,16 +2062,16 @@ function useDollarState(specs, props, $ctx) {
|
|
|
1987
2062
|
});
|
|
1988
2063
|
}, [props, resetSpecs]);
|
|
1989
2064
|
useIsomorphicLayoutEffect$1(function () {
|
|
1990
|
-
$$state.registrationsQueue.
|
|
1991
|
-
var
|
|
1992
|
-
|
|
1993
|
-
|
|
2065
|
+
while ($$state.registrationsQueue.length) {
|
|
2066
|
+
var _$$state$registration = $$state.registrationsQueue.shift(),
|
|
2067
|
+
node = _$$state$registration.node,
|
|
2068
|
+
path = _$$state$registration.path,
|
|
2069
|
+
f = _$$state$registration.f;
|
|
1994
2070
|
var stateCell = node.getState(path);
|
|
1995
2071
|
stateCell.registeredInitFunc = f;
|
|
1996
2072
|
reInitializeState(node, stateCell);
|
|
1997
|
-
}
|
|
1998
|
-
|
|
1999
|
-
}, [$$state.registrationsQueue]);
|
|
2073
|
+
}
|
|
2074
|
+
}, [$$state.registrationsQueue.length]);
|
|
2000
2075
|
// immediately initialize exposed non-private states
|
|
2001
2076
|
useIsomorphicLayoutEffect$1(function () {
|
|
2002
2077
|
$$state.specTreeLeaves.forEach(function (node) {
|
|
@@ -2011,6 +2086,7 @@ function useDollarState(specs, props, $ctx) {
|
|
|
2011
2086
|
valtio.useSnapshot($$state.stateValues, {
|
|
2012
2087
|
sync: true
|
|
2013
2088
|
});
|
|
2089
|
+
valtio.useSnapshot($$state.registrationsQueue);
|
|
2014
2090
|
return $state;
|
|
2015
2091
|
}
|
|
2016
2092
|
|