@plasmicapp/react-web 0.2.183 → 0.2.184

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.
@@ -15,6 +15,7 @@ export declare function generateStateOnChangePropForCodeComponents($state: $Stat
15
15
  export declare function generateStateValueProp($state: $State, path: ObjectPath): any;
16
16
  export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
17
17
  export declare function isPlasmicStateProxy(obj: any): boolean;
18
+ export declare function is$StateProxy(obj: any): number | boolean | undefined;
18
19
  export declare function getStateCells($state: $State, root: StateSpecNode<any>): StateCell<any>[];
19
20
  export declare function getStateCellsInPlasmicProxy(obj: any): {
20
21
  realPath: ObjectPath;
@@ -1,4 +1,4 @@
1
1
  export { default as get } from "dlv";
2
- export { generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, initializeCodeComponentStates, initializePlasmicStates, isPlasmicStateProxy, resetToInitialValue, set, } from "./helpers";
2
+ export { generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, initializeCodeComponentStates, initializePlasmicStates, is$StateProxy, isPlasmicStateProxy, resetToInitialValue, set, } from "./helpers";
3
3
  export { $State, $StateSpec } from "./types";
4
4
  export { useDollarState } from "./valtio";
@@ -1,6 +1,10 @@
1
- import { $State, $StateSpec, ObjectPath, StateCell } from "./types";
1
+ import { StateSpecNode } from "./graph";
2
+ import { $State, $StateSpec, Internal$State, ObjectPath, StateCell } from "./types";
2
3
  export declare const proxyObjToStateCell: WeakMap<object, Record<string | number | symbol, StateCell<any>>>;
4
+ export declare const valtioSubscriptions: WeakMap<object, (() => boolean)[]>;
3
5
  export declare function tryGetStateCellFrom$StateRoot($state: $State, path: ObjectPath): StateCell<any> | undefined;
4
6
  export declare function getStateCellFrom$StateRoot($state: $State, path: ObjectPath): StateCell<any>;
7
+ export declare function unsubscribeToValtio($$state: Internal$State, statePath: ObjectPath): void;
8
+ export declare function subscribeToValtio($$state: Internal$State, statePath: ObjectPath, node: StateSpecNode<any>): void;
5
9
  export declare function useDollarState(specs: $StateSpec<any>[], ...rest: any[]): $State;
6
10
  export default useDollarState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/react-web",
3
- "version": "0.2.183",
3
+ "version": "0.2.184",
4
4
  "description": "plasmic library for rendering in the presentational style",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -171,5 +171,5 @@
171
171
  "react": ">=16.8.0",
172
172
  "react-dom": ">=16.8.0"
173
173
  },
174
- "gitHead": "40a5cdf705d54bbd0e4891bf5ea4b8470731bd12"
174
+ "gitHead": "9eeaff2cc90823316469cbf70122b0c5c650a594"
175
175
  }
@@ -14,9 +14,9 @@ import { T as Trans } from './ssr-08aff522.js';
14
14
  export { P as PlasmicRootProvider, T as Trans, g as genTranslatableString, u as useIsSSR } from './ssr-08aff522.js';
15
15
  import ReactDOM__default from 'react-dom';
16
16
  import { useFocusRing } from '@react-aria/focus';
17
+ import { proxy, ref, useSnapshot, getVersion, subscribe } from 'valtio';
17
18
  import clone from 'clone';
18
19
  import deepEqual from 'fast-deep-equal';
19
- import { proxy, ref, useSnapshot } from 'valtio';
20
20
  import '@react-aria/ssr';
21
21
 
22
22
  // https://stackoverflow.com/a/2117523
@@ -945,6 +945,7 @@ function canProxy(value) {
945
945
  return typeof value === "object" && value != null;
946
946
  }
947
947
  var proxyObjToStateCell = new WeakMap();
