@plasmicapp/react-web 0.2.170 → 0.2.172

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/all.d.ts +6 -2
  2. package/dist/react-web.cjs.development.js +288 -269
  3. package/dist/react-web.cjs.development.js.map +1 -1
  4. package/dist/react-web.cjs.production.min.js +1 -1
  5. package/dist/react-web.cjs.production.min.js.map +1 -1
  6. package/dist/react-web.esm.js +288 -271
  7. package/dist/react-web.esm.js.map +1 -1
  8. package/dist/states/graph.d.ts +2 -20
  9. package/dist/states/helpers.d.ts +5 -1
  10. package/dist/states/index.d.ts +1 -1
  11. package/dist/states/types.d.ts +26 -1
  12. package/dist/states/valtio.d.ts +4 -1
  13. package/dist/stories/UseDollarState.stories.d.ts +1 -0
  14. package/package.json +4 -4
  15. package/skinny/dist/{collection-utils-3496fd68.js → collection-utils-719bb030.js} +4 -4
  16. package/skinny/dist/{collection-utils-3496fd68.js.map → collection-utils-719bb030.js.map} +1 -1
  17. package/skinny/dist/{common-c2744215.js → common-dd6b46fb.js} +11 -2
  18. package/skinny/dist/{common-c2744215.js.map → common-dd6b46fb.js.map} +1 -1
  19. package/skinny/dist/index.js +279 -254
  20. package/skinny/dist/index.js.map +1 -1
  21. package/skinny/dist/plume/button/index.js +2 -2
  22. package/skinny/dist/plume/checkbox/index.js +4 -4
  23. package/skinny/dist/plume/menu/index.js +5 -5
  24. package/skinny/dist/plume/menu-button/index.js +4 -4
  25. package/skinny/dist/plume/select/index.js +5 -5
  26. package/skinny/dist/plume/switch/index.js +4 -4
  27. package/skinny/dist/plume/text-input/index.js +2 -2
  28. package/skinny/dist/plume/triggered-overlay/index.js +4 -4
  29. package/skinny/dist/{plume-utils-653f4b8a.js → plume-utils-43df92e6.js} +2 -2
  30. package/skinny/dist/{plume-utils-653f4b8a.js.map → plume-utils-43df92e6.js.map} +1 -1
  31. package/skinny/dist/{props-utils-70c2d02a.js → props-utils-eaa18f66.js} +2 -2
  32. package/skinny/dist/{props-utils-70c2d02a.js.map → props-utils-eaa18f66.js.map} +1 -1
  33. package/skinny/dist/{react-utils-76d05959.js → react-utils-628465ba.js} +2 -2
  34. package/skinny/dist/{react-utils-76d05959.js.map → react-utils-628465ba.js.map} +1 -1
  35. package/skinny/dist/render/PlasmicImg/index.js +2 -2
  36. package/skinny/dist/states/graph.d.ts +2 -20
  37. package/skinny/dist/states/helpers.d.ts +5 -1
  38. package/skinny/dist/states/index.d.ts +1 -1
  39. package/skinny/dist/states/types.d.ts +26 -1
  40. package/skinny/dist/states/valtio.d.ts +4 -1
  41. package/skinny/dist/stories/UseDollarState.stories.d.ts +1 -0
@@ -964,6 +964,14 @@ function chainSingleArgFuncs() {
964
964
  return res;
965
965
  };
966
966
  }
967
+ function ensure(x) {
968
+ if (x === null || x === undefined) {
969
+ debugger;
970
+ throw new Error("Value must not be undefined or null");
971
+ } else {
972
+ return x;
973
+ }
974
+ }
967
975
  function isString(x) {
968
976
  return typeof x === "string";
969
977
  }
