@stonecrop/stonecrop 0.62.6 → 0.62.8

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.
@@ -2623,7 +2623,8 @@ export declare function useOperationLog(config?: Partial<OperationLogConfig>): {
2623
2623
 
2624
2624
  /**
2625
2625
  * Global HST Operation Log Store
2626
- * Tracks all mutations with full metadata for undo/redo, sync, and audit
2626
+ * Tracks each change made through the store's `set()` with full metadata for undo/redo, sync, and audit.
2627
+ * A write made straight into an object read from the store is not logged; outside production it logs a warning.
2627
2628
  *
2628
2629
  * @public
2629
2630
  */
package/dist/stonecrop.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useRouter } from "vue-router";
2
2
  import { defineStore, storeToRefs } from "pinia";
3
- import { computed, getCurrentInstance, inject, nextTick, onMounted, provide, reactive, ref, watch } from "vue";
3
+ import { ReactiveEffect, computed, effectScope, getCurrentInstance, inject, isReactive, nextTick, onMounted, provide, reactive, ref, toRaw, watch } from "vue";
4
4
  import { useLocalStorage, useMagicKeys, whenever } from "@vueuse/core";
5
5
  import { componentCategory, componentLinkExpansion, flattenFields, getRecordIdField, getRecordIdentity, isActionAllowedInState, isActionAllowedOnNewRecord, normalizeFieldKind, resolveLinkRenderMode, toSlug } from "@stonecrop/schema";
6
6
  import { List } from "immutable";
@@ -191,7 +191,8 @@ function applyOperation(operation, store) {
191
191
  }
192
192
  /**
193
193
  * Global HST Operation Log Store
194
- * Tracks all mutations with full metadata for undo/redo, sync, and audit
194
+ * Tracks each change made through the store's `set()` with full metadata for undo/redo, sync, and audit.
195
+ * A write made straight into an object read from the store is not logged; outside production it logs a warning.
195
196
  *
196
197
  * @public
197
198
  */
@@ -1069,6 +1070,114 @@ function getOperationLogStore() {
1069
1070
  return null;
1070
1071
  }
1071
1072
  }
