npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@asyncref/vue

v0.3.0

Published

Express your async state with ease and in **one variable**.

Downloads

9

Readme

AsyncRef for Vue

Express your async state with ease and in one variable.

Features

  • Simple and easy to use
  • Comes with a set of utilities for mapping and composition
  • Typesafe with the magic of TypeScript

Usage

An asyncRef can be in state of loading, resolved or rejected.

type LoadingState = { isLoading: true }
type ResolvedState<TData> = { isLoading: false, data: TData }
type RejectedState<TError> = { isLoading: false, error: TError }

type AsyncState<TData, TError> =
  | LoadingState
  | ResolvedState<TData>
  | RejectedState<TError>

type AsyncRef<TData, TError = Error> = Ref<AsyncState<TData, TError>>

The underlying value of an asyncRef can only be accessed with proper inspection of isLoading flag that helps the type safety. The library provides a set of utilities that helps with creation, mapping and composition of asyncRef variables.

asyncRef

Allows the creation of asyncRef with optional initial value. The result will allow you to resolve, reject or reset the state of the variable.

const willBeInLoadingState = asyncRef()
const willBeInResolvedState = asyncRef('some data')

willBeInLoadingState.resolve('now it will be resolved')
willBeInLoadingState.reject('now it will be rejected')
willBeInLoadingState.reset() // and now is again in loading state

fromPromise

Allows you to convert a promise into an asyncRef. The result will be in resolved state if the promise resolves, or in rejected state if the promise rejects. You need to pass a function that returns a promise - this function will also receive an abort signal that you can use to abort the promise.

const asyncRef = fromPromise(() => fetch('https://example.com'))

fromRefs

Allows you to convert a set of refs into an asyncRef. The result will be

  • in loading state when isLoading ref is true
  • in rejected state when isLoading ref is false and isError is true with an error from error ref
  • in resolved state when isLoading ref is false and isError is false with a value from data ref

useCompose

Allows you to compose multiple asyncRefs into a single one. The result will be in resolved state when all of the asyncRefs are in resolved state, or in rejected state when at least one of the asyncRefs is in rejected state.

const a = asyncRef('Hello')
const b = asyncRef('World')

const composed = useCompose([a, b], ([aValue, bValue]) => {
  // types of aValue and bValue are inferred from the asyncRefs
  return `${aValue} ${bValue}`
})

useMap

Allows you to map an asyncRef into another one. The result will be in resolved state when the source asyncRef is in resolved state, or in rejected state when the source asyncRef is in rejected state.

const a = asyncRef('Hello')
const b = useMap(a, (aValue) => {
  // type of aValue is inferred from the asyncRef
  return `${aValue} World`
})

useMapError

Allows you to map an error of an asyncRef into another one. The result will be in resolved state when the source asyncRef is in resolved state, or in rejected state when the source asyncRef is in rejected state.

const a = asyncRef('Hello')
  
const b = useMapError(a, (aError) => {
  // type of aError is inferred from the asyncRef
  return new Error(`Error: ${aError.message}`)
})

useMatch

The asyncRef finalizer that allows you to return a value based on matched state.

const a = asyncRef('Hello')
const b = useMatch(a, {
  loading: () => 'Loading...',
  data: (aValue) => {
    // type of aValue is inferred from the asyncRef
    return `${aValue} World`
  },
  error: (aError) => {
    // type of aError is inferred from the asyncRef
    return new Error(`Error: ${aError.message}`)
  }
})

UseMatch

The Vue component that reduces the boilerplate of using useMatch with components

<template>
  <UseMatch :asyncRef="asyncRef">
    <template #loading>
      Loading...
    </template>
    <template #default="{ data }">
      {{ data }}
    </template>
    <template #error="{ error }">
      {{ error.message }}
    </template>
  </UseMatch>
</template>

useValue

The asyncRef finalizer that returns either a value or undefined based on the state of the asyncRef.

const a = asyncRef('Hello')
const aValue = useValue(a) // 'Hello'

const b = asyncRef()
const bValue = useValue(b) // undefined

Installation

npm install @asyncref/vue