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

loadio

v1.0.8

Published

Simply provides a pool and wrapper to manage the promises that generate loading status and percentage information based on the execution of tasks

Downloads

43

Readme

loadio

Npm Version License

About

Simply provides a pool and wrapper to manage the promises that generate loading status and percentage information based on the execution of tasks.

Demo

Edit Example usage - loadio

Installation

Install React Table as a dependency

# Yarn
$ yarn add loadio

# NPM
$ npm install loadio

Usage

import React from "react";
import { withPool } from "loadio"; 
const fetch = withPool(global.fetch);

class HomePage extends React.Component {

  render(){
    const { isLoading } = this.props;  
    return (
      <>
        {isLoading ? "Loading..." : "Loaded!"}
        <button onClick={() => fetch("get/data")}>Get</button>
      </>
    );
  }
}
export default withStatus(HomePage);

Hook

import { withPool, useStatus } from "loadio"; 
const fetch = withPool(global.fetch);

function HomePage() {
  const status = useStatus();
  return (
    <>
      {status.isLoading ? "Loading..." : "Loaded!"}
      <button onClick={() => fetch("get/data")}>Get</button>
    </>
  );
}

Promises can also be added directly to the pool without wrapping it

import { PoolManager, useStatus } from "loadio"; 

function HomePage() {
  const status = useStatus();
  return (
    <>
      {status.isLoading ? "Loading..." : "Loaded!"}
      <button onClick={() => PoolManager.append(fetch("get/data"))}>Get</button>
    </>
  );
}

Example

For this example, loading component will automatically appear when wrapped fetch function called Wrap the function you want to show while running

fetch.js
import { withPool } from "loadio"; 
export const fetch = withPool(global.fetch); 

Wrap the main component with 'withStatus()' to inject pool information. The information to be added to the component is as follows:

{
    // default
    isLoading?: boolean,
    percentage?: number,
    runningTasks?: number,
    // This property will only be filled to when using a different pool than 'default'
    // Pool keys must be given while wrapping otherwise this value is empty
    statusList?: any[] {
        [customPoolKey:string]:{
            isLoading?: boolean,
            percentage?: number,
            runningTasks?: number
        },
        ...
    }
}
index.js
import { withStatus } from "loadio";

class Main extends React.Component {
  render() {
    const { isLoading, percentage } = this.props;
    return (
      <>
        <MyLoadingComponent open={isLoading} percentage={percentage} />
        <ExamplePage />
      </>
    );
  }
}
export default withStatus(Main);
ExamplePage.jsx

Call the wrapped promise anywhere to show loading screen When run multiple times at the same time, it will also create a percentage rate until all promises are finished.

import { fetch } from "./fetch.js";

getData = () => {
  fetch("http://example.com/movies.json");
};
 

Or

const fetch = withPool(global.fetch);

getData = () => {
  fetch("http://example.com/movies.json");
};
 

Or it can be created multiple times dynamically.

getData = () => {
  withPool(global.fetch)("http://example.com/movies.json");
}; 

Multiple usage

The withPool wrapper can be created with a different key and can be used in different screens. Second parameter value is 'default' by default.

fetch.js
import { withPool } from "loadio";
export const fetch = withPool(global.fetch, "fetch"); 
longRunningTask.js
import { withPool } from "loadio";

function myLongRunningTask() {
  return new Promise((resolve) => setTimeout(resolve, 10000));
}
export const longRunningTask = withPool(myLongRunningTask, "longRunningTask"); 
index.js

The root loading props(isLoading, percentage, runningTasks) come null when using multiple pool keys except for the 'statusList' prop. Both pools can be connected to a single page or can be used individually.

import { withStatus } from "loadio";

class Main extends React.Component {
  render() {
    const { isLoading, percentage, statusList } = this.props;
    var fetchStatus = statusList["fetch"] ?? {};
    var lrtStatus = statusList["longRunningTask"] ?? {};
    return (
      <>
        <MyLoadingComponent
          open={fetchStatus.isLoading}
          percentage={fetchStatus.percentage}
        />
        <MyLoadingComponent
          open={lrtStatus.isLoading}
          percentage={lrtStatus.percentage}
        />
        <ExamplePage />
      </>
    );
  }
}
export default withStatus(Main, {
  poolKey: ["longRunningTask", "fetch"],
});

Or bind only one pool 'longRunningTask' instead of 'default'. Thus, instead of coming statuses as the list, the bound pool status comes from the props in the root directly.

import { withStatus } from "loadio";

class Main extends React.Component {
  render() {
    const { isLoading, percentage, statusList } = this.props;
    return (
      <>
        <MyLoadingComponent
          open={isLoading}
          percentage={percentage}
        />
        <ExamplePage />
      </>
    );
  }
}
export default withStatus(Main, {
  poolKey: "longRunningTask",
});

API

Check here for the API document

License

MIT - Mustafa Kuru