1073
+ var writesThroughSet = 0;
1074
+ var isDevelopment = () => {
1075
+ try {
1076
+ return process.env.NODE_ENV !== "production";
1077
+ } catch {
1078
+ return false;
1079
+ }
1080
+ };
1081
+ function pathTo(root, target, seen = /* @__PURE__ */ new Set()) {
1082
+ if (root === target) return [];
1083
+ if (root === null || typeof root !== "object" || seen.has(root)) return void 0;
1084
+ seen.add(root);
1085
+ for (const [key, value] of Object.entries(root)) {
1086
+ const rest = pathTo(toRaw(value), target, seen);
1087
+ if (rest) return [key, ...rest];
1088
+ }
1089
+ }
1090
+ function isPlainContainer(value) {
1091
+ if (value === null || typeof value !== "object") return false;
1092
+ return Array.isArray(value) || Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null;
1093
+ }
1094
+ /**
1095
+ * Warns the developer, outside production, when a value in `store` changes without going through
1096
+ * {@link HSTNode.set}: a component or app code wrote straight into an object it read from the store.
1097
+ * The write is let through, but the operation log never hears of it, so Undo skips it and reverses the
1098
+ * change made before it.
1099
+ *
1100
+ * A deep `watch` would walk the whole store again after every change, so each edit would get slower the
1101
+ * more records the store holds. Instead, each key in the store is read once, by an effect that is never
1102
+ * re-run: it stays subscribed to what it read, so a later write to that key reaches `onTrigger` without
1103
+ * any walk. After a change, only the keys `onTrigger` named are looked at, to start tracking keys and
1104
+ * objects not seen before; the object's other keys are not read again, so adding a record doesn't cost
1105
+ * more the more records its doctype holds. Each key is tracked by exactly one effect, so a write warns once.
1106
+ *
1107
+ * Each object gets its own effect, in its own detached scope, and the effect drops the object once it has
1108
+ * read it. Detached, so the warning doesn't stop when the component that happened to build Stonecrop
1109
+ * unmounts; one per object and holding nothing, so an effect lives only as long as the object it watches
1110
+ * (its subscriptions are kept by that object's reactive deps) and a replaced row or array, once no longer
1111
+ * referenced, is freed along with its effect.
1112
+ */
1113
+ function warnOnWritesOutsideSet(store) {
1114
+ const root = toRaw(store);
1115
+ const trackedKeys = /* @__PURE__ */ new WeakMap();
1116
+ const changed = /* @__PURE__ */ new Map();
1117
+ let readSomething = false;
1118
+ const onTrigger = (event) => {
1119
+ const key = typeof event.key === "symbol" || event.key === void 0 ? void 0 : String(event.key);
1120
+ let keys = changed.get(event.target);
1121
+ if (!keys) changed.set(event.target, keys = /* @__PURE__ */ new Set());
1122
+ if (key !== void 0) keys.add(key);
1123
+ if (writesThroughSet > 0) return;
1124
+ const owner = pathTo(root, event.target);
1125
+ if (!owner) return;
1126
+ const path = [...owner, ...key === void 0 ? [] : [key]].join(".") || "(store)";
1127
+ console.warn(`[Stonecrop] ${path} was changed by writing straight into an object read from the store, so the operation log did not record it and Undo will skip it. Make the change through useStonecrop (handleHSTChange) or the store's set() instead.`);
1128
+ };
1129
+ const read = (raw, keys, found) => {
1130
+ const proxy = reactive(raw);
1131
+ let tracked = trackedKeys.get(raw);
1132
+ if (!tracked) {
1133
+ tracked = /* @__PURE__ */ new Set();
1134
+ trackedKeys.set(raw, tracked);
1135
+ readSomething = true;
1136
+ keys = void 0;
1137
+ if (Array.isArray(proxy)) proxy[Symbol.iterator]();
1138
+ else Object.keys(proxy);
1139
+ }
1140
+ const isArray = Array.isArray(raw);
1141
+ for (const key of keys ?? Object.keys(raw)) {
1142
+ if (!Object.prototype.propertyIsEnumerable.call(raw, key)) continue;
1143
+ let value;
1144
+ if (isArray || tracked.has(key)) value = toRaw(raw[key]);
1145
+ else {
1146
+ tracked.add(key);
1147
+ readSomething = true;
1148
+ value = toRaw(proxy[key]);
1149
+ }
1150
+ if (isPlainContainer(value) && !trackedKeys.has(value)) found.add(value);
1151
+ }
1152
+ };
1153
+ const track = (raw, keys) => {
1154
+ if (!isPlainContainer(raw)) return;
1155
+ const found = /* @__PURE__ */ new Set();
1156
+ let target = raw;
1157
+ let targetKeys = keys;
1158
+ effectScope(true).run(() => {
1159
+ const effect = new ReactiveEffect(() => {
1160
+ if (target) read(target, targetKeys, found);
1161
+ target = void 0;
1162
+ targetKeys = void 0;
1163
+ });
1164
+ effect.onTrigger = onTrigger;
1165
+ effect.scheduler = flush;
1166
+ readSomething = false;
1167
+ effect.run();
1168
+ if (!readSomething) effect.stop();
1169
+ });
1170
+ for (const child of found) if (!trackedKeys.has(child)) track(child);
1171
+ found.clear();
1172
+ };
1173
+ function flush() {
1174
+ if (changed.size === 0) return;
1175
+ const targets = [...changed];
1176
+ changed.clear();
1177
+ for (const [target, keys] of targets) track(target, Array.isArray(target) ? void 0 : keys);
1178
+ }
1179
+ track(root);
1180
+ }
1072
1181
  /**
1073
1182
  * Global HST Manager (Singleton)
1074
1183
  * Manages hierarchical state trees and provides access to the global registry.
@@ -1177,7 +1286,12 @@ var HSTProxy = class HSTProxy {
1177
1286
  }, source);
1178
1287
  }
1179
1288
  }
1180
- this.updateValue(path, value);
1289
+ writesThroughSet++;
1290
+ try {
1291
+ this.updateValue(path, value);
1292
+ } finally {
1293
+ writesThroughSet--;
1294
+ }
1181
1295
  this.triggerFieldActions(fullPath, beforeValue, value);
1182
1296
  }
1183
1297
  has(path) {
@@ -1371,6 +1485,7 @@ var HSTProxy = class HSTProxy {
1371
1485
  * @public
1372
1486
  */
1373
1487
  function createHST(target, doctype) {
1488
+ if (isDevelopment() && isReactive(target)) warnOnWritesOutsideSet(target);
1374
1489
  return new HSTProxy(target, doctype, "", null);
1375
1490
  }
1376
1491
  // src/stonecrop.ts