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

jrd_task_loader_progress

v1.0.4

Published

Provides a component to handle multi-task requests and show the progress for the client.

Downloads

23

Readme

TaskLoaderProgress (React Component)

The TaskLoaderProgress component provides a mechanism to perform recurrent requests to an API endpoint and update the execution progress. It utilizes React's context to share progress information across its children.

npm i jrd_task_loader_progress

Props

TaskLoaderProgressProps<T, R>

  • taskLoader: TaskLoaderFunction<T>:

    • A function defined to retrieve the data. It takes arguments as input and returns a promise with a TaskResponse.
  • returnData: ReturnDataFunction:

    • A function that returns the response data to the parent component once the task processing is complete.
  • request: R | undefined:

    • The request arguments, which can be any type based on the specific implementation. It may also be undefined.
  • totalTasks: number:

    • The total number of tasks that are expected to be executed by the request.
  • fetchInterval: number (default: 1000):

    • The interval in milliseconds to execute the request function. If not specified, defaults to 1000 ms.
  • children?: ReactNode:

    • The child components that will be wrapped inside the TaskLoaderProgress context provider.

Context

TaskLoaderProgressContext

  • Provides the current progress and state of the tasks being executed.

  • progress: number:

    • The current progress percentage (0 to 100).
  • buffer: number:

    • The buffer percentage indicating how much data is pending.
  • taskId: number:

    • The ID of the current task being executed.

Example Usage

import React from 'react';
import { TaskLoaderProgress } from './path/to/TaskLoaderProgress';

const taskLoaderFunction = async (request) => {
  // Implementation of task loading logic
};

const returnDataFunction = (data) => {
  console.log('Data returned:', data);
};

// Usage within a component
const MyComponent = () => (
  <TaskLoaderProgress<User, IRegisterRequest>
    taskLoader={taskLoaderFunction}
    returnData={returnDataFunction}
    request={{ key1: 'value1' }}
    totalTasks={10}
    fetchInterval={2000}
  >
    {/* Child components can access context values */}
    <RingProgressBar />
  </TaskLoaderProgress>
);

{/* Child component*/}
const RingProgressBar = () => {
  const theme = useTheme();
  const { progress, buffer, taskId } = useContext(TaskLoaderProgressContext);

  const message = TASK_MSG_MAP[taskId];

  return (
      <Stack alignItems="center" spacing={2} sx={{ width: "30%" }}>
        <RingLoader color={theme.palette.primary.main} />
        <Typography variant="subtitle2">{`${message}: ${progress}%`}</Typography>
        <Box sx={{ width: "100%" }}>
          <LinearProgress
            variant="buffer"
            value={progress}
            valueBuffer={buffer}
          />
        </Box>
      </Stack>
  );
};

Internal Logic

  • Effect Hook:

    • The component uses the useEffect hook to set up an interval for fetching data and updating progress.
  • Progress and Buffer Management:

    • The component maintains state for progress, buffer, and taskId, updating them based on the response from the taskLoader function.
  • Final Task Handling:

    • When all tasks are completed, the progress is set to 100, and the returnData function is called with the final result after a short delay.

Notes

  • The component is designed to be flexible and reusable across different parts of an application where task loading and progress tracking are needed.

TaskLoaderProgress <T,R> Properties

| | | | |----------------------------------|-------------------|-------------------------------------------------------------------------------------| | Property | Type | Description | | taskLoader | TaskLoaderFunction<T> | Request function defined to retrieve the data. | | returnData | ReturnDataFunction | Function to return the response data to the parent component. | | request | R or undefined | Type used as request arguments; can be of type R or undefined. | | totalTasks | number | Total number of tasks to be executed by the request. | | fetchInterval | number (optional) | The interval in milliseconds to execute the request function (defaults to 1000 ms). | | children | ReactNode | Component children that can be rendered within this component. |