@@ -2300,13 +2308,12 @@ function useTrigger(trigger, opts) {
2300
2308
 
2301
2309
  var ARRAY_SYMBOL = /*#__PURE__*/Symbol("[]");
2302
2310
  var PLASMIC_STATE_PROXY_SYMBOL = /*#__PURE__*/Symbol("plasmic.state.proxy");
2303
-
2304
2311
  var UNINITIALIZED = /*#__PURE__*/Symbol("plasmic.unitialized");
2312
+
2305
2313
  var StateSpecNode = /*#__PURE__*/function () {
2306
2314
  function StateSpecNode(specs) {
2307
2315
  this._specs = specs;
2308
2316
  this._edges = new Map();
2309
- this._state = {};
2310
2317
  }
2311
2318
  var _proto = StateSpecNode.prototype;
2312
2319
  _proto.setSpecs = function setSpecs(specs) {
@@ -2315,9 +2322,6 @@ var StateSpecNode = /*#__PURE__*/function () {
2315
2322
  _proto.edges = function edges() {
2316
2323
  return this._edges;
2317
2324
  };
2318
- _proto.state = function state() {
2319
- return this._state;
2320
- };
2321
2325
  _proto.hasEdge = function hasEdge(key) {
2322
2326
  return this._edges.has(key);
2323
2327
  };
@@ -2331,11 +2335,11 @@ var StateSpecNode = /*#__PURE__*/function () {
2331
2335
  return this._edges.values();
2332
2336
  };
2333
2337
  _proto.makeTransition = function makeTransition(key) {
2334
- key = isNum(key) ? ARRAY_SYMBOL : key;
2338
+ key = isNum$1(key) ? ARRAY_SYMBOL : key;
2335
2339
  return this._edges.get(key);
2336
2340
  };
2337
2341
  _proto.isLeaf = function isLeaf() {
2338
- return this._edges.size === 0;
2342
+ return this._edges.size === 0 && this.getAllSpecs().length > 0;
2339
2343
  };
2340
2344
  _proto.hasArrayTransition = function hasArrayTransition() {
2341
2345
  return this._edges.has(ARRAY_SYMBOL);
@@ -2346,52 +2350,6 @@ var StateSpecNode = /*#__PURE__*/function () {
2346
2350
  _proto.getAllSpecs = function getAllSpecs() {
2347
2351
  return this._specs;
2348
2352
  };
2349
- _proto.getState = function getState(path) {
2350
- return this._state[JSON.stringify(path)];
2351
- };
2352
- _proto.getInitFunc = function getInitFunc(stateCell) {
2353
- var _stateCell$registered;
2354
- return (_stateCell$registered = stateCell.registeredInitFunc) != null ? _stateCell$registered : this.getSpec().initFunc;
2355
- };
2356
- _proto.clearStates = function clearStates() {
2357
- this._state = {};
2358
- };
2359
- _proto.states = function states() {
2360
- return Object.values(this._state);
2361
- };
2362
- _proto.hasState = function hasState(path) {
2363
- var key = JSON.stringify(path);
2364
- return key in this._state;
2365
- };
2366
- _proto.createStateCell = function createStateCell(path) {
2367
- var key = JSON.stringify(path);
2368
- this._state[key] = {
2369
- listeners: [],
2370
- initialValue: UNINITIALIZED,
2371
- path: path
2372
- };
2373
- };
2374
- _proto.deleteStateCell = function deleteStateCell(prefixPath) {
2375
- var _this = this;
2376
- [].concat(Object.keys(this._state)).forEach(function (key) {
2377
- var path = JSON.parse(key);
2378
- if (shallowEqual(prefixPath, path.slice(0, prefixPath.length))) {
2379
- delete _this._state[key];
2380
- }
2381
- });
2382
- };
2383
- _proto.setInitialValue = function setInitialValue(path, value) {
2384
- var key = JSON.stringify(path);
2385
- this._state[key].initialValue = value;
2386
- };
2387
- _proto.getInitialValue = function getInitialValue(path) {
2388
- var key = JSON.stringify(path);
2389
- return this._state[key].initialValue;
2390
- };
2391
- _proto.addListener = function addListener(path, f) {
2392
- var key = JSON.stringify(path);
2393
- this._state[key].listeners.push(f);
2394
- };
2395
2353
  return StateSpecNode;
2396
2354
  }();
2397
2355
  var transformPathStringToObj = function transformPathStringToObj(str) {
@@ -2454,7 +2412,7 @@ function updateTree(root, specs) {
2454
2412
  };
2455
2413
  return rec(root, []);
2456
2414
  }
2457
- function getStateCells(root) {
2415
+ function getSpecTreeLeaves(root) {
2458
2416
  var leaves = [];
2459
2417
  var rec = function rec(node) {
2460
2418
  for (var _iterator = _createForOfIteratorHelperLoose(node.children()), _step; !(_step = _iterator()).done;) {
@@ -2494,156 +2452,63 @@ function findStateCell(root, pathStr, repetitionIndex) {
2494
2452
  };
2495
2453
  }
2496
2454
 
2497
- function generateStateOnChangeProp($state, path) {
2498
- return function (val) {
2499
- return set($state, path, val);
2500
- };
2501
- }
2502
- function generateStateValueProp($state, path) {
2503
- return _get($state, path);
2504
- }
2505
- var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
2506
- function isPlasmicStateProxy(obj) {
2507
- return obj != null && typeof obj === "object" && !!obj[PLASMIC_STATE_PROXY_SYMBOL];
2508
- }
2509
- function getStateCellsInPlasmicProxy(obj) {
2510
- if (!isPlasmicStateProxy(obj)) {
2511
- return [];
2512
- }
2513
- var _obj$PLASMIC_STATE_PR = obj[PLASMIC_STATE_PROXY_SYMBOL],
2514
- rootNode = _obj$PLASMIC_STATE_PR.node,
2515
- rootPath = _obj$PLASMIC_STATE_PR.path,
2516
- isOutside = _obj$PLASMIC_STATE_PR.isOutside;
2517
- if (isOutside) {
2518
- return [];
2519
- }
2520
- return getStateCells(rootNode).flatMap(function (node) {
2521
- return node.states().map(function (stateCell) {
2522
- return {
2523
- path: node.getSpec().path,
2524
- realPath: stateCell.path.slice(rootPath.length)
2525
- };
2526
- });
2527
- });
2528
- }
2529
- function getStateSpecInPlasmicProxy(obj, path) {
2530
- obj = _get(obj, path.slice(0, path.length - 1));
2531
- if (!isPlasmicStateProxy(obj)) {
2532
- return undefined;
2533
- }
2534
- var _obj$PLASMIC_STATE_PR2 = obj[PLASMIC_STATE_PROXY_SYMBOL],
2535
- node = _obj$PLASMIC_STATE_PR2.node,
2536
- isOutside = _obj$PLASMIC_STATE_PR2.isOutside;
2537
- var nextNode = node.makeTransition(path[path.length - 1]);
2538
- if (isOutside || node.isLeaf() || !nextNode) {
2539
- return undefined;
2540
- }
2541
- return {
2542
- spec: nextNode.getSpec(),
2543
- isImplicitStateArray: nextNode.hasArrayTransition()
2544
- };
2545
- }
2546
- function shallowEqual(a1, a2) {
2547
- if (a1.length !== a2.length) {
2548
- return false;
2549
- }
2550
- for (var i = 0; i < a1.length; i++) {
2551
- if (a1[i] !== a2[i]) {
2552
- return false;
2553
- }
2554
- }
2555
- return true;
2556
- }
2557
- /**
2558
- * Shallow comparison of arrays.
2559
- */
2560
- function arrayEq(xs, ys) {
2561
- return xs.length === ys.length && xs.every(function (_, index) {
2562
- return xs[index] === ys[index];
2563
- });
2564
- }
2565
2455
  function isNum(value) {
2566
2456
  return typeof value === "symbol" ? false : !isNaN(+value);
2567
2457
  }
2568
- function assert(cond, msg) {
2569
- if (msg === void 0) {
2570
- msg = "Assertion failed";
2571
- }
2572
- if (!cond) {
2573
- // We always generate an non empty message so that it doesn't get swallowed
2574
- // by the async library.
2575
- msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
2576
- debugger;
2577
- throw new Error(msg);
2578
- }
2458
+ function canProxy(value) {
2459
+ return typeof value === "object" && value != null;
2579
2460
  }
2580
- /**
2581
- * Forked from https://github.com/lukeed/dset
2582
- * Changes: fixed setting a deep value to a proxy object
2583
- */
2584
- function set(obj, keys, val) {
2585
- keys = keys.split ? keys.split(".") : keys;
2586
- var i = 0,
2587
- l = keys.length,
2588
- t = obj,
2589
- x,
2590
- k;
2591
- while (i < l) {
2592
- k = keys[i++];
2593
- if (k === "__proto__" || k === "constructor" || k === "prototype") break;
2594
- var newValue = i === l ? val : typeof (x = t[k]) === typeof keys ? x : keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".") ? {} : [];
2595
- assignValue(t, k, newValue);
2596
- t = t[k];
2461
+ var proxyObjToStateCell = /*#__PURE__*/new WeakMap();
2462
+ function ensureStateCell(target, property, path, node) {
2463
+ if (!proxyObjToStateCell.has(target)) {
2464
+ proxyObjToStateCell.set(target, {});
2597
2465
  }
2598
- }
2599
- /**
2600
- * Forked from lodash
2601
- */
2602
- function baseAssignValue(object, key, value) {
2603
- if (key == "__proto__") {
2604
- Object.defineProperty(object, key, {
2605
- configurable: true,
2606
- enumerable: true,
2607
- value: value,
2608
- writable: true
2609
- });
2610
- } else {
2611
- object[key] = value;
2466
+ var stateCell = proxyObjToStateCell.get(target);
2467
+ if (!(property in stateCell)) {
2468
+ var _node$getSpec$initFun;
2469
+ stateCell[property] = {
2470
+ listeners: [],
2471
+ initialValue: UNINITIALIZED,
2472
+ path: path,
2473
+ node: node,
2474
+ initFunc: node.getSpec().initFunc,
2475
+ initFuncHash: (_node$getSpec$initFun = node.getSpec().initFuncHash) != null ? _node$getSpec$initFun : ""
2476
+ };
2612
2477
  }
2478
+ return stateCell[property];
2613
2479
  }
2614
- function eq(value, other) {
2615
- return value === other || value !== value && other !== other;
2480
+ function getStateCell(target, property) {
2481
+ var _proxyObjToStateCell$;
2482
+ return (_proxyObjToStateCell$ = proxyObjToStateCell.get(target)) == null ? void 0 : _proxyObjToStateCell$[property];
2616
2483
  }
2617
- function assignValue(object, key, value) {
2618
- var objValue = object[key];
2619
- if (!(Object.prototype.hasOwnProperty.call(object, key) && eq(objValue, value)) || value === undefined && !(key in object)) {
2620
- baseAssignValue(object, key, value);
2484
+ function tryGetStateCellFrom$StateRoot($state, path) {
2485
+ if (path.length === 0) {
2486
+ throw new Error("expected a path with length greater than 0");
2621
2487
  }
2488
+ var target = _get($state, path.slice(0, -1));
2489
+ _get(target, path.slice(-1)); // create state cell;
2490
+ return getStateCell(target, path.slice(-1)[0]);
2622
2491
  }
2623
-
2624
- function isNum$1(value) {
2625
- return typeof value === "symbol" ? false : !isNaN(+value);
2626
- }
2627
- function canProxy(value) {
2628
- return typeof value === "object" && value != null;
2492
+ function getStateCellFrom$StateRoot($state, path) {
2493
+ return ensure(tryGetStateCellFrom$StateRoot($state, path));
2629
2494
  }
2630
- function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyRoot) {
2495
+ function initializeStateValue($$state, initialStateCell, proxyRoot) {
2496
+ var _initialStateCell$ove2;
2631
2497
  var stateAccess = new Set();
2632
- var $state = create$StateProxy($$state, function (node, path) {
2498
+ var $state = create$StateProxy($$state, function (internalStateCell) {
2633
2499
  return {
2634
2500
  get: function get() {
2501
+ var stateCell = getStateCellFrom$StateRoot(proxyRoot, internalStateCell.path);
2635
2502
  stateAccess.add({
2636
- path: path,
2637
- node: node
2503
+ stateCell: stateCell
2638
2504
  });
2639
- var spec = node.getSpec();
2505
+ var spec = stateCell.node.getSpec();
2640
2506
  if (spec.valueProp) {
2641
2507
  return $$state.env.$props[spec.valueProp];
2642
- } else if (!node.hasState(path) && spec.initFunc) {
2643
- node.createStateCell(path);
2644
- return initializeStateValue($$state, node, path, proxyRoot);
2508
+ } else if (spec.initFunc && stateCell.initialValue === UNINITIALIZED) {
2509
+ return initializeStateValue($$state, stateCell, proxyRoot);
2645
2510
  }
2646
- return _get(proxyRoot, path);
2511
+ return _get(proxyRoot, stateCell.path);
2647
2512
  },
2648
2513
  set: function set() {
2649
2514
  throw new Error("Cannot update state values during initialization");
@@ -2651,23 +2516,22 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
2651
2516
  };
2652
2517
  });
2653
2518
  stateAccess.forEach(function (_ref) {
2654
- var node = _ref.node,
2655
- path = _ref.path;
2656
- node.addListener(path, function () {
2657
- var newValue = invokeInitFuncBackwardsCompatible(initialSpecNode.getSpec().initFunc, _extends({
2519
+ var stateCell = _ref.stateCell;
2520
+ stateCell.listeners.push(function () {
2521
+ var _initialStateCell$ove;
2522
+ var newValue = invokeInitFuncBackwardsCompatible(initialStateCell.node.getSpec().initFunc, _extends({
2658
2523
  $state: $state
2659
- }, $$state.env));
2660
- set(proxyRoot, initialStatePath, newValue);
2524
+ }, (_initialStateCell$ove = initialStateCell.overrideEnv) != null ? _initialStateCell$ove : $$state.env));
2525
+ set(proxyRoot, initialStateCell.path, newValue);
2661
2526
  });
2662
2527
  });
2663
- var initFunc = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath));
2664
- var initialValue = invokeInitFuncBackwardsCompatible(initFunc, _extends({
2528
+ var initialValue = invokeInitFuncBackwardsCompatible(initialStateCell.initFunc, _extends({
2665
2529
  $state: $state
2666
- }, $$state.env));
2667
- initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
2668
- var initialSpec = initialSpecNode.getSpec();
2530
+ }, (_initialStateCell$ove2 = initialStateCell.overrideEnv) != null ? _initialStateCell$ove2 : $$state.env));
2531
+ initialStateCell.initialValue = clone(initialValue);
2532
+ var initialSpec = initialStateCell.node.getSpec();
2669
2533
  var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
2670
- set(proxyRoot, initialStatePath, value);
2534
+ set(proxyRoot, initialStateCell.path, value);
2671
2535
  //immediately fire onChange
2672
2536
  if (initialSpec.onChangeProp) {
2673
2537
  var _$$state$env$$props$i, _$$state$env$$props;
@@ -2679,12 +2543,12 @@ function create$StateProxy($$state, leafHandlers) {
2679
2543
  var proxyRoot;
2680
2544
  var rec = function rec(currPath, currNode, isOutside, initialObject) {
2681
2545
  var getNextPath = function getNextPath(property) {
2682
- return [].concat(currPath, [isNum$1(property) ? +property : property]);
2546
+ return [].concat(currPath, [isNum(property) ? +property : property]);
2683
2547
  };
2684
2548
  var spec = currNode.getSpec();
2685
2549
  var handlers = {
2686
2550
  deleteProperty: function deleteProperty(target, property) {
2687
- if (!isOutside && !currNode.isLeaf() && !currNode.hasArrayTransition() && !isNum$1(property)) {
2551
+ if (!isOutside && !currNode.isLeaf() && !currNode.hasArrayTransition() && !isNum(property)) {
2688
2552
  throw new Error("Can't delete a property in the middle of the state spec");
2689
2553
  }
2690
2554
  delete _get($$state.stateValues, currPath)[property];
@@ -2695,13 +2559,6 @@ function create$StateProxy($$state, leafHandlers) {
2695
2559
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
2696
2560
  (_$$state$env$$props$s = (_$$state$env$$props2 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s.call(_$$state$env$$props2, _get(proxyRoot, currPath.slice(spec.pathObj.length)));
2697
2561
  }
2698
- var nextPath = getNextPath(property);
2699
- var nextNode = currNode.makeTransition(property);
2700
- if (nextNode) {
2701
- getStateCells(nextNode).forEach(function (node) {
2702
- node.deleteStateCell(nextPath);
2703
- });
2704
- }
2705
2562
  return Reflect.deleteProperty(target, property);
2706
2563
  },
2707
2564
  get: function get(target, property, receiver) {
@@ -2719,7 +2576,7 @@ function create$StateProxy($$state, leafHandlers) {
2719
2576
  var nextNode = currNode.makeTransition(property);
2720
2577
  if (nextNode != null && nextNode.isLeaf()) {
2721
2578
  var _leafHandlers$get, _leafHandlers;
2722
- return (_leafHandlers$get = (_leafHandlers = leafHandlers(nextNode, nextPath)).get) == null ? void 0 : _leafHandlers$get.call(_leafHandlers, target, property, receiver);
2579
+ return (_leafHandlers$get = (_leafHandlers = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).get) == null ? void 0 : _leafHandlers$get.call(_leafHandlers, target, property, receiver);
2723
2580
  } else if (nextNode && !(property in target)) {
2724
2581
  target[property] = rec(nextPath, nextNode, false, undefined);
2725
2582
  }
@@ -2739,7 +2596,7 @@ function create$StateProxy($$state, leafHandlers) {
2739
2596
  }
2740
2597
  if ((_nextNode = nextNode) != null && _nextNode.isLeaf()) {
2741
2598
  var _leafHandlers$set, _leafHandlers2;
2742
- (_leafHandlers$set = (_leafHandlers2 = leafHandlers(nextNode, nextPath)).set) == null ? void 0 : _leafHandlers$set.call(_leafHandlers2, target, property, value, receiver);
2599
+ (_leafHandlers$set = (_leafHandlers2 = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).set) == null ? void 0 : _leafHandlers$set.call(_leafHandlers2, target, property, value, receiver);
2743
2600
  }
2744
2601
  if (!isOutside && !currNode.isLeaf() && !nextNode) {
2745
2602
  // can't set an unknown field in $state
@@ -2855,28 +2712,27 @@ function useDollarState(specs) {
2855
2712
  var rootSpecTree = buildTree(specs);
2856
2713
  return {
2857
2714
  rootSpecTree: rootSpecTree,
2858
- specTreeLeaves: getStateCells(rootSpecTree),
2715
+ specTreeLeaves: getSpecTreeLeaves(rootSpecTree),
2859
2716
  stateValues: valtio.proxy({}),
2860
2717
  env: envFieldsAreNonNill(env),
2861
2718
  specs: [],
2862
2719
  registrationsQueue: valtio.proxy([])
2863
2720
  };
2864
2721
  }()).current;
2865
- $$state.env = envFieldsAreNonNill(env), $$state.specs = specs;
2866
- var create$State = function create$State() {
2867
- var $state = Object.assign(create$StateProxy($$state, function (node, path) {
2868
- if (!node.hasState(path)) {
2869
- node.createStateCell(path);
2870
- var spec = node.getSpec();
2871
- if (spec.initFunc) {
2872
- initializeStateValue($$state, node, path, $state);
2873
- } else if (!spec.valueProp) {
2874
- set($state, path, spec.initVal);
2875
- }
2722
+ $$state.env = envFieldsAreNonNill(env);
2723
+ $$state.specs = specs;
2724
+ var create$State = React__default.useCallback(function () {
2725
+ var $state = Object.assign(create$StateProxy($$state, function (stateCell) {
2726
+ var spec = stateCell.node.getSpec();
2727
+ if (stateCell.initialValue === UNINITIALIZED && spec.initFunc) {
2728
+ initializeStateValue($$state, stateCell, $state);
2729
+ } else if (stateCell.initialValue === UNINITIALIZED && !spec.valueProp) {
2730
+ stateCell.initialValue = spec.initVal;
2731
+ set($state, stateCell.path, spec.initVal);
2876
2732
  }
2877
2733
  return {
2878
2734
  get: function get(target, property, receiver) {
2879
- var spec = node.getSpec();
2735
+ var spec = stateCell.node.getSpec();
2880
2736
  if (spec.valueProp) {
2881
2737
  return $$state.env.$props[spec.valueProp];
2882
2738
  } else {
@@ -2885,26 +2741,26 @@ function useDollarState(specs) {
2885
2741
  }
2886
2742
  };
2887
2743
  }), {
2888
- registerInitFunc: function registerInitFunc(pathStr, f, repetitionIndex) {
2744
+ registerInitFunc: function registerInitFunc(pathStr, f, repetitionIndex, overrideEnv) {
2889
2745
  var _findStateCell = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex),
2890
2746
  node = _findStateCell.node,
2891
2747
  realPath = _findStateCell.realPath;
2892
- if (!node.hasState(realPath)) {
2893
- node.createStateCell(realPath);
2894
- }
2895
- if (!deepEqual(node.getState(realPath).initialValue, f(_extends({
2748
+ var stateCell = getStateCellFrom$StateRoot($state, realPath);
2749
+ var env = overrideEnv ? envFieldsAreNonNill(overrideEnv) : $$state.env;
2750
+ if (!deepEqual(stateCell.initialValue, f(_extends({
2896
2751
  $state: $state
2897
- }, $$state.env)))) {
2752
+ }, env)))) {
2898
2753
  $$state.registrationsQueue.push(mkUntrackedValue({
2899
2754
  node: node,
2900
2755
  path: realPath,
2901
- f: f
2756
+ f: f,
2757
+ overrideEnv: overrideEnv ? envFieldsAreNonNill(overrideEnv) : undefined
2902
2758
  }));
2903
2759
  }
2904
2760
  }
2905
2761
  });
2906
2762
  return $state;
2907
- };
2763
+ }, []);
2908
2764
  var ref = React__default.useRef(undefined);
2909
2765
  if (!ref.current) {
2910
2766
  ref.current = create$State();
@@ -2912,79 +2768,79 @@ function useDollarState(specs) {
2912
2768
  var $state = ref.current;
2913
2769
  if (opts != null && opts.inCanvas) {
2914
2770
  $$state.rootSpecTree = updateTree($$state.rootSpecTree, specs);
2915
- var newLeaves = getStateCells($$state.rootSpecTree);
2771
+ var newLeaves = getSpecTreeLeaves($$state.rootSpecTree);
2916
2772
  if (!arrayEq(newLeaves, $$state.specTreeLeaves)) {
2917
- var old$State = $state;
2773
+ var old$state = $state;
2918
2774
  $state = ref.current = create$State();
2919
2775
  $$state.specTreeLeaves = newLeaves;
2920
- $$state.specTreeLeaves.flatMap(function (node) {
2921
- return node.states();
2922
- }).forEach(function (_ref2) {
2776
+ getStateCells(newLeaves, $$state.rootSpecTree).forEach(function (_ref2) {
2923
2777
  var path = _ref2.path;
2924
- set($state, path, _get(old$State, path));
2778
+ var oldStateCell = tryGetStateCellFrom$StateRoot(old$state, path);
2779
+ if (oldStateCell) {
2780
+ set($state, path, _get(old$state, path));
2781
+ }
2925
2782
  });
2926
2783
  }
2927
2784
  // we need to eager initialize all states in canvas to populate the data picker
2928
2785
  $$state.specTreeLeaves.forEach(function (node) {
2786
+ var _newSpec$initFuncHash;
2929
2787
  var spec = node.getSpec();
2930
- if (spec.isRepeated || node.hasState(spec.pathObj)) {
2788
+ if (spec.isRepeated) {
2931
2789
  return;
2932
2790
  }
2933
- node.createStateCell(spec.pathObj);
2934
- var init = spec.valueProp ? $$state.env.$props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
2791
+ var stateCell = getStateCellFrom$StateRoot($state, spec.pathObj);
2792
+ var newSpec = specs.find(function (sp) {
2793
+ return sp.path === spec.path;
2794
+ });
2795
+ if (!newSpec || stateCell.initFuncHash === (newSpec == null ? void 0 : newSpec.initFuncHash)) {
2796
+ return;
2797
+ }
2798
+ stateCell.initFunc = newSpec.initFunc;
2799
+ stateCell.initFuncHash = (_newSpec$initFuncHash = newSpec.initFuncHash) != null ? _newSpec$initFuncHash : "";
2800
+ var init = spec.valueProp ? $$state.env.$props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, stateCell, $state) : spec.initVal;
2935
2801
  set($state, spec.pathObj, init);
2936
2802
  });
2937
2803
  }
2938
2804
  // For each spec with an initFunc, evaluate it and see if
2939
2805
  // the init value has changed. If so, reset its state.
2940
2806
  var resetSpecs = [];
2941
- $$state.specTreeLeaves.flatMap(function (node) {
2942
- return node.states().map(function (stateCell) {
2943
- return {
2944
- stateCell: stateCell,
2945
- node: node
2946
- };
2947
- });
2948
- }).forEach(function (_ref3) {
2949
- var node = _ref3.node,
2950
- stateCell = _ref3.stateCell;
2951
- var initFunc = node.getInitFunc(stateCell);
2952
- if (initFunc) {
2953
- var newInit = invokeInitFuncBackwardsCompatible(initFunc, _extends({
2807
+ getStateCells($state, $$state.rootSpecTree).forEach(function (stateCell) {
2808
+ if (stateCell.initFunc) {
2809
+ var _stateCell$overrideEn;
2810
+ var newInit = invokeInitFuncBackwardsCompatible(stateCell.initFunc, _extends({
2954
2811
  $state: $state
2955
- }, envFieldsAreNonNill(env)));
2812
+ }, (_stateCell$overrideEn = stateCell.overrideEnv) != null ? _stateCell$overrideEn : envFieldsAreNonNill(env)));
2956
2813
  if (!deepEqual(newInit, stateCell.initialValue)) {
2957
2814
  resetSpecs.push({
2958
- stateCell: stateCell,
2959
- node: node
2815
+ stateCell: stateCell
2960
2816
  });
2961
2817
  }
2962
2818
  }
2963
2819
  });
2964
- var reInitializeState = function reInitializeState(node, stateCell) {
2965
- var newInit = initializeStateValue($$state, node, stateCell.path, $state);
2966
- var spec = node.getSpec();
2820
+ var reInitializeState = function reInitializeState(stateCell) {
2821
+ var newInit = initializeStateValue($$state, stateCell, $state);
2822
+ var spec = stateCell.node.getSpec();
2967
2823
  if (spec.onChangeProp) {
2968
2824
  var _$$state$env$$props$s4, _$$state$env$$props5;
2969
2825
  (_$$state$env$$props$s4 = (_$$state$env$$props5 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s4.call(_$$state$env$$props5, newInit);
2970
2826
  }
2971
2827
  };
2972
2828
  useIsomorphicLayoutEffect$1(function () {
2973
- resetSpecs.forEach(function (_ref4) {
2974
- var stateCell = _ref4.stateCell,
2975
- node = _ref4.node;
2976
- reInitializeState(node, stateCell);
2829
+ resetSpecs.forEach(function (_ref3) {
2830
+ var stateCell = _ref3.stateCell;
2831
+ reInitializeState(stateCell);
2977
2832
  });
2978
2833
  }, [env.$props, resetSpecs]);
2979
2834
  useIsomorphicLayoutEffect$1(function () {
2980
2835
  while ($$state.registrationsQueue.length) {
2981
2836
  var _$$state$registration = $$state.registrationsQueue.shift(),
2982
- node = _$$state$registration.node,
2983
2837
  path = _$$state$registration.path,
2984
- f = _$$state$registration.f;
2985
- var stateCell = node.getState(path);
2986
- stateCell.registeredInitFunc = f;
2987
- reInitializeState(node, stateCell);
2838
+ f = _$$state$registration.f,
2839
+ overrideEnv = _$$state$registration.overrideEnv;
2840
+ var stateCell = getStateCellFrom$StateRoot($state, path);
2841
+ stateCell.initFunc = f;
2842
+ stateCell.overrideEnv = overrideEnv;
2843
+ reInitializeState(stateCell);
2988
2844
  }
2989
2845
  }, [$$state.registrationsQueue.length]);
2990
2846
  // immediately initialize exposed non-private states
@@ -2992,8 +2848,8 @@ function useDollarState(specs) {
2992
2848
  $$state.specTreeLeaves.forEach(function (node) {
2993
2849
  var spec = node.getSpec();
2994
2850
  if (!spec.isRepeated && spec.type !== "private" && spec.initFunc) {
2995
- node.createStateCell(spec.pathObj);
2996
- initializeStateValue($$state, node, spec.pathObj, $state);
2851
+ var stateCell = getStateCellFrom$StateRoot($state, spec.pathObj);
2852
+ initializeStateValue($$state, stateCell, $state);
2997
2853
  }
2998
2854
  });
2999
2855
  }, []);
@@ -3001,10 +2857,171 @@ function useDollarState(specs) {
3001
2857
  valtio.useSnapshot($$state.stateValues, {
3002
2858
  sync: true
3003
2859
  });
3004
- valtio.useSnapshot($$state.registrationsQueue);
2860
+ valtio.useSnapshot($$state.registrationsQueue, {
2861
+ sync: true
2862
+ });
3005
2863
  return $state;
3006
2864
  }
3007
2865
 
2866
+ function generateStateOnChangeProp($state, path) {
2867
+ return function (val) {
2868
+ return set($state, path, val);
2869
+ };
2870
+ }
2871
+ function generateStateValueProp($state, path) {
2872
+ return _get($state, path);
2873
+ }
2874
+ var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
2875
+ function isPlasmicStateProxy(obj) {
2876
+ return obj != null && typeof obj === "object" && !!obj[PLASMIC_STATE_PROXY_SYMBOL];
2877
+ }
2878
+ function getStateCells($state, root) {
2879
+ var _proxyObjToStateCell$;
2880
+ if ($state == null || typeof $state !== "object") {
2881
+ return [];
2882
+ }
2883
+ var stateCells = Object.values((_proxyObjToStateCell$ = proxyObjToStateCell.get($state)) != null ? _proxyObjToStateCell$ : {});
2884
+ if (root.isLeaf()) {
2885
+ return stateCells;
2886
+ }
2887
+ if (root.hasArrayTransition()) {
2888
+ return [].concat(stateCells, Object.keys($state).flatMap(function (key) {
2889
+ return getStateCells($state[key], ensure(root.makeTransition(ARRAY_SYMBOL)));
2890
+ }));
2891
+ } else {
2892
+ return [].concat(stateCells, [].concat(root.edges().entries()).flatMap(function (_ref) {
2893
+ var key = _ref[0],
2894
+ child = _ref[1];
2895
+ return typeof key === "string" && key in $state ? getStateCells($state[key], child) : [];
2896
+ }));
2897
+ }
2898
+ }
2899
+ function getStateCellsInPlasmicProxy(obj) {
2900
+ if (!isPlasmicStateProxy(obj)) {
2901
+ return [];
2902
+ }
2903
+ var _obj$PLASMIC_STATE_PR = obj[PLASMIC_STATE_PROXY_SYMBOL],
2904
+ rootNode = _obj$PLASMIC_STATE_PR.node,
2905
+ rootPath = _obj$PLASMIC_STATE_PR.path,
2906
+ isOutside = _obj$PLASMIC_STATE_PR.isOutside;
2907
+ if (isOutside) {
2908
+ return [];
2909
+ }
2910
+ return getStateCells(obj, rootNode).map(function (stateCell) {
2911
+ return {
2912
+ path: stateCell.node.getSpec().path,
2913
+ realPath: stateCell.path.slice(rootPath.length)
2914
+ };
2915
+ });
2916
+ }
2917
+ function getStateSpecInPlasmicProxy(obj, path) {
2918
+ obj = _get(obj, path.slice(0, path.length - 1));
2919
+ if (!isPlasmicStateProxy(obj)) {
2920
+ return undefined;
2921
+ }
2922
+ var _obj$PLASMIC_STATE_PR2 = obj[PLASMIC_STATE_PROXY_SYMBOL],
2923
+ node = _obj$PLASMIC_STATE_PR2.node,
2924
+ isOutside = _obj$PLASMIC_STATE_PR2.isOutside;
2925
+ var nextNode = node.makeTransition(path[path.length - 1]);
2926
+ if (isOutside || node.isLeaf() || !nextNode) {
2927
+ return undefined;
2928
+ }
2929
+ return {
2930
+ spec: nextNode.getSpec(),
2931
+ isImplicitStateArray: nextNode.hasArrayTransition()
2932
+ };
2933
+ }
2934
+ function getCurrentInitialValue(obj, path) {
2935
+ var _tryGetStateCellFrom$;
2936
+ if (!isPlasmicStateProxy(obj)) {
2937
+ return undefined;
2938
+ }
2939
+ return (_tryGetStateCellFrom$ = tryGetStateCellFrom$StateRoot(obj, path)) == null ? void 0 : _tryGetStateCellFrom$.initialValue;
2940
+ }
2941
+ function resetToInitialValue(obj, path) {
2942
+ var stateCell = tryGetStateCellFrom$StateRoot(obj, path);
2943
+ if (stateCell) {
2944
+ set(obj, path, stateCell.initialValue);
2945
+ }
2946
+ }
2947
+ function shallowEqual(a1, a2) {
2948
+ if (a1.length !== a2.length) {
2949
+ return false;
2950
+ }
2951
+ for (var i = 0; i < a1.length; i++) {
2952
+ if (a1[i] !== a2[i]) {
2953
+ return false;
2954
+ }
2955
+ }
2956
+ return true;
2957
+ }
2958
+ /**
2959
+ * Shallow comparison of arrays.
2960
+ */
2961
+ function arrayEq(xs, ys) {
2962
+ return xs.length === ys.length && xs.every(function (_, index) {
2963
+ return xs[index] === ys[index];
2964
+ });
2965
+ }
2966
+ function isNum$1(value) {
2967
+ return typeof value === "symbol" ? false : !isNaN(+value);
2968
+ }
2969
+ function assert(cond, msg) {
2970
+ if (msg === void 0) {
2971
+ msg = "Assertion failed";
2972
+ }
2973
+ if (!cond) {
2974
+ // We always generate an non empty message so that it doesn't get swallowed
2975
+ // by the async library.
2976
+ msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
2977
+ debugger;
2978
+ throw new Error(msg);
2979
+ }
2980
+ }
2981
+ /**
2982
+ * Forked from https://github.com/lukeed/dset
2983
+ * Changes: fixed setting a deep value to a proxy object
2984
+ */
2985
+ function set(obj, keys, val) {
2986
+ keys = keys.split ? keys.split(".") : keys;
2987
+ var i = 0,
2988
+ l = keys.length,
2989
+ t = obj,
2990
+ x,
2991
+ k;
2992
+ while (i < l) {
2993
+ k = keys[i++];
2994
+ if (k === "__proto__" || k === "constructor" || k === "prototype") break;
2995
+ var newValue = i === l ? val : typeof (x = t[k]) === typeof keys ? x : keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".") ? {} : [];
2996
+ assignValue(t, k, newValue);
2997
+ t = t[k];
2998
+ }
2999
+ }
3000
+ /**
3001
+ * Forked from lodash
3002
+ */
3003
+ function baseAssignValue(object, key, value) {
3004
+ if (key == "__proto__") {
3005
+ Object.defineProperty(object, key, {
3006
+ configurable: true,
3007
+ enumerable: true,
3008
+ value: value,
3009
+ writable: true
3010
+ });
3011
+ } else {
3012
+ object[key] = value;
3013
+ }
3014
+ }
3015
+ function eq(value, other) {
3016
+ return value === other || value !== value && other !== other;
3017
+ }
3018
+ function assignValue(object, key, value) {
3019
+ var objValue = object[key];
3020
+ if (!(Object.prototype.hasOwnProperty.call(object, key) && eq(objValue, value)) || value === undefined && !(key in object)) {
3021
+ baseAssignValue(object, key, value);
3022
+ }
3023
+ }
3024
+
3008
3025
  // Utilities used by generated code
3009
3026
  var classNames = classNames$1;
3010
3027
 
@@ -4515,6 +4532,7 @@ exports.ensureGlobalVariants = ensureGlobalVariants;
4515
4532
  exports.genTranslatableString = genTranslatableString;
4516
4533
  exports.generateStateOnChangeProp = generateStateOnChangeProp;
4517
4534
  exports.generateStateValueProp = generateStateValueProp;
4535
+ exports.getCurrentInitialValue = getCurrentInitialValue;
4518
4536
  exports.getDataProps = getDataProps;
4519
4537
  exports.getStateCellsInPlasmicProxy = getStateCellsInPlasmicProxy;
4520
4538
  exports.getStateSpecInPlasmicProxy = getStateSpecInPlasmicProxy;
@@ -4526,6 +4544,7 @@ exports.omit = omit;
4526
4544
  exports.pick = pick;
4527
4545
  exports.plasmicHeadMeta = plasmicHeadMeta;
4528
4546
  exports.renderPlasmicSlot = renderPlasmicSlot;
4547
+ exports.resetToInitialValue = resetToInitialValue;
4529
4548
  exports.set = set;
4530
4549
  exports.setPlumeStrictMode = setPlumeStrictMode;
4531
4550
  exports.useButton = useButton;