From 1f6567f4b71741b8aa65cde56d632fd7db417ccc Mon Sep 17 00:00:00 2001 From: lsdsjy Date: Mon, 26 Sep 2022 09:50:04 +0800 Subject: [PATCH] fix(until): `.not` returns new instance (#2224) Co-authored-by: Anthony Fu --- packages/shared/until/index.test.ts | 20 +++++++++++++ packages/shared/until/index.ts | 44 ++++++++++++++--------------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/packages/shared/until/index.test.ts b/packages/shared/until/index.test.ts index e7af354bf67..4255b0c3238 100644 --- a/packages/shared/until/index.test.ts +++ b/packages/shared/until/index.test.ts @@ -58,6 +58,26 @@ describe('until', () => { }) }) + it('should support `not` as separate instances', () => { + return new Promise((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((resolve, reject) => { const r = ref(null) diff --git a/packages/shared/until/index.ts b/packages/shared/until/index.ts index 66012c89978..fed3af1a84d 100644 --- a/packages/shared/until/index.ts +++ b/packages/shared/until/index.ts @@ -66,24 +66,7 @@ export interface UntilArrayInstance extends UntilBaseInstance { toContains(value: MaybeComputedRef>>, options?: UntilToMatchOptions): Promise } -/** - * 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(r: WatchSource | MaybeComputedRef): UntilArrayInstance -export function until(r: WatchSource | MaybeComputedRef): UntilValueInstance -export function until(r: any): any { - let isNot = false - +function createUntil(r: any, isNot = false) { function toMatch( condition: (v: any) => boolean, { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {}, @@ -201,8 +184,7 @@ export function until(r: any): any { changed, changedTimes, get not() { - isNot = !isNot - return this + return createUntil(r, !isNot) as UntilArrayInstance }, } return instance @@ -218,11 +200,29 @@ export function until(r: any): any { changed, changedTimes, get not() { - isNot = !isNot - return this + return createUntil(r, !isNot) as UntilValueInstance }, } 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(r: WatchSource | MaybeComputedRef): UntilArrayInstance +export function until(r: WatchSource | MaybeComputedRef): UntilValueInstance +export function until(r: any): UntilValueInstance | UntilArrayInstance { + return createUntil(r) +}