Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(until): .not returns new instance #2224

Merged
merged 4 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}