Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 11
📦 Imports 10
📊 Variables & Constants 9
⚡ Async/Await Patterns 3
🟢 Vue Composition API 2
📐 Interfaces 4
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/until/index.ts

📦 Imports

Name Source
MaybeRefOrGetter vue
WatchOptions vue
WatchSource vue
ElementOf ../utils
ShallowUnwrapRef ../utils
isRef vue
nextTick vue
toValue vue
watch vue
promiseTimeout ../utils

Variables & Constants

Name Type Kind Value Exported
stop (() => void) | null let/var null
watcher Promise<T> const `new Promise((resolve) => {
stop = watch(
r,
(v) => {
if (condition(v) !== isNot) {
if (stop)
stop()
else
nextTick(() => stop?.())
resolve(v)
}
},
{
flush,
deep,
immediate: true,
},
)
})`
promises Promise<T>[] const [watcher]
stop (() => void) | null let/var null
watcher Promise<T> const `new Promise((resolve) => {
stop = watch(
[r, value],
([v1, v2]) => {
if (isNot !== (v1 === v2)) {
if (stop)
stop()
else
nextTick(() => stop?.())
resolve(v1)
}
},
{
flush,
deep,
immediate: true,
},
)
})`
promises Promise<T>[] const [watcher]
count number let/var -1
instance UntilArrayInstance<T> const `{
toMatch: toMatch as any,
toContains,
changed,
changedTimes,
get not() {
return createUntil(r, !isNot) as UntilArrayInstance
},
}`
instance UntilValueInstance<T, boolean> const `{
toMatch: toMatch as any,
toBe,
toBeTruthy: toBeTruthy as any,
toBeNull: toBeNull as any,
toBeNaN,
toBeUndefined: toBeUndefined as any,
changed,
changedTimes,
get not() {
return createUntil(r, !isNot) as UntilValueInstance
},
}`

Async/Await Patterns

Type Function Await Expressions Promise Chains
promise-chain createUntil none new Promise(...), promiseTimeout(timeout, throwOnTimeout)
.then(() => toValue(r)).finally, promiseTimeout(timeout, throwOnTimeout).then, Promise.race, new Promise(...), promiseTimeout(timeout, throwOnTimeout)
.then(() => toValue(r)).finally, promiseTimeout(timeout, throwOnTimeout).then, Promise.race
promise-chain toMatch none new Promise(...), promiseTimeout(timeout, throwOnTimeout)
.then(() => toValue(r)).finally, promiseTimeout(timeout, throwOnTimeout).then, Promise.race
promise-chain toBe none new Promise(...), promiseTimeout(timeout, throwOnTimeout)
.then(() => toValue(r)).finally, promiseTimeout(timeout, throwOnTimeout).then, Promise.race

Vue Composition API

Name Type Reactive Variables Composables
watch watch none none
watch watch none none

Functions

createUntil(r: any, isNot: boolean): UntilArrayInstance<T> | UntilValueInstance<T, boolean>

