@yiin/reactive-proxy-state 1.0.39 → 1.0.41

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
@@ -2362,6 +2362,35 @@ __export(exports_src, {
2362
2362
  module.exports = __toCommonJS(exports_src);
2363
2363
 
2364
2364
  // src/utils.ts
2365
+ function toRaw(observed) {
2366
+ let value = observed;
2367
+ while (value && value["__v_raw" /* RAW */]) {
2368
+ value = value["__v_raw" /* RAW */];
2369
+ }
2370
+ return value;
2371
+ }
2372
+ function unwrapForStore(value) {
2373
+ if (value["__v_isReactive" /* IS_REACTIVE */])
2374
+ return toRaw(value);
2375
+ if (Array.isArray(value)) {
2376
+ for (let i = 0;i < value.length; i++) {
2377
+ const child = value[i];
2378
+ if (child != null && typeof child === "object" && child["__v_isReactive" /* IS_REACTIVE */]) {
2379
+ value[i] = toRaw(child);
2380
+ }
2381
+ }
2382
+ } else if (!(value instanceof Map) && !(value instanceof Set) && !(value instanceof Date)) {
2383
+ for (const key in value) {
2384
+ if (!Object.prototype.hasOwnProperty.call(value, key))
2385
+ continue;
2386
+ const child = value[key];
2387
+ if (child != null && typeof child === "object" && child["__v_isReactive" /* IS_REACTIVE */]) {
2388
+ value[key] = toRaw(child);
2389
+ }
2390
+ }
2391
+ }
2392
+ return value;
2393
+ }
2365
2394
  var deepEqualCache = new WeakMap;
2366
2395
  var MAX_CACHE_SIZE = 1000;
2367
2396
  var pathCache = new WeakMap;
@@ -2392,6 +2421,7 @@ function resetProxyStats() {
2392
2421
  function evictDeep(value) {
2393
2422
  if (value == null || typeof value !== "object")
2394
2423
  return;
2424
+ value = toRaw(value);
2395
2425
  if (!globalSeen.has(value))
2396
2426
  return;
2397
2427
  globalSeen.delete(value);
@@ -3047,6 +3077,10 @@ function wrapSet(set, emit, path = []) {
3047
3077
  const methodCache = {};
3048
3078
  const proxy = new Proxy(set, {
3049
3079
  get(target, prop, receiver) {
3080
+ if (prop === "__v_raw" /* RAW */)
3081
+ return target;
3082
+ if (prop === "__v_isReactive" /* IS_REACTIVE */)
3083
+ return true;
3050
3084
  track(target, prop);
3051
3085
  if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
3052
3086
  track(target, Symbol.iterator);
@@ -3056,6 +3090,8 @@ function wrapSet(set, emit, path = []) {
3056
3090
  }
3057
3091
  if (prop === "add") {
3058
3092
  methodCache[prop] = function(value2) {
3093
+ if (value2 != null && typeof value2 === "object")
3094
+ value2 = unwrapForStore(value2);
3059
3095
  const existed = target.has(value2);
3060
3096
  const oldSize = target.size;
3061
3097
  if (!existed) {
@@ -3224,6 +3260,10 @@ function wrapMap(map, emit, path = []) {
3224
3260
  const methodCache = {};
3225
3261
  const proxy = new Proxy(map, {
3226
3262
  get(target, prop, receiver) {
3263
+ if (prop === "__v_raw" /* RAW */)
3264
+ return target;
3265
+ if (prop === "__v_isReactive" /* IS_REACTIVE */)
3266
+ return true;
3227
3267
  track(target, prop);
3228
3268
  if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
3229
3269
  track(target, Symbol.iterator);
@@ -3233,6 +3273,8 @@ function wrapMap(map, emit, path = []) {
3233
3273
  }
3234
3274
  if (prop === "set") {
3235
3275
  methodCache[prop] = function(key, value2) {
3276
+ if (value2 != null && typeof value2 === "object")
3277
+ value2 = unwrapForStore(value2);
3236
3278
  const existed = target.has(key);
3237
3279
  const oldValue = target.get(key);
3238
3280
  const oldSize = target.size;
@@ -3253,7 +3295,7 @@ function wrapMap(map, emit, path = []) {
3253
3295
  path: cachedPath,
3254
3296
  key,
3255
3297
  oldValue,
3256
- newValue: value2
3298
+ newValue: value2 !== null && typeof value2 === "object" ? deepClone(value2) : value2
3257
3299
  };
3258
3300
  emit?.(event);
3259
3301
  if (!existed) {
@@ -3470,6 +3512,10 @@ function wrapArray(arr, emit, path = []) {
3470
3512
  const methodCache = {};
3471
3513
  const proxy = new Proxy(arr, {
3472
3514
  get(target, prop, receiver) {
3515
+ if (prop === "__v_raw" /* RAW */)
3516
+ return target;
3517
+ if (prop === "__v_isReactive" /* IS_REACTIVE */)
3518
+ return true;
3473
3519
  track(target, prop);
3474
3520
  if (methodCache[prop]) {
3475
3521
  return methodCache[prop];
@@ -3729,6 +3775,8 @@ function wrapArray(arr, emit, path = []) {
3729
3775
  return value;
3730
3776
  },
3731
3777
  set(target, prop, value, receiver) {
3778
+ if (isObject2(value))
3779
+ value = unwrapForStore(value);
3732
3780
  const oldValue = target[prop];
3733
3781
  if (oldValue === value)
3734
3782
  return true;
@@ -3749,7 +3797,7 @@ function wrapArray(arr, emit, path = []) {
3749
3797
  action: "set",
3750
3798
  path: newPath,
3751
3799
  oldValue,
3752
- newValue: value
3800
+ newValue: isObject2(value) ? deepClone(value) : value
3753
3801
  };
3754
3802
  emit?.(event);
3755
3803
  if (isObject2(oldValue) && oldValue !== value) {
@@ -3774,10 +3822,6 @@ function isObject3(v) {
3774
3822
  function isReactive(value) {
3775
3823
  return !!(value && value["__v_isReactive" /* IS_REACTIVE */]);
3776
3824
  }
3777
- function toRaw(observed) {
3778
- const raw = observed && observed["__v_raw" /* RAW */];
3779
- return raw ? toRaw(raw) : observed;
3780
- }
3781
3825
  function createAsyncEmit(emit) {
3782
3826
  const queue = [];
3783
3827
  let flushScheduled = false;
@@ -3862,6 +3906,8 @@ function reactive(obj, emit, path = [], options) {
3862
3906
  return wrapValue(value, newPath);
3863
3907
  },
3864
3908
  set(target, prop, value, receiver) {
3909
+ if (isObject3(value))
3910
+ value = unwrapForStore(value);
3865
3911
  const oldValue = target[prop];
3866
3912
  if (oldValue === value)
3867
3913
  return true;
@@ -3881,7 +3927,7 @@ function reactive(obj, emit, path = [], options) {
3881
3927
  action: "set",
3882
3928
  path: newPath,
3883
3929
  oldValue,
3884
- newValue: value
3930
+ newValue: isObject3(value) ? deepClone(value) : value
3885
3931
  };
3886
3932
  emit?.(event);
3887
3933
  trigger(target, prop);
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './types';
2
- export { deepClone, deepEqual, getProxyStats, resetProxyStats } from './utils';
2
+ export { deepClone, deepEqual, getProxyStats, resetProxyStats, toRaw } from './utils';
3
3
  export * from './state';
4
4
  export * from './reactive';
5
5
  export * from './watch';
package/dist/index.js CHANGED
@@ -2300,6 +2300,35 @@ var require_reactivity_cjs = __commonJS((exports) => {
2300
2300
  exports.watch = watch2;
2301
2301
  });
2302
2302
  // src/utils.ts
2303
+ function toRaw(observed) {
2304
+ let value = observed;
2305
+ while (value && value["__v_raw" /* RAW */]) {
2306
+ value = value["__v_raw" /* RAW */];
2307
+ }
2308
+ return value;
2309
+ }
2310
+ function unwrapForStore(value) {
2311
+ if (value["__v_isReactive" /* IS_REACTIVE */])
2312
+ return toRaw(value);
2313
+ if (Array.isArray(value)) {
2314
+ for (let i = 0;i < value.length; i++) {
2315
+ const child = value[i];
2316
+ if (child != null && typeof child === "object" && child["__v_isReactive" /* IS_REACTIVE */]) {
2317
+ value[i] = toRaw(child);
2318
+ }
2319
+ }
2320
+ } else if (!(value instanceof Map) && !(value instanceof Set) && !(value instanceof Date)) {
2321
+ for (const key in value) {
2322
+ if (!Object.prototype.hasOwnProperty.call(value, key))
2323
+ continue;
2324
+ const child = value[key];
2325
+ if (child != null && typeof child === "object" && child["__v_isReactive" /* IS_REACTIVE */]) {
2326
+ value[key] = toRaw(child);
2327
+ }
2328
+ }
2329
+ }
2330
+ return value;
2331
+ }
2303
2332
  var deepEqualCache = new WeakMap;
2304
2333
  var MAX_CACHE_SIZE = 1000;
2305
2334
  var pathCache = new WeakMap;
@@ -2330,6 +2359,7 @@ function resetProxyStats() {
2330
2359
  function evictDeep(value) {
2331
2360
  if (value == null || typeof value !== "object")
2332
2361
  return;
2362
+ value = toRaw(value);
2333
2363
  if (!globalSeen.has(value))
2334
2364
  return;
2335
2365
  globalSeen.delete(value);
@@ -2985,6 +3015,10 @@ function wrapSet(set, emit, path = []) {
2985
3015
  const methodCache = {};
2986
3016
  const proxy = new Proxy(set, {
2987
3017
  get(target, prop, receiver) {
3018
+ if (prop === "__v_raw" /* RAW */)
3019
+ return target;
3020
+ if (prop === "__v_isReactive" /* IS_REACTIVE */)
3021
+ return true;
2988
3022
  track(target, prop);
2989
3023
  if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
2990
3024
  track(target, Symbol.iterator);
@@ -2994,6 +3028,8 @@ function wrapSet(set, emit, path = []) {
2994
3028
  }
2995
3029
  if (prop === "add") {
2996
3030
  methodCache[prop] = function(value2) {
3031
+ if (value2 != null && typeof value2 === "object")
3032
+ value2 = unwrapForStore(value2);
2997
3033
  const existed = target.has(value2);
2998
3034
  const oldSize = target.size;
2999
3035
  if (!existed) {
@@ -3162,6 +3198,10 @@ function wrapMap(map, emit, path = []) {
3162
3198
  const methodCache = {};
3163
3199
  const proxy = new Proxy(map, {
3164
3200
  get(target, prop, receiver) {
3201
+ if (prop === "__v_raw" /* RAW */)
3202
+ return target;
3203
+ if (prop === "__v_isReactive" /* IS_REACTIVE */)
3204
+ return true;
3165
3205
  track(target, prop);
3166
3206
  if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
3167
3207
  track(target, Symbol.iterator);
@@ -3171,6 +3211,8 @@ function wrapMap(map, emit, path = []) {
3171
3211
  }
3172
3212
  if (prop === "set") {
3173
3213
  methodCache[prop] = function(key, value2) {
3214
+ if (value2 != null && typeof value2 === "object")
3215
+ value2 = unwrapForStore(value2);
3174
3216
  const existed = target.has(key);
3175
3217
  const oldValue = target.get(key);
3176
3218
  const oldSize = target.size;
@@ -3191,7 +3233,7 @@ function wrapMap(map, emit, path = []) {
3191
3233
  path: cachedPath,
3192
3234
  key,
3193
3235
  oldValue,
3194
- newValue: value2
3236
+ newValue: value2 !== null && typeof value2 === "object" ? deepClone(value2) : value2
3195
3237
  };
3196
3238
  emit?.(event);
3197
3239
  if (!existed) {
@@ -3408,6 +3450,10 @@ function wrapArray(arr, emit, path = []) {
3408
3450
  const methodCache = {};
3409
3451
  const proxy = new Proxy(arr, {
3410
3452
  get(target, prop, receiver) {
3453
+ if (prop === "__v_raw" /* RAW */)
3454
+ return target;
3455
+ if (prop === "__v_isReactive" /* IS_REACTIVE */)
3456
+ return true;
3411
3457
  track(target, prop);
3412
3458
  if (methodCache[prop]) {
3413
3459
  return methodCache[prop];
@@ -3667,6 +3713,8 @@ function wrapArray(arr, emit, path = []) {
3667
3713
  return value;
3668
3714
  },
3669
3715
  set(target, prop, value, receiver) {
3716
+ if (isObject2(value))
3717
+ value = unwrapForStore(value);
3670
3718
  const oldValue = target[prop];
3671
3719
  if (oldValue === value)
3672
3720
  return true;
@@ -3687,7 +3735,7 @@ function wrapArray(arr, emit, path = []) {
3687
3735
  action: "set",
3688
3736
  path: newPath,
3689
3737
  oldValue,
3690
- newValue: value
3738
+ newValue: isObject2(value) ? deepClone(value) : value
3691
3739
  };
3692
3740
  emit?.(event);
3693
3741
  if (isObject2(oldValue) && oldValue !== value) {
@@ -3712,10 +3760,6 @@ function isObject3(v) {
3712
3760
  function isReactive(value) {
3713
3761
  return !!(value && value["__v_isReactive" /* IS_REACTIVE */]);
3714
3762
  }
3715
- function toRaw(observed) {
3716
- const raw = observed && observed["__v_raw" /* RAW */];
3717
- return raw ? toRaw(raw) : observed;
3718
- }
3719
3763
  function createAsyncEmit(emit) {
3720
3764
  const queue = [];
3721
3765
  let flushScheduled = false;
@@ -3800,6 +3844,8 @@ function reactive(obj, emit, path = [], options) {
3800
3844
  return wrapValue(value, newPath);
3801
3845
  },
3802
3846
  set(target, prop, value, receiver) {
3847
+ if (isObject3(value))
3848
+ value = unwrapForStore(value);
3803
3849
  const oldValue = target[prop];
3804
3850
  if (oldValue === value)
3805
3851
  return true;
@@ -3819,7 +3865,7 @@ function reactive(obj, emit, path = [], options) {
3819
3865
  action: "set",
3820
3866
  path: newPath,
3821
3867
  oldValue,
3822
- newValue: value
3868
+ newValue: isObject3(value) ? deepClone(value) : value
3823
3869
  };
3824
3870
  emit?.(event);
3825
3871
  trigger(target, prop);
@@ -3,11 +3,6 @@ import { EmitFunction, Path, ReactiveOptions } from "./types";
3
3
  * Checks if an object is a reactive proxy
4
4
  */
5
5
  export declare function isReactive(value: any): boolean;
6
- /**
7
- * Returns the raw, original object underlying a reactive proxy.
8
- * If the input is not a proxy, returns the input itself.
9
- */
10
- export declare function toRaw<T>(observed: T): T;
11
6
  /**
12
7
  * Create a reactive proxy for an object
13
8
  */
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,13 @@
1
+ /**
2
+ * Returns the raw, original object underlying a reactive proxy.
3
+ * If the input is not a proxy, returns the input itself.
4
+ */
5
+ export declare function toRaw<T>(observed: T): T;
6
+ /**
7
+ * Strip proxy refs from spread results before storing in raw state.
8
+ * Prevents unbounded proxy nesting from `{ ...reactiveProxy }` patterns.
9
+ */
10
+ export declare function unwrapForStore(value: any): any;
1
11
  export declare const pathCache: WeakMap<object, Map<string, any>>;
2
12
  export declare const pathConcatCache: Map<string, any[]>;
3
13
  export declare const globalSeen: WeakMap<any, any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yiin/reactive-proxy-state",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
4
4
  "author": "Yiin <stanislovas@yiin.lt>",
5
5
  "repository": {
6
6
  "type": "git",