@yiin/reactive-proxy-state 1.0.14 → 1.0.15
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.js +24 -2
- package/dist/watch-effect.d.ts +7 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -410,6 +410,18 @@ function cleanupEffect(effect) {
|
|
|
410
410
|
effect.dependencies.clear();
|
|
411
411
|
}
|
|
412
412
|
}
|
|
413
|
+
function runCleanupFunctions(effect) {
|
|
414
|
+
if (effect.cleanupFns && effect.cleanupFns.length > 0) {
|
|
415
|
+
effect.cleanupFns.forEach((cleanupFn) => {
|
|
416
|
+
try {
|
|
417
|
+
cleanupFn();
|
|
418
|
+
} catch (error) {
|
|
419
|
+
console.error("Error in effect cleanup function:", error);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
effect.cleanupFns = [];
|
|
423
|
+
}
|
|
424
|
+
}
|
|
413
425
|
function track(target, key) {
|
|
414
426
|
if (!activeEffect || !activeEffect.active)
|
|
415
427
|
return;
|
|
@@ -510,9 +522,16 @@ function watchEffect(effectCallback, options = {}) {
|
|
|
510
522
|
}
|
|
511
523
|
const previousEffect = activeEffect;
|
|
512
524
|
try {
|
|
525
|
+
runCleanupFunctions(effectFn);
|
|
513
526
|
cleanupEffect(effectFn);
|
|
514
527
|
setActiveEffect(effectFn);
|
|
515
|
-
|
|
528
|
+
const onCleanup = (cleanupFn) => {
|
|
529
|
+
if (!effectFn.cleanupFns) {
|
|
530
|
+
effectFn.cleanupFns = [];
|
|
531
|
+
}
|
|
532
|
+
effectFn.cleanupFns.push(cleanupFn);
|
|
533
|
+
};
|
|
534
|
+
return effectCallback(onCleanup);
|
|
516
535
|
} finally {
|
|
517
536
|
setActiveEffect(previousEffect);
|
|
518
537
|
}
|
|
@@ -522,13 +541,15 @@ function watchEffect(effectCallback, options = {}) {
|
|
|
522
541
|
dependencies: new Set,
|
|
523
542
|
options,
|
|
524
543
|
active: true,
|
|
525
|
-
_rawCallback: effectCallback
|
|
544
|
+
_rawCallback: effectCallback,
|
|
545
|
+
cleanupFns: []
|
|
526
546
|
};
|
|
527
547
|
if (!options.lazy) {
|
|
528
548
|
effectFn.run();
|
|
529
549
|
}
|
|
530
550
|
const stopHandle = () => {
|
|
531
551
|
if (effectFn.active) {
|
|
552
|
+
runCleanupFunctions(effectFn);
|
|
532
553
|
cleanupEffect(effectFn);
|
|
533
554
|
effectFn.active = false;
|
|
534
555
|
queuedEffects.delete(effectFn);
|
|
@@ -1454,6 +1475,7 @@ export {
|
|
|
1454
1475
|
setPathConcat,
|
|
1455
1476
|
setInPathCache,
|
|
1456
1477
|
setActiveEffect,
|
|
1478
|
+
runCleanupFunctions,
|
|
1457
1479
|
ref,
|
|
1458
1480
|
reactive,
|
|
1459
1481
|
pathConcatCache,
|
package/dist/watch-effect.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type EffectCallback<T = any> = () => T;
|
|
1
|
+
type EffectCallback<T = any> = (onCleanup?: (cleanupFn: () => void) => void) => T;
|
|
2
2
|
type Scheduler = (job: () => void) => void;
|
|
3
3
|
export interface WatchEffectStopHandle<T = any> {
|
|
4
4
|
(): void;
|
|
@@ -11,6 +11,7 @@ export interface TrackedEffect<T = any> {
|
|
|
11
11
|
active?: boolean;
|
|
12
12
|
_rawCallback: EffectCallback<T>;
|
|
13
13
|
triggerDepth?: number;
|
|
14
|
+
cleanupFns?: (() => void)[];
|
|
14
15
|
}
|
|
15
16
|
export declare let activeEffect: TrackedEffect<any> | null;
|
|
16
17
|
export declare function setActiveEffect(effect: TrackedEffect<any> | null): void;
|
|
@@ -19,6 +20,11 @@ export declare function setActiveEffect(effect: TrackedEffect<any> | null): void
|
|
|
19
20
|
* this is crucial to prevent memory leaks and unnecessary updates when an effect is stopped or re-run.
|
|
20
21
|
*/
|
|
21
22
|
export declare function cleanupEffect(effect: TrackedEffect<any>): void;
|
|
23
|
+
/**
|
|
24
|
+
* runs all cleanup functions registered for an effect and clears them.
|
|
25
|
+
* called before re-running an effect or when stopping it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function runCleanupFunctions(effect: TrackedEffect<any>): void;
|
|
22
28
|
/**
|
|
23
29
|
* establishes a dependency between the currently active effect and a specific object property.
|
|
24
30
|
* called by proxy getters or ref getters.
|