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

react-waiter

v1.0.0-alpha.9

Published

A react promise manager

Downloads

23

Readme

React Waiter

NPM Build Status license: MIT

Installation

npm i react-waiter --save

See it in action https://billyxs.github.io/react-waiter/

Basic Usage

import React from 'react';
import { useWaiter } from 'react-waiter';

function requestCreator() {
  return Promise.resolve({ name: 'react-waiter' })
}

function Component() {
  const { 
    response, 
    isPending, 
    isResolved 
  } = useWaiter(requestCreator);

  if (isPending) {
    return <span>working...</span>;
  }

  if (response) {
    return <span>{response.name} success!</span>;
  }
}

useWaiter(requestCreator, requestParams)

useWaiter() is a react hook for handling your async requests. Provide a function(requestCreator) for react-waiter to call and you're set.

function requestCreator() {
  return getItems()
}

const waiter = useWaiter(requestCreator)

If you need to provide dynamic parameters to your request, this can be handled with the requestParams, the second useWaiter() argument.

Say we have an API to request an item by ID, called getItemById().

function requestCreator({ id }) {
  return getItemById(id)
}

const { callWaiter} = useWaiter(requestCreator, { id: 1 })

// When you are done with item 1, get item 2 using callWaiter()
callWaiter({ id: 2 })

<button onClick={() => {
  callWaiter({ id: 2})
}}>
  Get next item
</button>

default values

// The request ID of the waiter. This will increment with each call.
id: null,

// The params sent to the requestCreator based on the last request
params: undefined,

// the promise returned from the requestCreator
request: null,

// resolved data
response: null,

// rejected error
error: null,

// true when the request is pending
isPending: false,

// true when the request has resolved successfully
isResolved: false,

// true when the request has rejected/errored out
isRejected: false,

// true when the request has rejected or resolved
isCompleted: false,

// true if the request completed previously and is being called again
isRefreshing: false,

// true if the request is canceled from calling cancelWaiter  
isCanceled: false,

// unix timestamp in milliseconds when the request is initialzed
startTime: null,

// unix timestamp in milliseconds when the request is completes
endTime: null,

// duration in milliseconds for the request to complete 
elapsedTime: null,

// unix timestamp in milliseconds of the last update to any property
lastModified: null,

Hook properties

const {
  callWaiter,
  cancelWaiter,
  clearWaiter,

  // waiter data
  id,
  request,
  params, 
  response,
  error,

  // lifecyle
  isPending,
  isResolved,
  isRejected,
  isCompleted,
  isRefreshing,
  isCanceled,

  // timestamps
  startTime,
  endTime,
  elapsedTime,
  lastModified,
} = useWaiter(requestCreator);

callWaiter(params)

This will invoke your requestCreator with any new params. If your request previously succeeded before calling callWaiter(), calling it a second time will set isRefreshing to be true.

const { callWaiter } = useWaiter(() => myRequest()) 

callWaiter({ param: 'Hello' })

cancelWaiter()

If your request is currently pending, you may cancel the current request with cancelWaiter(). This simply ignores the request when it resolves or rejects.

If the request is interrupted, isCanceled will be true.

If the request completed, cancelWaiter() will have no effect.

const { cancelWaiter, isCanceled } = useWaiter(() => myRequest()) 

cancelWaiter()

clearWaiter()

This will clear all hook properties to look as if the requestCreator had never been invoked. The requestCreator will stay intact and may be called again by callWaiter().

If a request is currently running, the request will not complete and any result will be ignored.

const { clearWaiter } = useWaiter(() => myRequest()) 

clearWaiter()