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 CompilerSFC ScriptAnalyzer 1 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  7f6ed9c7_0eee_315d_2940_fda2cc028d1f["wait-for-update.ts"]
  c5601857_7faf_30c6_efca_20de90db006c["vue"]
  7f6ed9c7_0eee_315d_2940_fda2cc028d1f --> c5601857_7faf_30c6_efca_20de90db006c
  952d5e6c_0fe9_6d39_4a74_a557dadd434e["vitest.setup.ts"]
  952d5e6c_0fe9_6d39_4a74_a557dadd434e --> 7f6ed9c7_0eee_315d_2940_fda2cc028d1f
  style 7f6ed9c7_0eee_315d_2940_fda2cc028d1f 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 CompilerSFC domain, ScriptAnalyzer 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: CompilerSFC, subdomain: ScriptAnalyzer, directory: test/helpers).

Analyze Your Own Codebase

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

Try Supermodel Free