Skip to content

Commit

Permalink
fix(until): .not returns new instance (#2224)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
lsdsjy and antfu committed Sep 26, 2022
1 parent 553f07a commit 1f6567f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
20 changes: 20 additions & 0 deletions packages/shared/until/index.test.ts
Expand Up @@ -58,6 +58,26 @@ describe('until', () => {
})
})

it('should support `not` as separate instances', () => {
return new Promise<void>((resolve, reject) => {
const r = ref(0)

invoke(async () => {
expect(r.value).toBe(0)
const instance = until(r)
const x = await instance.not.toBe(0)
const y = await instance.not.toBe(2)
expect(x).toBe(1)
expect(y).toBe(1)
resolve()
}).catch(reject)

setTimeout(() => {
r.value = 1
}, 100)
})
})

it('should support toBeNull()', () => {
return new Promise<void>((resolve, reject) => {
const r = ref<number | null>(null)
Expand Down
44 changes: 22 additions & 22 deletions packages/shared/until/index.ts
Expand Up @@ -66,24 +66,7 @@ export interface UntilArrayInstance<T> extends UntilBaseInstance<T> {
toContains(value: MaybeComputedRef<ElementOf<ShallowUnwrapRef<T>>>, options?: UntilToMatchOptions): Promise<T>
}

/**
* Promised one-time watch for changes
*
* @see https://vueuse.org/until
* @example
* ```
* const { count } = useCounter()
*
* await until(count).toMatch(v => v > 7)
*
* alert('Counter is now larger than 7!')
* ```
*/
export function until<T extends unknown[]>(r: WatchSource<T> | MaybeComputedRef<T>): UntilArrayInstance<T>
export function until<T>(r: WatchSource<T> | MaybeComputedRef<T>): UntilValueInstance<T>
export function until<T>(r: any): any {
let isNot = false

function createUntil<T>(r: any, isNot = false) {
function toMatch(
condition: (v: any) => boolean,
{ flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {},
Expand Down Expand Up @@ -201,8 +184,7 @@ export function until<T>(r: any): any {
changed,
changedTimes,
get not() {
isNot = !isNot
return this
return createUntil(r, !isNot) as UntilArrayInstance<T>
},
}
return instance
Expand All @@ -218,11 +200,29 @@ export function until<T>(r: any): any {
changed,
changedTimes,
get not() {
isNot = !isNot
return this
return createUntil(r, !isNot) as UntilValueInstance<T, boolean>
},
}

return instance
}
}

/**
* Promised one-time watch for changes
*
* @see https://vueuse.org/until
* @example
* ```
* const { count } = useCounter()
*
* await until(count).toMatch(v => v > 7)
*
* alert('Counter is now larger than 7!')
* ```
*/
export function until<T extends unknown[]>(r: WatchSource<T> | MaybeComputedRef<T>): UntilArrayInstance<T>
export function until<T>(r: WatchSource<T> | MaybeComputedRef<T>): UntilValueInstance<T>
export function until<T>(r: any): UntilValueInstance<T> | UntilArrayInstance<T> {
return createUntil(r)
}

0 comments on commit 1f6567f

Please sign in to comment.