Home / File/ wait-for-update.ts — vue Source File

wait-for-update.ts — vue Source File

Architecture documentation for wait-for-update.ts, a typescript file in the vue codebase. 1 imports, 1 dependents.

File typescript VueCore Instance 1 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  c4611146_2d9c_334d_478d_8cad5a9e4b34["wait-for-update.ts"]
  db9e7bef_009d_3918_6e7d_543a36a38d75["vue"]
  c4611146_2d9c_334d_478d_8cad5a9e4b34 --> db9e7bef_009d_3918_6e7d_543a36a38d75
  0e3275eb_033e_79be_7c0a_56de4c630110["vitest.setup.ts"]
  0e3275eb_033e_79be_7c0a_56de4c630110 --> c4611146_2d9c_334d_478d_8cad5a9e4b34
  style c4611146_2d9c_334d_478d_8cad5a9e4b34 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import Vue from 'vue'

// helper for async assertions.
// Use like this:
//
// vm.a = 123
// waitForUpdate(() => {
//   expect(vm.$el.textContent).toBe('123')
//   vm.a = 234
// })
// .then(() => {
//   // more assertions...
// })
// .then(done)

interface Job extends Function {
  wait?: boolean
  fail?: (e: any) => void
}

const waitForUpdate = (initialCb: Job) => {
  let end
  const queue: Job[] = initialCb ? [initialCb] : []

  function shift() {
    const job = queue.shift()
    if (queue.length) {
      let hasError = false
      try {
        job!.wait ? job!(shift) : job!()
      } catch (e) {
        hasError = true
        const done = queue[queue.length - 1]
        if (done && done.fail) {
          done.fail(e)
        }
      }
      if (!hasError && !job!.wait) {
        if (queue.length) {
          Vue.nextTick(shift)
        }
      }
    } else if (job && (job.fail || job === end)) {
      job() // done
    }
  }

  Vue.nextTick(() => {
    if (!queue.length || (!end && !queue[queue.length - 1]!.fail)) {
      throw new Error('waitForUpdate chain is missing .then(done)')
    }
    shift()
  })

  const chainer = {
    then: nextCb => {
      queue.push(nextCb)
      return chainer
    },
    thenWaitFor: wait => {
      if (typeof wait === 'number') {
        wait = timeout(wait)
      }
      wait.wait = true
      queue.push(wait)
      return chainer
    },
    end: endFn => {
      queue.push(endFn)
      end = endFn
    }
  }

  return chainer
}

function timeout(n) {
  return next => setTimeout(next, n)
}

export { waitForUpdate }

Domain

Subdomains

Types

Dependencies

  • vue

Imported By

Frequently Asked Questions

What does wait-for-update.ts do?
wait-for-update.ts is a source file in the vue codebase, written in typescript. It belongs to the VueCore domain, Instance subdomain.
What functions are defined in wait-for-update.ts?
wait-for-update.ts defines 3 function(s): e, timeout, waitForUpdate.
What does wait-for-update.ts depend on?
wait-for-update.ts imports 1 module(s): vue.
What files import wait-for-update.ts?
wait-for-update.ts is imported by 1 file(s): vitest.setup.ts.
Where is wait-for-update.ts in the architecture?
wait-for-update.ts is located at test/helpers/wait-for-update.ts (domain: VueCore, subdomain: Instance, directory: test/helpers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free