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

busy-indicators

v1.0.1

Published

Lightweight, framework-agnostic package for managing and tracking busy states.

Downloads

90

Readme

busy-indicators

busy-indicators is a lightweight, framework-agnostic package for managing and tracking busy states across different tasks. Ideal for tracking async operations, background tasks, or other processes.

Note for React Users: This package will not trigger re-renders on state updates, so be sure to understand how it works in a React project to manage states effectively.

Installation

Install the package via npm:

npm install busy-indicators

Usage

Basic Example (Vanilla JS)

import { isBusy } from "busy-indicators";

// Set the task as busy (implicitly registers the key if enforceRegistration is not enabled)
isBusy.set("myTask", true);

// Check if the task is busy
console.log(isBusy.check("myTask")); // Output: true

// Set the task as not busy
isBusy.set("myTask", false);

// Check again
console.log(isBusy.check("myTask")); // Output: false

More of a Rearl-World Example (React)

import { isBusy } from "busy-indicators";

// OPTIONAL: Initialize the busy state manager. You can provide a custom type
// that will enforce the shape of the busy state object. It must be an object
// with enum or string keys.
isBusy.init<Record<string, any>>({
  enforceRegistration: true,
});

// Register a key to enforce its usage if `enforceRegistration` is enabled.
// You can also register a key with an initial value to enforce a specific type.
isBusy.register("fetchingData");

const MyComponent = () => {
  // Optionally register and remove it within the component lifecycle
  useEffect(() => {
    isBusy.register("fetchingData");
    return () => isBusy.remove("fetchingData");
  }, []);

  const fetchData = async () => {
    if (isBusy.check("fetchingData")) return; // Check if already busy
    isBusy.set("fetchingData"); // Sets busy state to `true` by default

    try {
      // Simulate a task
      await new Promise((resolve) => setTimeout(resolve, 1000));
    } finally {
      isBusy.set("fetchingData", false); // Mark as no longer busy
    }
  };

  useEffect(() => {
    fetchData();
  }, []);

  return (
    <div>
      <p>Fetching Data: {isBusy.check("fetchingData") ? "Loading..." : "Complete"}</p>
    </div>
  );
};

export default MyComponent;

Example with Multiple Busy States (React)

This package can track multiple tasks simultaneously by using unique keys.

import { isBusy } from "busy-indicators";

const MyComponent = () => {
  // Register multiple keys and remove them when the component unmounts
  useEffect(() => {
    isBusy.register("processA");
    isBusy.register("processB");
    return () => {
      isBusy.remove("processA");
      isBusy.remove("processB");
    };
  }, []);

  const startProcessA = () => isBusy.set("processA", true);
  const startProcessB = () => isBusy.set("processB", true);

  const stopProcessA = () => isBusy.set("processA", false);
  const stopProcessB = () => isBusy.set("processB", false);

  return (
    <div>
      <p>Process A: {isBusy.check("processA") ? "Running" : "Idle"}</p>
      <p>Process B: {isBusy.check("processB") ? "Running" : "Idle"}</p>
      <button onClick={startProcessA}>Start Process A</button>
      <button onClick={stopProcessA}>Stop Process A</button>
      <button onClick={startProcessB}>Start Process B</button>
      <button onClick={stopProcessB}>Stop Process B</button>
    </div>
  );
};

API

The isBusy singleton provides methods to optionally provide the instance with configuration options, as well as add, get, set, and remove busy states.

init

init(options?: InitOptions<T>)

  • enforceRegistration: If true, requires keys to be registered before use.
  • initialStates: Initializes the entire state object with a predefined set of keys and values.
isBusy.init({
  enforceRegistration: true,
  initialStates: { myTask: false },
});

add

add(key: string, initialState: any = false): Adds a new key with an optional initial state.

isBusy.add("myTask", false);

remove

remove(key: string): Removes a key and its associated state.

isBusy.remove("myTask");

set

set(key: string, value: any = true): Updates the state for a given key, defaulting to true.

isBusy.set("myTask", true);

get

get(key: string): any: Returns the state for the specified key or false if the key doesn’t exist.

const isTaskBusy = isBusy.check("myTask");

Why Use This Package?

  • Particularly useful in performance-sensitive applications.
  • Lightweight: Simple and minimal, it works across different frameworks, including React.
  • TypeScript Support: Fully typed for seamless integration into TypeScript projects.

Installation Requirements

This package is framework-agnostic and compatible with essentially all JavaScript/TypeScript applications.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open issues for bug reports or feature requests.

Author

Created by Angus Ryer https://ryer.io