Skip to content

Commit

Permalink
feat(syncRef): support more watch source type (vitest-dev#918)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
zmtlwzy and antfu committed Nov 14, 2021
1 parent 23356b2 commit ca0ff73
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/shared/syncRef/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, watch } from 'vue-demi'
import { Ref, watch, WatchSource } from 'vue-demi'
import { ConfigurableFlushSync } from '../utils'

export interface SyncRefOptions extends ConfigurableFlushSync {
Expand All @@ -22,19 +22,20 @@ export interface SyncRefOptions extends ConfigurableFlushSync {
* @param source source ref
* @param targets
*/
export function syncRef<R extends Ref<any>>(source: R, targets: R | R[], {
flush = 'sync',
deep = false,
immediate = true,
}: SyncRefOptions = {}) {
export function syncRef<T>(
source: WatchSource<T>,
targets: Ref<T> | Ref<T>[],
{
flush = 'sync',
deep = false,
immediate = true,
}: SyncRefOptions = {}) {
if (!Array.isArray(targets))
targets = [targets]

return watch(source, (newValue) => {
(targets as R[]).forEach(target => target.value = newValue)
}, {
flush,
deep,
immediate,
})
return watch(
source,
newValue => (targets as Ref<T>[]).forEach(target => target.value = newValue),
{ flush, deep, immediate },
)
}

0 comments on commit ca0ff73

Please sign in to comment.