Code
function createUntil<T>(r: any, isNot = false) {
  function toMatch(
    condition: (v: any) => boolean,
    { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {},
  ): Promise<T> {
    let stop: (() => void) | null = null
    const watcher = new Promise<T>((resolve) => {
      stop = watch(
        r,
        (v) => {
          if (condition(v) !== isNot) {
            if (stop)
              stop()
            else
              nextTick(() => stop?.())
            resolve(v)
          }
        },
        {
          flush,
          deep,
          immediate: true,
        },
      )
    })

    const promises = [watcher]
    if (timeout != null) {
      promises.push(
        promiseTimeout(timeout, throwOnTimeout)
          .then(() => toValue(r))
          .finally(() => stop?.()),
      )
    }

    return Promise.race(promises)
  }

  function toBe<P>(value: MaybeRefOrGetter<P | T>, options?: UntilToMatchOptions) {
    if (!isRef(value))
      return toMatch(v => v === value, options)

    const { flush = 'sync', deep = false, timeout, throwOnTimeout } = options ?? {}
    let stop: (() => void) | null = null
    const watcher = new Promise<T>((resolve) => {
      stop = watch(
        [r, value],
        ([v1, v2]) => {
          if (isNot !== (v1 === v2)) {
            if (stop)
              stop()
            else
              nextTick(() => stop?.())
            resolve(v1)
          }
        },
        {
          flush,
          deep,
          immediate: true,
        },
      )
    })

    const promises = [watcher]
    if (timeout != null) {
      promises.push(
        promiseTimeout(timeout, throwOnTimeout)
          .then(() => toValue(r))
          .finally(() => {
            stop?.()
            return toValue(r)
          }),
      )
    }

    return Promise.race(promises)
  }

  function toBeTruthy(options?: UntilToMatchOptions) {
    return toMatch(v => Boolean(v), options)
  }

  function toBeNull(options?: UntilToMatchOptions) {
    return toBe<null>(null, options)
  }

  function toBeUndefined(options?: UntilToMatchOptions) {
    return toBe<undefined>(undefined, options)
  }

  function toBeNaN(options?: UntilToMatchOptions) {
    return toMatch(Number.isNaN, options)
  }

  function toContains(
    value: any,
    options?: UntilToMatchOptions,
  ) {
    return toMatch((v) => {
      const array = Array.from(v as any)
      return array.includes(value) || array.includes(toValue(value))
    }, options)
  }

  function changed(options?: UntilToMatchOptions) {
    return changedTimes(1, options)
  }

  function changedTimes(n = 1, options?: UntilToMatchOptions) {
    let count = -1 // skip the immediate check
    return toMatch(() => {
      count += 1
      return count >= n
    }, options)
  }

  if (Array.isArray(toValue(r))) {
    const instance: UntilArrayInstance<T> = {
      toMatch: toMatch as any,
      toContains,
      changed,
      changedTimes,
      get not() {
        return createUntil(r, !isNot) as UntilArrayInstance<T>
      },
    }
    return instance
  }
  else {
    const instance: UntilValueInstance<T, boolean> = {
      toMatch: toMatch as any,
      toBe,
      toBeTruthy: toBeTruthy as any,
      toBeNull: toBeNull as any,
      toBeNaN,
      toBeUndefined: toBeUndefined as any,
      changed,
      changedTimes,
      get not() {
        return createUntil(r, !isNot) as UntilValueInstance<T, boolean>
      },
    }

    return instance
  }
}
  • Parameters:
  • r: any
  • isNot: boolean
  • Return Type: UntilArrayInstance<T> | UntilValueInstance<T, boolean>
  • Calls:
  • watch (from vue)
  • condition
  • stop
  • nextTick (from vue)
  • resolve
  • promises.push
  • promiseTimeout(timeout, throwOnTimeout) .then(() => toValue(r)) .finally
  • Promise.race
  • isRef (from vue)
  • toMatch
  • toValue (from vue)
  • Boolean
  • toBe
  • Array.from
  • array.includes
  • changedTimes
  • Array.isArray
  • createUntil

toMatch(condition: (v: any) => boolean, { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions): Promise<T>

Code
function toMatch(
    condition: (v: any) => boolean,
    { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions = {},
  ): Promise<T> {
    let stop: (() => void) | null = null
    const watcher = new Promise<T>((resolve) => {
      stop = watch(
        r,
        (v) => {
          if (condition(v) !== isNot) {
            if (stop)
              stop()
            else
              nextTick(() => stop?.())
            resolve(v)
          }
        },
        {
          flush,
          deep,
          immediate: true,
        },
      )
    })

    const promises = [watcher]
    if (timeout != null) {
      promises.push(
        promiseTimeout(timeout, throwOnTimeout)
          .then(() => toValue(r))
          .finally(() => stop?.()),
      )
    }

    return Promise.race(promises)
  }
  • Parameters:
  • condition: (v: any) => boolean
  • { flush = 'sync', deep = false, timeout, throwOnTimeout }: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • watch (from vue)
  • condition
  • stop
  • nextTick (from vue)
  • resolve
  • promises.push
  • promiseTimeout(timeout, throwOnTimeout) .then(() => toValue(r)) .finally
  • Promise.race

toBe(value: MaybeRefOrGetter<P | T>, options: UntilToMatchOptions): Promise<T>

Code
function toBe<P>(value: MaybeRefOrGetter<P | T>, options?: UntilToMatchOptions) {
    if (!isRef(value))
      return toMatch(v => v === value, options)

    const { flush = 'sync', deep = false, timeout, throwOnTimeout } = options ?? {}
    let stop: (() => void) | null = null
    const watcher = new Promise<T>((resolve) => {
      stop = watch(
        [r, value],
        ([v1, v2]) => {
          if (isNot !== (v1 === v2)) {
            if (stop)
              stop()
            else
              nextTick(() => stop?.())
            resolve(v1)
          }
        },
        {
          flush,
          deep,
          immediate: true,
        },
      )
    })

    const promises = [watcher]
    if (timeout != null) {
      promises.push(
        promiseTimeout(timeout, throwOnTimeout)
          .then(() => toValue(r))
          .finally(() => {
            stop?.()
            return toValue(r)
          }),
      )
    }

    return Promise.race(promises)
  }
  • Parameters:
  • value: MaybeRefOrGetter<P | T>
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • isRef (from vue)
  • toMatch
  • watch (from vue)
  • stop
  • nextTick (from vue)
  • resolve
  • promises.push
  • promiseTimeout(timeout, throwOnTimeout) .then(() => toValue(r)) .finally
  • toValue (from vue)
  • Promise.race

toBeTruthy(options: UntilToMatchOptions): Promise<T>

Code
function toBeTruthy(options?: UntilToMatchOptions) {
    return toMatch(v => Boolean(v), options)
  }
  • Parameters:
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • toMatch
  • Boolean

toBeNull(options: UntilToMatchOptions): Promise<T>

Code
function toBeNull(options?: UntilToMatchOptions) {
    return toBe<null>(null, options)
  }
  • Parameters:
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • toBe

toBeUndefined(options: UntilToMatchOptions): Promise<T>

Code
function toBeUndefined(options?: UntilToMatchOptions) {
    return toBe<undefined>(undefined, options)
  }
  • Parameters:
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • toBe

toBeNaN(options: UntilToMatchOptions): Promise<T>

Code
function toBeNaN(options?: UntilToMatchOptions) {
    return toMatch(Number.isNaN, options)
  }
  • Parameters:
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • toMatch

toContains(value: any, options: UntilToMatchOptions): Promise<T>

Code
function toContains(
    value: any,
    options?: UntilToMatchOptions,
  ) {
    return toMatch((v) => {
      const array = Array.from(v as any)
      return array.includes(value) || array.includes(toValue(value))
    }, options)
  }
  • Parameters:
  • value: any
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • toMatch
  • Array.from
  • array.includes
  • toValue (from vue)

changed(options: UntilToMatchOptions): Promise<T>

Code
function changed(options?: UntilToMatchOptions) {
    return changedTimes(1, options)
  }
  • Parameters:
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • changedTimes

changedTimes(n: number, options: UntilToMatchOptions): Promise<T>

Code
function changedTimes(n = 1, options?: UntilToMatchOptions) {
    let count = -1 // skip the immediate check
    return toMatch(() => {
      count += 1
      return count >= n
    }, options)
  }
  • Parameters:
  • n: number
  • options: UntilToMatchOptions
  • Return Type: Promise<T>
  • Calls:
  • toMatch

until(r: WatchSource<T> | MaybeRefOrGetter<T>): UntilArrayInstance<T>

Code
export function until<T extends unknown[]>(r: WatchSource<T> | MaybeRefOrGetter<T>): UntilArrayInstance<T>
  • JSDoc:

    /**
     * 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!')
     * ```
     */
    

  • Parameters:

  • r: WatchSource<T> | MaybeRefOrGetter<T>
  • Return Type: UntilArrayInstance<T>

Interfaces

UntilToMatchOptions

Interface Code
export interface UntilToMatchOptions {
  /**
   * Milliseconds timeout for promise to resolve/reject if the when condition does not meet.
   * 0 for never timed out
   *
   * @default 0
   */
  timeout?: number

  /**
   * Reject the promise when timeout
   *
   * @default false
   */
  throwOnTimeout?: boolean

  /**
   * `flush` option for internal watch
   *
   * @default 'sync'
   */
  flush?: WatchOptions['flush']

  /**
   * `deep` option for internal watch
   *
   * @default 'false'
   */
  deep?: WatchOptions['deep']
}

Properties

Name Type Optional Description
timeout number
throwOnTimeout boolean
flush WatchOptions['flush']
deep WatchOptions['deep']

UntilBaseInstance<T, Not extends boolean = false>

Interface Code
export interface UntilBaseInstance<T, Not extends boolean = false> {
  toMatch: (<U extends T = T>(
    condition: (v: T) => v is U,
    options?: UntilToMatchOptions
  ) => Not extends true ? Promise<Exclude<T, U>> : Promise<U>) & ((
    condition: (v: T) => boolean,
    options?: UntilToMatchOptions
  ) => Promise<T>)
  changed: (options?: UntilToMatchOptions) => Promise<T>
  changedTimes: (n?: number, options?: UntilToMatchOptions) => Promise<T>
}

Properties

Name Type Optional Description
toMatch `((
condition: (v: T) => v is U,
options?: UntilToMatchOptions
) => Not extends true ? Promise> : Promise) & ((
condition: (v: T) => boolean,
options?: UntilToMatchOptions
) => Promise)`
changed (options?: UntilToMatchOptions) => Promise<T>
changedTimes (n?: number, options?: UntilToMatchOptions) => Promise<T>

UntilValueInstance<T, Not extends boolean = false>

Interface Code
export interface UntilValueInstance<T, Not extends boolean = false> extends UntilBaseInstance<T, Not> {
  readonly not: UntilValueInstance<T, Not extends true ? false : true>

  toBe: <P = T>(value: MaybeRefOrGetter<P>, options?: UntilToMatchOptions) => Not extends true ? Promise<T> : Promise<P>
  toBeTruthy: (options?: UntilToMatchOptions) => Not extends true ? Promise<T & Falsy> : Promise<Exclude<T, Falsy>>
  toBeNull: (options?: UntilToMatchOptions) => Not extends true ? Promise<Exclude<T, null>> : Promise<null>
  toBeUndefined: (options?: UntilToMatchOptions) => Not extends true ? Promise<Exclude<T, undefined>> : Promise<undefined>
  toBeNaN: (options?: UntilToMatchOptions) => Promise<T>
}

Properties

Name Type Optional Description
not UntilValueInstance<T, Not extends true ? false : true>
toBe <P = T>(value: MaybeRefOrGetter<P>, options?: UntilToMatchOptions) => Not extends true ? Promise<T> : Promise<P>
toBeTruthy (options?: UntilToMatchOptions) => Not extends true ? Promise<T & Falsy> : Promise<Exclude<T, Falsy>>
toBeNull (options?: UntilToMatchOptions) => Not extends true ? Promise<Exclude<T, null>> : Promise<null>
toBeUndefined (options?: UntilToMatchOptions) => Not extends true ? Promise<Exclude<T, undefined>> : Promise<undefined>
toBeNaN (options?: UntilToMatchOptions) => Promise<T>

UntilArrayInstance<T>

Interface Code
export interface UntilArrayInstance<T> extends UntilBaseInstance<T> {
  readonly not: UntilArrayInstance<T>

  toContains: (value: MaybeRefOrGetter<ElementOf<ShallowUnwrapRef<T>>>, options?: UntilToMatchOptions) => Promise<T>
}

Properties

Name Type Optional Description
not UntilArrayInstance<T>
toContains (value: MaybeRefOrGetter<ElementOf<ShallowUnwrapRef<T>>>, options?: UntilToMatchOptions) => Promise<T>

Type Aliases

Falsy

type Falsy = false | void | null | undefined | 0 | 0n | '';