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

@mhellams/mth-use-poll

v0.1.0

Published

A React hook that runs a poll to check for a condition to be met, and if the condition is not met within a certain amount of time, the polling is stopped.

Downloads

2

Readme

mth-use-poll

A React hook that runs a poll to check for a condition to be met, and if the condition is not met within a certain amount of time, the polling is stopped.

Parameters

  • options (object): The options to pass to usePoll.
    • fn (function): The callback function that checks for a condition to be met. It receives the interval ID as a parameter and should call clearInterval to stop the polling when the condition is met.
    • ms (number, optional): The interval time in milliseconds for the polling. Defaults to 500ms.
    • pollCount (number, optional): The maximum number of times the polling function should run before it stops if the condition is not met. Defaults to 10.
    • pollFailsFn (function, optional): A callback function that runs if the condition that is checked for never comes to conclusion and the pollCount is met.

Example Usage

import React, { useState } from 'react';
import { usePoll } from '@mhellams/mth-use-poll';

const PollingComponent = () => {
  const [data, setData] = useState(null);

  const pollFailsFn = ()=>{
  	console.error('Never received a response')
  }

  const fetchData = async (intervalToClear) => {
    const response = await fetch('https://api.example.com/data');
    const result = await response.json();

    if (result.conditionMet) {
      setData(result.data);
      clearInterval(intervalToClear);
    }
  };

  usePoll({ fn: fetchData, ms: 1000, pollCount: 5, pollFailsFn });

  return <div>{data ? <p>Data: {data}</p> : <p>Loading...</p>}</div>;
};

export default PollingComponent;

Details

  • Polling Mechanism: The usePoll hook sets up an interval that runs the provided callback function at the specified interval (ms). The callback function receives the interval ID, which can be used to clear the interval and stop polling when the desired condition is met.
  • Automatic Stop: The polling will automatically stop if the specified pollCount is reached, ensuring that the polling does not run indefinitely if the condition is never met A callback function can be run if the poll us automatically stopped.
  • Usage with API Calls: This hook is particularly useful for scenarios where you need to repeatedly check an external condition, such as the result of an API call, and stop checking once the condition is satisfied.

Parameters in Detail

  • fn: This callback function is the core of the polling mechanism. It should contain the logic to check if the condition is met and call clearInterval with the provided interval ID to stop further polling.
  • ms: This parameter defines the time interval between each poll. Adjust this based on how frequently you need to check the condition.
  • pollCount: This parameter limits the number of polling attempts, preventing infinite polling loops and ensuring that your application does not keep checking indefinitely if the condition cannot be met.
  • pollFailsFn: The callback function that is run if the condition being checked for is never met.

Tips

  • Handling Side Effects: Ensure that any side effects within the callback function (like state updates) are properly managed to avoid memory leaks or unnecessary re-renders.
  • Error Handling: Implement error handling within the callback function to manage any potential issues that arise during polling, such as network errors or unexpected responses.

Liscense

MIT

This documentation provides a comprehensive guide on how to use the usePoll hook, including an example to illustrate its usage in a real-world scenario.