-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dispatches in watchEffect should no longer trigger when selector…
… is changed
- Loading branch information
1 parent
cd41c34
commit 20973fb
Showing
3 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/vue-redux/src/utils/use-trackless-shallow-ref-with-equality-check.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { customRef } from 'vue' | ||
import type { Ref } from 'vue' | ||
import type { EqualityFn } from '../types' | ||
|
||
/** | ||
* In some instances, `watch` will attempt to read the value from an inner ref, | ||
* even when not part of the `() => ...` function. This can cause unwanted | ||
* side effects to occur. | ||
* | ||
* To sidestep this, we can expose the inner value as an untracked property | ||
*/ | ||
export function useTracklessShallowRefWithEqualityCheck<T>( | ||
initialValue: T, | ||
equalityFn: EqualityFn<T>, | ||
): { | ||
untrackedValue: { get(): T; set(_newValue: T): void } | ||
ref: Ref<T> | ||
} { | ||
let untrackedValue = initialValue | ||
return { | ||
// Avoid stale values by exposing the inner value as an untracked property | ||
untrackedValue: { | ||
get() { | ||
return untrackedValue | ||
}, | ||
set(_newValue: T) {}, | ||
}, | ||
ref: customRef((track, trigger) => { | ||
return { | ||
get() { | ||
track() | ||
return untrackedValue | ||
}, | ||
set(newValue: T) { | ||
if (equalityFn(untrackedValue, newValue)) { | ||
return | ||
} | ||
untrackedValue = newValue | ||
trigger() | ||
}, | ||
} | ||
}), | ||
} | ||
} |