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

resuspend

v1.0.0-rc10

Published

<!-- markdownlint-disable-next-line --> <p align="center"> <a href="https://resuspend.js.org" rel="noopener" target="_blank"><img height="100" src="https://gist.githubusercontent.com/json2d/ffbf9ae39c31a3e1ea1c84277e157f1d/raw/7fa47e68047eb7eaf1bae30eb3

Downloads

15

Readme

this unassuming library provides a <Suspension/> component which is designed to work together w/ the <Suspense/> component from the React API in order to facilitate whatever suspension logic you may wish to implement in your React app, with an initial focus on content fulfillment and user experience pacing as the primary use cases

npm node license downloads Travis

getting started

to install the latest stable version:

npm i resuspend -S

to import it into a module:

import { Suspension } from 'resuspend';

basic example

here's a simple example of a <DramaticPause/> component that waits for some amount of time specified by its duration prop before finally rendering its children prop:

import { Suspense, useState, useEffect } from 'react';
import { Suspension } from 'resuspend';

export function DramaticPause(props) {
  // state mapped to the activation status
  const [show, setShow] = useState(!props.duration);

  // activation effect - called when the activation status is set to active
  useEffect(
    function waitAndThenShow() {
      if (!show) {
        const timeoutId = setTimeout(() => setShow(true), props.duration);

        // cleanup callback
        return () => clearTimeout(timeoutId);
      }
    },
    [show, props.duration]
  );

  return (
    <Suspense fallback={<>[pausing dramatically...]</>}>
      <Suspension active={!show}>{props.children}</Suspension>
    </Suspense>
  );
}

try it out in a live editor via CodeSandbox ✨

props reference

as you can see here above w/ the <Suspension/> component working in conjuntion w/ the useEffect hook:

  • the active prop is used to declare whether its activation status is active or inactive

  • the waitAndTheShow effect is used to declare an activation effect to specify what happens when its activation status is switched to active or inactive

essentially, the suspension logic is composed of and encapsulated by these values.

data fetching example

more commonly, your suspension logic will probably involve fetching some data from a remote API service and rendering it in some kind of way.

here's a simple example of a <UserStatus> component that does just that:

import { Suspense, useState, useEffect } from 'react';
import { Suspension } from 'resuspend';

export function UserStatus(props) {
  // state mapped to the activation status
  const [status, setStatus] = useState();

  // activation effect - called when the activation status is set to active
  useEffect(
    function fetchStatus() {
      if (!status) {
        fetch(`/api/status/${props.userId}`)
          .then((response) => response.json())
          .then((data) => setStatus(data.status));
      }
      x;
    },
    [status, props.userId]
  );

  return (
    <Suspense fallback={<>[fetching status...]</>}>
      <Suspension active={!status}>{status}</Suspension>
    </Suspense>
  );
}

try it out now in a live editor via CodeSandbox ✨

activation

to set the activation status to active, you would simply assign the prop active={true}:

<Suspense fallback={<>[pausing dramatically...]</>}>
  <Suspension active={true}>
    you will not be seeing this text (except here in code) 👀
  </Suspension>
</Suspense>

that's all you need to activate a <Suspension/> component, consequently triggering its ancestral <Suspense/> component and causing it to suspend updating and rendering of its children and instead rendering its fallback prop.

inactivation

likewise to set the activation status to inactive, you would simply assign the prop active={false}:

<Suspense fallback={<>[pausing dramatically...]</>}>
  <Suspension active={false}>you will be seeing this text 👀</Suspension>
</Suspense>

you may also assign a callback to the active prop and it will be evaluated to active (for truthy values) or inactive (for falsey values.)

<Suspense fallback={<>[pausing randomly and dramatically...]</>}>
  <Suspension active={() => Math.random() >= 0.5}>
    you will maybe be seeing this text 👀
  </Suspension>
</Suspense>

reactivation

in general, using a basic callback is a fine way to switch an activation status from inactive to active. this callback can simply be attached to a useEffect hook or perhaps to some UI element event, e.g. a button click:

import { Suspense, useState, useCallback } from 'react';
import { Suspension } from 'resuspend';

export function DramaticPause(props) {
  // state mapped to the activation status
  const [show, setShow] = useState(!props.duration);

  // activation effect callback - called when the activation status is set to active
  useEffect(
    function waitAndThenShow() {
      if (!show) {
        const timeoutId = setTimeout(() => setShow(true), props.duration);

        // cleanup callback - called when the activation status is switched to inactive
        return () => clearTimeout(timeoutId);
      }
    },
    [show, props.duration]
  );

  // (re)activation callback
  const reload = useCallback(() => setShow(false));

  return (
    <>
      <Suspense fallback={<>[pausing dramatically...]</>}>
        <Suspension active={!show}>{props.children}</Suspension>
      </Suspense>
      <button disabled={!show} onClick={reload}>
        Reload
      </button>
    </>
  );
}

try it out now in a live editor via CodeSandbox ✨

lifecycle

as implied so far, a <Suspension/> component may be activated, deactivated, then reactivated and deactivated again, and so on until it's unmounted.

this flow of state encapsulates the lifecycle of <Suspension/> components, and is designed intentionally to provide a level of continuity and reusability you'd expect from a decent React component library (especially one whose name starts with re-!)