@yiin/reactive-proxy-state 1.0.30 → 1.0.32

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/index.cjs CHANGED
@@ -2465,8 +2465,12 @@ function evictDescendantsFromPathCache(root, pathKey) {
2465
2465
  const cache = pathCache.get(root);
2466
2466
  if (!cache)
2467
2467
  return;
2468
- const prefix = pathKey + ".";
2469
2468
  let evicted = 0;
2469
+ if (cache.has(pathKey)) {
2470
+ cache.delete(pathKey);
2471
+ evicted++;
2472
+ }
2473
+ const prefix = pathKey + ".";
2470
2474
  for (const key of cache.keys()) {
2471
2475
  if (key.startsWith(prefix)) {
2472
2476
  cache.delete(key);
@@ -2593,6 +2597,26 @@ function deleteValue(obj, key) {
2593
2597
  else
2594
2598
  delete obj[key];
2595
2599
  }
2600
+ function validateCachedPath(root, fullPath, pathKey, cached) {
2601
+ if (fullPath.length === 0)
2602
+ return cached;
2603
+ const lastKey = fullPath[fullPath.length - 1];
2604
+ let grandparent = root;
2605
+ for (let i = 0;i < fullPath.length - 1; i++) {
2606
+ grandparent = grandparent ? getValue(grandparent, fullPath[i]) : undefined;
2607
+ if (grandparent === undefined)
2608
+ break;
2609
+ }
2610
+ if (grandparent === undefined) {
2611
+ evictDescendantsFromPathCache(root, pathKey);
2612
+ return;
2613
+ }
2614
+ const actual = getValue(grandparent, lastKey);
2615
+ if (actual === cached)
2616
+ return cached;
2617
+ evictDescendantsFromPathCache(root, pathKey);
2618
+ return;
2619
+ }
2596
2620
  var actionHandlers = {
2597
2621
  set: function(parent, key, event) {
2598
2622
  setValue(parent, key, event.newValue);
@@ -2746,6 +2770,9 @@ function updateState(root, event) {
2746
2770
  const parentPath = path.slice(0, -1);
2747
2771
  const parentPathKey = parentPath.join(".");
2748
2772
  let parent = pathCache.get(root)?.get(parentPathKey);
2773
+ if (parent !== undefined) {
2774
+ parent = validateCachedPath(root, parentPath, parentPathKey, parent);
2775
+ }
2749
2776
  if (parent === undefined) {
2750
2777
  parent = parentPath.reduce((acc, key) => acc ? getValue(acc, key) : undefined, root);
2751
2778
  if (parent !== undefined)
@@ -2765,6 +2792,9 @@ function updateState(root, event) {
2765
2792
  const targetPath = path;
2766
2793
  const targetPathKey = targetPath.join(".");
2767
2794
  let targetCollection = pathCache.get(root)?.get(targetPathKey);
2795
+ if (targetCollection !== undefined) {
2796
+ targetCollection = validateCachedPath(root, targetPath, targetPathKey, targetCollection);
2797
+ }
2768
2798
  if (targetCollection === undefined) {
2769
2799
  targetCollection = targetPath.reduce((acc, key) => acc ? getValue(acc, key) : undefined, root);
2770
2800
  if (targetCollection !== undefined)
package/dist/index.js CHANGED
@@ -2405,8 +2405,12 @@ function evictDescendantsFromPathCache(root, pathKey) {
2405
2405
  const cache = pathCache.get(root);
2406
2406
  if (!cache)
2407
2407
  return;
2408
- const prefix = pathKey + ".";
2409
2408
  let evicted = 0;
2409
+ if (cache.has(pathKey)) {
2410
+ cache.delete(pathKey);
2411
+ evicted++;
2412
+ }
2413
+ const prefix = pathKey + ".";
2410
2414
  for (const key of cache.keys()) {
2411
2415
  if (key.startsWith(prefix)) {
2412
2416
  cache.delete(key);
@@ -2533,6 +2537,26 @@ function deleteValue(obj, key) {
2533
2537
  else
2534
2538
  delete obj[key];
2535
2539
  }
2540
+ function validateCachedPath(root, fullPath, pathKey, cached) {
2541
+ if (fullPath.length === 0)
2542
+ return cached;
2543
+ const lastKey = fullPath[fullPath.length - 1];
2544
+ let grandparent = root;
2545
+ for (let i = 0;i < fullPath.length - 1; i++) {
2546
+ grandparent = grandparent ? getValue(grandparent, fullPath[i]) : undefined;
2547
+ if (grandparent === undefined)
2548
+ break;
2549
+ }
2550
+ if (grandparent === undefined) {
2551
+ evictDescendantsFromPathCache(root, pathKey);
2552
+ return;
2553
+ }
2554
+ const actual = getValue(grandparent, lastKey);
2555
+ if (actual === cached)
2556
+ return cached;
2557
+ evictDescendantsFromPathCache(root, pathKey);
2558
+ return;
2559
+ }
2536
2560
  var actionHandlers = {
2537
2561
  set: function(parent, key, event) {
2538
2562
  setValue(parent, key, event.newValue);
@@ -2686,6 +2710,9 @@ function updateState(root, event) {
2686
2710
  const parentPath = path.slice(0, -1);
2687
2711
  const parentPathKey = parentPath.join(".");
2688
2712
  let parent = pathCache.get(root)?.get(parentPathKey);
2713
+ if (parent !== undefined) {
2714
+ parent = validateCachedPath(root, parentPath, parentPathKey, parent);
2715
+ }
2689
2716
  if (parent === undefined) {
2690
2717
  parent = parentPath.reduce((acc, key) => acc ? getValue(acc, key) : undefined, root);
2691
2718
  if (parent !== undefined)
@@ -2705,6 +2732,9 @@ function updateState(root, event) {
2705
2732
  const targetPath = path;
2706
2733
  const targetPathKey = targetPath.join(".");
2707
2734
  let targetCollection = pathCache.get(root)?.get(targetPathKey);
2735
+ if (targetCollection !== undefined) {
2736
+ targetCollection = validateCachedPath(root, targetPath, targetPathKey, targetCollection);
2737
+ }
2708
2738
  if (targetCollection === undefined) {
2709
2739
  targetCollection = targetPath.reduce((acc, key) => acc ? getValue(acc, key) : undefined, root);
2710
2740
  if (targetCollection !== undefined)
package/dist/utils.d.ts CHANGED
@@ -6,9 +6,9 @@ export declare function deepEqual(a: any, b: any, seen?: WeakMap<any, any>): boo
6
6
  export declare function getFromPathCache(root: object, pathKey: string): any | undefined;
7
7
  export declare function setInPathCache(root: object, pathKey: string, value: any): void;
8
8
  /**
9
- * Evict all cached paths that are descendants of the given path prefix.
10
- * Called when a value at `pathKey` is replaced so that deeper cached references
11
- * (which now point to the old, detached object) are not reused.
9
+ * Evict the cached path entry at `pathKey` AND all its descendants.
10
+ * Called when a value at `pathKey` is replaced so that the stale cached reference
11
+ * (and any deeper ones) are not reused on subsequent mutations.
12
12
  */
13
13
  export declare function evictDescendantsFromPathCache(root: object, pathKey: string): void;
14
14
  export declare function getPathConcat(path: string): any[] | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yiin/reactive-proxy-state",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "author": "Yiin <stanislovas@yiin.lt>",
5
5
  "repository": {
6
6
  "type": "git",