Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!:
biSyncRef
renamed to syncRef
, configable direction
- Loading branch information
Showing
10 changed files
with
127 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ packages/core/README.md | |
types | ||
playgrounds/*/pnpm-lock.yaml | ||
components.d.ts | ||
*.log |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
4 changes: 2 additions & 2 deletions
4
packages/shared/biSyncRef/demo.vue → packages/shared/syncRef/demo.vue
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
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,50 @@ | ||
import { ref } from 'vue-demi' | ||
import { syncRef } from '.' | ||
|
||
describe('syncRef', () => { | ||
it('should work', () => { | ||
const a = ref('foo') | ||
const b = ref('bar') | ||
|
||
const stop = syncRef(a, b) | ||
|
||
expect(b.value).toBe('foo') | ||
|
||
a.value = 'bar' | ||
|
||
expect(a.value).toBe('bar') | ||
expect(b.value).toBe('bar') | ||
|
||
b.value = 'foo' | ||
|
||
expect(a.value).toBe('foo') | ||
expect(b.value).toBe('foo') | ||
|
||
stop() | ||
|
||
a.value = 'bar2' | ||
|
||
expect(a.value).toBe('bar2') | ||
expect(b.value).toBe('foo') | ||
}) | ||
|
||
it('trl', () => { | ||
const left = ref('left') | ||
const right = ref('right') | ||
|
||
const stop = syncRef(left, right, { direction: 'rtl' }) | ||
|
||
expect(left.value).toBe('right') | ||
expect(right.value).toBe('right') | ||
|
||
left.value = 'bar' | ||
|
||
expect(left.value).toBe('bar') | ||
expect(right.value).toBe('right') | ||
|
||
right.value = 'foobar' | ||
|
||
expect(left.value).toBe('foobar') | ||
expect(right.value).toBe('foobar') | ||
}) | ||
}) |
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,63 @@ | ||
import type { Ref } from 'vue-demi' | ||
import { watch } from 'vue-demi' | ||
import type { ConfigurableFlushSync } from '../utils' | ||
|
||
export interface SyncRefOptions extends ConfigurableFlushSync { | ||
/** | ||
* Watch deeply | ||
* | ||
* @default false | ||
*/ | ||
deep?: boolean | ||
/** | ||
* Sync values immediately | ||
* | ||
* @default true | ||
*/ | ||
immediate?: boolean | ||
|
||
/** | ||
* Direction of syncing | ||
* | ||
* @default 'both' | ||
*/ | ||
direction?: 'ltr' | 'rtl' | 'both' | ||
} | ||
|
||
/** | ||
* Two-way refs synchronization. | ||
* | ||
* @param left | ||
* @param right | ||
*/ | ||
export function syncRef<R extends Ref<any>>(left: R, right: R, options: SyncRefOptions = {}) { | ||
const { | ||
flush = 'sync', | ||
deep = false, | ||
immediate = true, | ||
direction = 'both', | ||
} = options | ||
|
||
let stop1: Function, stop2: Function | ||
|
||
if (direction === 'both' || direction === 'ltr') { | ||
stop1 = watch( | ||
left, | ||
newValue => right.value = newValue, | ||
{ flush, deep, immediate }, | ||
) | ||
} | ||
|
||
if (direction === 'both' || direction === 'rtl') { | ||
stop2 = watch( | ||
right, | ||
newValue => left.value = newValue, | ||
{ flush, deep, immediate }, | ||
) | ||
} | ||
|
||
return () => { | ||
stop1?.() | ||
stop2?.() | ||
} | ||
} |
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