948
+ var valtioSubscriptions = new WeakMap();
948
949
  function ensureStateCell(target, property, path, node) {
949
950
  var _a;
950
951
  if (!proxyObjToStateCell.has(target)) {
@@ -978,6 +979,32 @@ function tryGetStateCellFrom$StateRoot($state, path) {
978
979
  function getStateCellFrom$StateRoot($state, path) {
979
980
  return ensure(tryGetStateCellFrom$StateRoot($state, path));
980
981
  }
982
+ function unsubscribeToValtio($$state, statePath) {
983
+ var _a;
984
+ var oldValue = get($$state.stateValues, statePath);
985
+ if (getVersion(oldValue)) {
986
+ (_a = valtioSubscriptions.get(oldValue)) === null || _a === void 0 ? void 0 : _a.forEach(function (f) { return f(); });
987
+ valtioSubscriptions["delete"](oldValue);
988
+ }
989
+ }
990
+ function subscribeToValtio($$state, statePath, node) {
991
+ var spec = node.getSpec();
992
+ var maybeValtioProxy = spec.valueProp
993
+ ? $$state.env.$props[spec.valueProp]
994
+ : get($$state.stateValues, statePath);
995
+ if (getVersion(maybeValtioProxy) && spec.onChangeProp) {
996
+ var unsub = subscribe(maybeValtioProxy, function () {
997
+ var _a, _b;
998
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, spec.valueProp
999
+ ? $$state.env.$props[spec.valueProp]
1000
+ : get($$state.stateValues, statePath));
1001
+ });
1002
+ if (!valtioSubscriptions.has(maybeValtioProxy)) {
1003
+ valtioSubscriptions.set(maybeValtioProxy, []);
1004
+ }
1005
+ ensure(valtioSubscriptions.get(maybeValtioProxy)).push(unsub);
1006
+ }
1007
+ }
981
1008
  function initializeStateValue($$state, initialStateCell, proxyRoot) {
982
1009
  var _a, _b, _c;
983
1010
  var initialStateName = initialStateCell.node.getSpec().path;
@@ -1041,7 +1068,7 @@ function initializeStateValue($$state, initialStateCell, proxyRoot) {
1041
1068
  }
1042
1069
  function create$StateProxy($$state, leafHandlers) {
1043
1070
  var proxyRoot;
1044
- var rec = function (currPath, currNode, isOutside, initialObject) {
1071
+ var rec = function (currPath, currNode) {
1045
1072
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
1046
1073
  isNum$1(property) ? +property : property,
1047
1074
  ], false); };
@@ -1049,8 +1076,7 @@ function create$StateProxy($$state, leafHandlers) {
1049
1076
  var handlers = {
1050
1077
  deleteProperty: function (target, property) {
1051
1078
  var _a, _b;
1052
- if (!isOutside &&
1053
- !currNode.isLeaf() &&
1079
+ if (!currNode.isLeaf() &&
1054
1080
  !currNode.hasArrayTransition() &&
1055
1081
  !isNum$1(property)) {
1056
1082
  throw new InvalidOperation("Can't delete a property in the middle of the state spec");
@@ -1069,20 +1095,16 @@ function create$StateProxy($$state, leafHandlers) {
1069
1095
  if (property === PLASMIC_STATE_PROXY_SYMBOL) {
1070
1096
  return {
1071
1097
  node: currNode,
1072
- path: currPath,
1073
- isOutside: isOutside
1098
+ path: currPath
1074
1099
  };
1075
1100
  }
1076
1101
  var nextPath = getNextPath(property);
1077
- if (isOutside || currNode.isLeaf()) {
1078
- return Reflect.get(target, property, receiver);
1079
- }
1080
1102
  var nextNode = currNode.makeTransition(property);
1081
1103
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1082
1104
  return (_b = (_a = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1083
1105
  }
1084
1106
  else if (nextNode && !(property in target)) {
1085
- target[property] = rec(nextPath, nextNode, false, undefined);
1107
+ target[property] = rec(nextPath, nextNode);
1086
1108
  }
1087
1109
  return Reflect.get(target, property, receiver);
1088
1110
  },
@@ -1090,6 +1112,7 @@ function create$StateProxy($$state, leafHandlers) {
1090
1112
  var _a, _b, _c, _d;
1091
1113
  var nextPath = getNextPath(property);
1092
1114
  var nextNode = currNode.makeTransition(property);
1115
+ var nextSpec = nextNode === null || nextNode === void 0 ? void 0 : nextNode.getSpec();
1093
1116
  if (property === "registerInitFunc" && currPath.length === 0) {
1094
1117
  return Reflect.set(target, property, value, receiver);
1095
1118
  }
@@ -1100,70 +1123,41 @@ function create$StateProxy($$state, leafHandlers) {
1100
1123
  }
1101
1124
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1102
1125
  (_b = (_a = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1126
+ Reflect.set(target, property, value, receiver);
1127
+ if (nextSpec === null || nextSpec === void 0 ? void 0 : nextSpec.onChangeProp) {
1128
+ (_d = (_c = $$state.env.$props)[nextSpec.onChangeProp]) === null || _d === void 0 ? void 0 : _d.call(_c, value);
1129
+ }
1103
1130
  }
1104
- if (!isOutside && !currNode.isLeaf() && !nextNode) {
1131
+ if (!nextNode) {
1105
1132
  // can't set an unknown field in $state
1106
1133
  return false;
1107
1134
  }
1108
- // we keep pointing to the leaf
1109
- if (!nextNode) {
1110
- assert(isOutside || currNode.isLeaf, "unexpected update in nextNode");
1111
- nextNode = currNode;
1112
- }
1113
- if (canProxy(value)) {
1114
- target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
1135
+ if (canProxy(value) && !nextNode.isLeaf()) {
1136
+ target[property] = rec(nextPath, nextNode);
1137
+ Reflect.ownKeys(value).forEach(function (key) {
1138
+ target[property][key] = value[key];
1139
+ });
1115
1140
  }
1116
- else if (!isOutside && !currNode.isLeaf() && !(nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf())) {
1141
+ else if (!nextNode.isLeaf()) {
1117
1142
  throw new InvalidOperation("inserting a primitive value into a non-leaf");
1118
1143
  }
1119
- else {
1120
- Reflect.set(target, property, value, receiver);
1121
- }
1122
- if (currNode.isLeaf()) {
1123
- if (spec.onChangeProp) {
1124
- (_d = (_c = $$state.env.$props)[spec.onChangeProp]) === null || _d === void 0 ? void 0 : _d.call(_c, target);
1125
- }
1126
- }
1127
- else {
1128
- nextNode.getAllSpecs().forEach(function (spec) {
1129
- var _a, _b;
1130
- if (spec.onChangeProp) {
1131
- (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1132
- }
1133
- });
1134
- }
1135
- var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable
1144
+ var newValue = nextNode.isLeaf() && (nextSpec === null || nextSpec === void 0 ? void 0 : nextSpec.isImmutable)
1136
1145
  ? mkUntrackedValue(value)
1137
1146
  : value;
1147
+ unsubscribeToValtio($$state, nextPath);
1138
1148
  set($$state.stateValues, nextPath, newValue);
1149
+ subscribeToValtio($$state, nextPath, nextNode);
1139
1150
  return true;
1140
1151
  }
1141
1152
  };
1142
- var baseObject = !isOutside && !currNode.isLeaf()
1143
- ? currNode.hasArrayTransition()
1144
- ? []
1145
- : {}
1146
- : Array.isArray(initialObject)
1147
- ? []
1148
- : Object.create(Object.getPrototypeOf(initialObject !== null && initialObject !== void 0 ? initialObject : {}));
1153
+ var baseObject = currNode.hasArrayTransition() ? [] : {};
1149
1154
  var proxyObj = new Proxy(baseObject, handlers);
1150
1155
  if (currPath.length === 0) {
1151
1156
  proxyRoot = proxyObj;
1152
1157
  }
1153
- if (initialObject) {
1154
- Reflect.ownKeys(initialObject).forEach(function (key) {
1155
- var desc = Object.getOwnPropertyDescriptor(initialObject, key);
1156
- if (desc.get || desc.set) {
1157
- Object.defineProperty(baseObject, key, desc);
1158
- }
1159
- else {
1160
- proxyObj[key] = initialObject[key];
1161
- }
1162
- });
1163
- }
1164
1158
  return proxyObj;
1165
1159
  };
1166
- return rec([], $$state.rootSpecTree, false, undefined);
1160
+ return rec([], $$state.rootSpecTree);
1167
1161
  }
1168
1162
  var mkUntrackedValue = function (o) {
1169
1163
  return o != null && typeof o === "object" ? ref(o) : o;
@@ -1253,13 +1247,15 @@ function useDollarState(specs) {
1253
1247
  set($state, stateCell.path, spec.initVal);
1254
1248
  }
1255
1249
  return {
1256
- get: function (target, property, receiver) {
1250
+ get: function () {
1257
1251
  var spec = stateCell.node.getSpec();
1258
1252
  if (spec.valueProp) {
1259
- return $$state.env.$props[spec.valueProp];
1253
+ var valueProp = $$state.env.$props[spec.valueProp];
1254
+ subscribeToValtio($$state, stateCell.path, stateCell.node);
1255
+ return valueProp;
1260
1256
  }
1261
1257
  else {
1262
- return Reflect.get(target, property, receiver);
1258
+ return get($$state.stateValues, stateCell.path);
1263
1259
  }
1264
1260
  }
1265
1261
  };
@@ -1449,6 +1445,11 @@ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect
1449
1445
  function isPlasmicStateProxy(obj) {
1450
1446
  return (obj != null && typeof obj === "object" && !!obj[PLASMIC_STATE_PROXY_SYMBOL]);
1451
1447
  }
1448
+ function is$StateProxy(obj) {
1449
+ return (obj != null &&
1450
+ typeof obj === "object" &&
1451
+ (!!obj[PLASMIC_STATE_PROXY_SYMBOL] || getVersion(obj)));
1452
+ }
1452
1453
  function getStateCells($state, root) {
1453
1454
  var e_3, _a;
1454
1455
  var _b;
@@ -1502,9 +1503,9 @@ function getStateSpecInPlasmicProxy(obj, path) {
1502
1503
  if (!isPlasmicStateProxy(obj)) {
1503
1504
  return undefined;
1504
1505
  }
1505
- var _a = obj[PLASMIC_STATE_PROXY_SYMBOL], node = _a.node, isOutside = _a.isOutside;
1506
+ var node = obj[PLASMIC_STATE_PROXY_SYMBOL].node;
1506
1507
  var nextNode = node.makeTransition(path[path.length - 1]);
1507
- if (isOutside || node.isLeaf() || !nextNode) {
1508
+ if (node.isLeaf() || !nextNode) {
1508
1509
  return undefined;
1509
1510
  }
1510
1511
  return {
@@ -1545,16 +1546,6 @@ function arrayEq(xs, ys) {
1545
1546
  function isNum(value) {
1546
1547
  return typeof value === "symbol" ? false : !isNaN(+value);
1547
1548
  }
1548
- function assert(cond, msg) {
1549
- if (msg === void 0) { msg = "Assertion failed"; }
1550
- if (!cond) {
1551
- // We always generate an non empty message so that it doesn't get swallowed
1552
- // by the async library.
1553
- msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
1554
- debugger;
1555
- throw new Error(msg);
1556
- }
1557
- }
1558
1549
  /**
1559
1550
  * Forked from https://github.com/lukeed/dset
1560
1551
  * Changes: fixed setting a deep value to a proxy object
@@ -1607,5 +1598,5 @@ function assignValue(object, key, value) {
1607
1598
  // Utilities used by generated code
1608
1599
  var classNames = classNames$1;
1609
1600
 
1610
- export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, hasVariant, initializeCodeComponentStates, initializePlasmicStates, isPlasmicStateProxy, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, resetToInitialValue, set, useDollarState, useTrigger, wrapWithClassName };
1601
+ export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, hasVariant, initializeCodeComponentStates, initializePlasmicStates, is$StateProxy, isPlasmicStateProxy, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, resetToInitialValue, set, useDollarState, useTrigger, wrapWithClassName };
1611
1602
  //# sourceMappingURL=index.js.map