@yiin/reactive-proxy-state 1.0.32 → 1.0.34
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 +23 -3
- package/dist/index.js +23 -3
- package/dist/reactive.d.ts +2 -2
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3706,12 +3706,32 @@ function toRaw(observed) {
|
|
|
3706
3706
|
const raw = observed && observed["__v_raw" /* RAW */];
|
|
3707
3707
|
return raw ? toRaw(raw) : observed;
|
|
3708
3708
|
}
|
|
3709
|
-
function
|
|
3709
|
+
function createAsyncEmit(emit) {
|
|
3710
|
+
const queue = [];
|
|
3711
|
+
let flushScheduled = false;
|
|
3712
|
+
return (event) => {
|
|
3713
|
+
queue.push(event);
|
|
3714
|
+
if (!flushScheduled) {
|
|
3715
|
+
flushScheduled = true;
|
|
3716
|
+
queueMicrotask(() => {
|
|
3717
|
+
flushScheduled = false;
|
|
3718
|
+
const batch = queue.splice(0);
|
|
3719
|
+
for (const queuedEvent of batch) {
|
|
3720
|
+
emit(queuedEvent);
|
|
3721
|
+
}
|
|
3722
|
+
});
|
|
3723
|
+
}
|
|
3724
|
+
};
|
|
3725
|
+
}
|
|
3726
|
+
function reactive(obj, emit, path = [], options) {
|
|
3710
3727
|
if (obj["__v_skip" /* SKIP */]) {
|
|
3711
3728
|
return obj;
|
|
3712
3729
|
}
|
|
3713
3730
|
if (globalSeen.has(obj))
|
|
3714
3731
|
return globalSeen.get(obj);
|
|
3732
|
+
if (emit && path.length === 0 && options?.async) {
|
|
3733
|
+
emit = createAsyncEmit(emit);
|
|
3734
|
+
}
|
|
3715
3735
|
if (emit && path.length === 0) {
|
|
3716
3736
|
try {
|
|
3717
3737
|
const initialEvent = {
|
|
@@ -3746,7 +3766,7 @@ function reactive(obj, emit, path = []) {
|
|
|
3746
3766
|
return wrapSet(val, emit, subPath);
|
|
3747
3767
|
if (val instanceof Date)
|
|
3748
3768
|
return new Date(val.getTime());
|
|
3749
|
-
return reactive(val, emit, subPath);
|
|
3769
|
+
return reactive(val, emit, subPath, options);
|
|
3750
3770
|
}
|
|
3751
3771
|
const proxy = new Proxy(obj, {
|
|
3752
3772
|
get(target, prop, receiver) {
|
|
@@ -3773,7 +3793,7 @@ function reactive(obj, emit, path = []) {
|
|
|
3773
3793
|
const oldValue = target[prop];
|
|
3774
3794
|
if (oldValue === value)
|
|
3775
3795
|
return true;
|
|
3776
|
-
if (isObject3(oldValue) && isObject3(value) && deepEqual(oldValue, value, new WeakMap))
|
|
3796
|
+
if (!options?.async && isObject3(oldValue) && isObject3(value) && deepEqual(oldValue, value, new WeakMap))
|
|
3777
3797
|
return true;
|
|
3778
3798
|
const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
3779
3799
|
const result = Reflect.set(target, prop, value, receiver);
|
package/dist/index.js
CHANGED
|
@@ -3646,12 +3646,32 @@ function toRaw(observed) {
|
|
|
3646
3646
|
const raw = observed && observed["__v_raw" /* RAW */];
|
|
3647
3647
|
return raw ? toRaw(raw) : observed;
|
|
3648
3648
|
}
|
|
3649
|
-
function
|
|
3649
|
+
function createAsyncEmit(emit) {
|
|
3650
|
+
const queue = [];
|
|
3651
|
+
let flushScheduled = false;
|
|
3652
|
+
return (event) => {
|
|
3653
|
+
queue.push(event);
|
|
3654
|
+
if (!flushScheduled) {
|
|
3655
|
+
flushScheduled = true;
|
|
3656
|
+
queueMicrotask(() => {
|
|
3657
|
+
flushScheduled = false;
|
|
3658
|
+
const batch = queue.splice(0);
|
|
3659
|
+
for (const queuedEvent of batch) {
|
|
3660
|
+
emit(queuedEvent);
|
|
3661
|
+
}
|
|
3662
|
+
});
|
|
3663
|
+
}
|
|
3664
|
+
};
|
|
3665
|
+
}
|
|
3666
|
+
function reactive(obj, emit, path = [], options) {
|
|
3650
3667
|
if (obj["__v_skip" /* SKIP */]) {
|
|
3651
3668
|
return obj;
|
|
3652
3669
|
}
|
|
3653
3670
|
if (globalSeen.has(obj))
|
|
3654
3671
|
return globalSeen.get(obj);
|
|
3672
|
+
if (emit && path.length === 0 && options?.async) {
|
|
3673
|
+
emit = createAsyncEmit(emit);
|
|
3674
|
+
}
|
|
3655
3675
|
if (emit && path.length === 0) {
|
|
3656
3676
|
try {
|
|
3657
3677
|
const initialEvent = {
|
|
@@ -3686,7 +3706,7 @@ function reactive(obj, emit, path = []) {
|
|
|
3686
3706
|
return wrapSet(val, emit, subPath);
|
|
3687
3707
|
if (val instanceof Date)
|
|
3688
3708
|
return new Date(val.getTime());
|
|
3689
|
-
return reactive(val, emit, subPath);
|
|
3709
|
+
return reactive(val, emit, subPath, options);
|
|
3690
3710
|
}
|
|
3691
3711
|
const proxy = new Proxy(obj, {
|
|
3692
3712
|
get(target, prop, receiver) {
|
|
@@ -3713,7 +3733,7 @@ function reactive(obj, emit, path = []) {
|
|
|
3713
3733
|
const oldValue = target[prop];
|
|
3714
3734
|
if (oldValue === value)
|
|
3715
3735
|
return true;
|
|
3716
|
-
if (isObject3(oldValue) && isObject3(value) && deepEqual(oldValue, value, new WeakMap))
|
|
3736
|
+
if (!options?.async && isObject3(oldValue) && isObject3(value) && deepEqual(oldValue, value, new WeakMap))
|
|
3717
3737
|
return true;
|
|
3718
3738
|
const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
3719
3739
|
const result = Reflect.set(target, prop, value, receiver);
|
package/dist/reactive.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EmitFunction, Path } from "./types";
|
|
1
|
+
import { EmitFunction, Path, ReactiveOptions } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* Checks if an object is a reactive proxy
|
|
4
4
|
*/
|
|
@@ -11,4 +11,4 @@ export declare function toRaw<T>(observed: T): T;
|
|
|
11
11
|
/**
|
|
12
12
|
* Create a reactive proxy for an object
|
|
13
13
|
*/
|
|
14
|
-
export declare function reactive<T extends object>(obj: T, emit?: EmitFunction, path?: Path): T;
|
|
14
|
+
export declare function reactive<T extends object>(obj: T, emit?: EmitFunction, path?: Path, options?: ReactiveOptions): T;
|
package/dist/types.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ export interface StateEvent {
|
|
|
12
12
|
oldValues?: any[];
|
|
13
13
|
}
|
|
14
14
|
export type EmitFunction = (event: StateEvent) => void;
|
|
15
|
+
export interface ReactiveOptions {
|
|
16
|
+
/** When true, defers emit calls to the next microtask instead of firing synchronously */
|
|
17
|
+
async?: boolean;
|
|
18
|
+
}
|
|
15
19
|
import { Ref } from './ref';
|
|
16
20
|
import { ComputedRef } from './computed';
|
|
17
21
|
export type BaseWatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T);
|