|
1 |
| -import type { Ref, WatchStopHandle } from 'vue-demi' |
2 |
| -import { watch } from 'vue-demi' |
| 1 | +import type { Ref } from 'vue-demi' |
3 | 2 | import type { ConfigurableFlushSync } from '../utils'
|
| 3 | +import type { WatchPausableReturn } from '../watchPausable' |
| 4 | +import { pausableWatch } from '../watchPausable' |
4 | 5 |
|
5 | 6 | export interface SyncRefOptions<L, R = L> extends ConfigurableFlushSync {
|
6 | 7 | /**
|
@@ -47,30 +48,38 @@ export function syncRef<L, R = L>(left: Ref<L>, right: Ref<R>, options: SyncRefO
|
47 | 48 | transform = {},
|
48 | 49 | } = options
|
49 | 50 |
|
50 |
| - let watchLeft: WatchStopHandle |
51 |
| - let watchRight: WatchStopHandle |
| 51 | + const watchers: WatchPausableReturn[] = [] |
52 | 52 |
|
53 | 53 | const transformLTR = transform.ltr ?? (v => v)
|
54 | 54 | const transformRTL = transform.rtl ?? (v => v)
|
55 | 55 |
|
56 | 56 | if (direction === 'both' || direction === 'ltr') {
|
57 |
| - watchLeft = watch( |
| 57 | + watchers.push(pausableWatch( |
58 | 58 | left,
|
59 |
| - newValue => right.value = transformLTR(newValue) as R, |
| 59 | + (newValue) => { |
| 60 | + watchers.forEach(w => w.pause()) |
| 61 | + right.value = transformLTR(newValue) as R |
| 62 | + watchers.forEach(w => w.resume()) |
| 63 | + }, |
60 | 64 | { flush, deep, immediate },
|
61 |
| - ) |
| 65 | + )) |
62 | 66 | }
|
63 | 67 |
|
64 | 68 | if (direction === 'both' || direction === 'rtl') {
|
65 |
| - watchRight = watch( |
| 69 | + watchers.push(pausableWatch( |
66 | 70 | right,
|
67 |
| - newValue => left.value = transformRTL(newValue) as L, |
| 71 | + (newValue) => { |
| 72 | + watchers.forEach(w => w.pause()) |
| 73 | + left.value = transformRTL(newValue) as L |
| 74 | + watchers.forEach(w => w.resume()) |
| 75 | + }, |
68 | 76 | { flush, deep, immediate },
|
69 |
| - ) |
| 77 | + )) |
70 | 78 | }
|
71 | 79 |
|
72 |
| - return () => { |
73 |
| - watchLeft?.() |
74 |
| - watchRight?.() |
| 80 | + const stop = () => { |
| 81 | + watchers.forEach(w => w.stop()) |
75 | 82 | }
|
| 83 | + |
| 84 | + return stop |
76 | 85 | }
|
0 commit comments