From 90b55f54771283b5ea6db1c3910a7606088bb2d8 Mon Sep 17 00:00:00 2001 From: lsdsjy Date: Mon, 19 Sep 2022 17:34:21 +0800 Subject: [PATCH 1/4] fix(until): `.not` returns new instance --- packages/shared/until/index.ts | 41 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/shared/until/index.ts b/packages/shared/until/index.ts index 66012c89978..302bfce5482 100644 --- a/packages/shared/until/index.ts +++ b/packages/shared/until/index.ts @@ -66,22 +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 { +export function _until(r: any): any { let isNot = false function toMatch( @@ -226,3 +211,27 @@ export function until(r: any): any { 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): any { + return { + ..._until(r), + get not() { + return _until(r).not + }, + } +} From 7151c3a67edf2bd4e90bfd48a3b7c94bda6a12d4 Mon Sep 17 00:00:00 2001 From: lsdsjy Date: Mon, 19 Sep 2022 20:04:28 +0800 Subject: [PATCH 2/4] chore: fix and add test --- packages/shared/until/index.test.ts | 20 ++++++++++++++++++++ packages/shared/until/index.ts | 13 +++---------- 2 files changed, 23 insertions(+), 10 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 302bfce5482..670cddfc647 100644 --- a/packages/shared/until/index.ts +++ b/packages/shared/until/index.ts @@ -66,9 +66,7 @@ export interface UntilArrayInstance extends UntilBaseInstance { toContains(value: MaybeComputedRef>>, options?: UntilToMatchOptions): Promise } -export function _until(r: any): any { - let isNot = false - +export function _until(r: any, isNot = false): any { function toMatch( condition: (v: any) => boolean, { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {}, @@ -186,8 +184,7 @@ export function _until(r: any): any { changed, changedTimes, get not() { - isNot = !isNot - return this + return _until(r, !isNot) }, } return instance @@ -203,8 +200,7 @@ export function _until(r: any): any { changed, changedTimes, get not() { - isNot = !isNot - return this + return _until(r, !isNot) }, } @@ -230,8 +226,5 @@ export function until(r: WatchSource | MaybeComputedRef): UntilValueIns export function until(r: any): any { return { ..._until(r), - get not() { - return _until(r).not - }, } } From 10947cb51fb35f8b937f6f83d2b9df225f649e5e Mon Sep 17 00:00:00 2001 From: lsdsjy Date: Mon, 19 Sep 2022 23:16:29 +0800 Subject: [PATCH 3/4] fix: export --- packages/shared/until/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/shared/until/index.ts b/packages/shared/until/index.ts index 670cddfc647..06322e06cb6 100644 --- a/packages/shared/until/index.ts +++ b/packages/shared/until/index.ts @@ -66,7 +66,7 @@ export interface UntilArrayInstance extends UntilBaseInstance { toContains(value: MaybeComputedRef>>, options?: UntilToMatchOptions): Promise } -export function _until(r: any, isNot = false): any { +function _until(r: any, isNot = false): any { function toMatch( condition: (v: any) => boolean, { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {}, @@ -224,7 +224,5 @@ export function _until(r: any, isNot = false): any { export function until(r: WatchSource | MaybeComputedRef): UntilArrayInstance export function until(r: WatchSource | MaybeComputedRef): UntilValueInstance export function until(r: any): any { - return { - ..._until(r), - } + return _until(r) } From 042a2709cd9912fbb4a590dc6a58c76cd3711dee Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 26 Sep 2022 09:43:10 +0800 Subject: [PATCH 4/4] chore: update --- packages/shared/until/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/shared/until/index.ts b/packages/shared/until/index.ts index 06322e06cb6..fed3af1a84d 100644 --- a/packages/shared/until/index.ts +++ b/packages/shared/until/index.ts @@ -66,7 +66,7 @@ export interface UntilArrayInstance extends UntilBaseInstance { toContains(value: MaybeComputedRef>>, options?: UntilToMatchOptions): Promise } -function _until(r: any, isNot = false): any { +function createUntil(r: any, isNot = false) { function toMatch( condition: (v: any) => boolean, { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {}, @@ -184,7 +184,7 @@ function _until(r: any, isNot = false): any { changed, changedTimes, get not() { - return _until(r, !isNot) + return createUntil(r, !isNot) as UntilArrayInstance }, } return instance @@ -200,7 +200,7 @@ function _until(r: any, isNot = false): any { changed, changedTimes, get not() { - return _until(r, !isNot) + return createUntil(r, !isNot) as UntilValueInstance }, } @@ -223,6 +223,6 @@ function _until(r: any, isNot = false): any { */ export function until(r: WatchSource | MaybeComputedRef): UntilArrayInstance export function until(r: WatchSource | MaybeComputedRef): UntilValueInstance -export function until(r: any): any { - return _until(r) +export function until(r: any): UntilValueInstance | UntilArrayInstance { + return createUntil(r) }