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

suspense-utils

v1.1.0

Published

[![Demo app](https://img.shields.io/badge/demo-app-ff69b4)](https://tyson-skiba.github.io/suspense-utils/) [![Bundle size](https://badgen.net/bundlephobia/min/suspense-utils)](suspense-utils) [![Support](https://img.shields.io/badge/react-%3E%3D16.3-brigh

Downloads

3

Readme

Suspense Utils

Demo app Bundle size Support semantic-release Conventional Commits

A small library with utils that make working suspense easy as well as an error boundary that can be used so that components can have error, loading and data states.

Install

yarn add suspense-utils

Suspense

Tools used to help with suspense itself

Create Repository

This utility method is used to create a data loader that caches each request.

This requires React 16.6+

This is an example using a fetch request and a public api.

import { createRepository } from 'suspense-utils';

const dataResolver = async (searchTerm: string) => {
    const response = await fetch(`https://api.publicapis.org/entries?title=${searchTerm}`);
    const data = await response.json();

    return data;
}

export const repository = createRepository(dataResolver);

Here is a second example that asynchronously loads an svg based on an enum value.

import { createRepository } from 'suspense-utils';

export const repository = createRepository(async (sprite: SpriteType) => {
    let spritePromise: () => Promise<DynamicImportType>;

    switch (sprite) {
        case SpriteType.Sun:
            spritePromise = () => import('../svg/sun');
            break;
        case SpriteType.Thermo:
            spritePromise = () => import('../svg/thermo');
            break;
        case SpriteType.Wind:
            spritePromise = () => import('../svg/wind');
            break;
        case SpriteType.Rain:
            spritePromise = () => import('../svg/rain');
            break;
        default:
            throw new Error(`Missing implementation for ${sprite} in sprite repository`);
    }

    const resolvedModule = await spritePromise();
    return resolvedModule.default;
})

HOC

This is a wrapper that aims to keep things simple by creating a component with a loading state and error state built in that can handle repositories.

This requires React 16.8+

import { SuspenseComponent } from 'suspense-utils';
import { repository } from '../repositories';

const LayoutComponent: React.FC<LayoutComponentProps> = ({
    children: Sprite,
}) => (
    <Panel>
        <Sprite />
    </Panel>
)

export const MyComponent: React.FC = () => (
    <SuspenseComponent
        repository={repository}
        repositoryArguments={[SpriteType.Sun]}
        layoutComponent={LayoutComponent}
        loadingFallback={<div>loading</div>}
    />
)

Suspend

This is a wrapper similar to redux connect that allows you to use suspense with existing components.

This requires React 16.6+

import { suspend } from 'suspense-utils';

const MyComponentBase: React.FC = () => <div>Hi</div>;

export const MyComponent = suspend(MyComponentBase, {
    loadingFallback: <div>loading</div>
})

Error

Error helpers

Error Boundary

A simple error boundary wrapper.

This requires React 16.6+

import { ErrorBoundary, ErrorBoundaryFallbackProps } from 'suspense-utils';
import { App } from '../';

/* Will show on error state */
const FallbackView: React.FC<ErrorBoundaryFallbackProps> = ({
    error,
    retry
}) => (
    <div>
        <h4>Oh no!</h4>
        <h5>Something went wrong</h5>
        <button onClick={retry}>Click to retry</button>
    </div>
)

export const MyComponent: React.FC = () => (
    <ErrorBoundary fallback={FallbackView}>
        <App />
    </ErrorBoundary>
)

Alternatively you can set the component to retry failure, every 3 seconds stopping after 5 attempts in the below example.

import { ErrorBoundary, ErrorBoundaryFallbackProps } from 'suspense-utils';
import { App } from '../';

/* Will show on error state */
const FallbackView: React.FC = () => (
    <div>
        <h4>Oh no!</h4>
        <h5>Something went wrong</h5>
    </div>
)

export const MyComponent: React.FC = () => (
    <ErrorBoundary
        fallback={FallbackView}
        retryPolicy={{
            times: 5,
            intervalMs: 3000
        }}
    >
        <App />
    </ErrorBoundary>
)

useCallback

This hook matches the api of reacts useCallback however unlike react it pipes errors to the error boundary.
Only use this if your application does not handle the possible error state of things like event handlers, data fetching etc..

This requires React 16.6+

import { useCallack } from 'suspense-utils';

export const Component: React.FC = () => {
    const onClick = useCallack(() => {
        throw new Error('Event handler error');
    }, []);

    return <button onClick={onClick}>Click me </button>
}