Git Product home page Git Product logo

async-computed's Introduction

@braw/async-computed

Compute values asynchronously with the Vue Composition API.

Example usage

<script setup>
  import { asyncComputed } from 'async-computed'
  import { ref } from 'vue'

  const counter = ref(2)

  const multiplication = asyncComputed(async () => {
    const count = counter.value

    await new Promise((resolve) =>
      setTimeout(() => resolve(), Math.random() * 2000),
    )

    if (Math.random() > 0.8) {
      throw new Error('Random is not on your side right now.')
    }

    return count * 2
  })

  console.log(multiplication)
</script>

<template>
  <input type="number" v-model="counter" />
  <div v-if="multiplication.pending">Calculating...</div>
  <div v-if="multiplication.fulfilled">
    {{ counter }} multiplied by 2 is {{ multiplication.value }}
  </div>
  <div v-if="multiplication.rejected" style="color: red">
    Failed to calculate value of {{ counter }} multiplied by 2
    <pre><code>{{ multiplication.error }}</code></pre>
  </div>
</template>

๐Ÿ’  Open in Vue SFC

API

Note: all methods are thoroughly documented using JSDoc available in TypeScript declaration. This is a short representation of that documentation.

asyncComputed

asyncComputed is the only method exported by this package. It is a function that accepts either a function (getter function) or an object of Options, which has both watch and getter functions, the latter accepts watched values as an argument.

Every getter function (but not watcher) is executed with this bound to an object of AsyncGetterThis, which contains function to register a callback for the cancellation of the call and checking whether the call has been cancelled.

It returns a reactive AsyncComputedRef object.

AsyncComputedOptions

Properties:

  • watch: a method that is called to retrieve values of reactive references in synchronous context. It can return a value that will be later passed to the getter as an argument.
  • get: a method that is called to asynchronously compute the value for the reactive values. If watch has returned a value, it will be provided as the first argument. This function can be both synchronous (return value directly) or synchronous (return a promise). It is called with this set to AsyncGetterThis object.

AsyncGetterThis

Properties:

  • canceled: a getter that returns a boolean which will be true if reactive values have changed and any further computation in that call is redundant as any return value will be discarded.
  • onCancel: a method that takes a function to be called when the computation gets cancelled. Can be used multiple times with different callbacks, each of which will be subscribed to an event of possible cancellation. This can be used to create AbortController.

AsyncComputedRef

Properties:

  • value: a getter that returns fulfilled value (or undefined if value is not available because the computation is still pending or has been rejected).
  • status: a getter that returns a string representation of the current status (pending, fulfilled, rejected).
  • error: a getter that returns the error that the computation has been rejected with, or undefined if it never rejected.
  • pending: a getter that returns a boolean value which indicates whether the current computation is still pending.
  • fulfilled: a getter that returns a boolean value which indicates whether the current computation has been fulfilled.
  • rejected: a getter that returns a boolean value which indicates whether the current computation has been rejected.

Note:

Because it is a custom object, AsyncComputedRef does not work like the other references in Vue: it cannot be de-referenced, it is not picked up by the compiler in setup function (requiring mandatory usage of .value).

Compatibility with Vue 2

This package should be compatible with Vue 2, but this is not guaranteed. Vue 3 is recommended.

async-computed's People

Contributors

brawaru avatar

Stargazers

 avatar

Watchers

 avatar

async-computed's Issues

Dangling promises have the potential of crashing Node.js apps

Unlike browsers which just fire error event, Node.js crashes immediately after encountering an unhandled rejection, which makes this module incredibly dangerous to use in SSR environment. I think it's worth reverting the changes that made this dangling behaviour possible, as first versions of this module always handled promises. How do you chain an error then though, that's the question...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.