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

use-scheduled

v0.1.0

Published

React hook to handle deferring activities for a later time - once or recurring.

Downloads

1

Readme

Custom React Hook useScheduled :calendar:

React hook to handle deferring activities for a later time - once or recurring. This may be used as an analogue of setInterval or setTimeout in the React Programming Model.

While most implementations using the method described by Dan Abramov work well enough for many use cases, there are edge cases where they may fire off schedule - or in the worst cases, won't fire at all. This is because it uses setInterval without taking into account our current state. When a component re-renders in a way that updates the delay passed to useInterval the hook implementation will clearInterval and then start a new interval with setInterval.

For example, if you were to use useScheduled(fireworksCallback, 400) and useInterval(fireworksCallback, 400) in your React Component that often has the props change, you'd see the following behavior:

| Time | Render | useScheduled | useInterval | | ----: | ------ | ------------ | ----------- | | 0ms | :gift: | :calendar: | :calendar: | | 100ms | | :watch: | :watch: | | 200ms | | :watch: | :watch: | | 300ms | :gift: | :watch: | :calendar: | | 400ms | :gift: | :fireworks: | :calendar: | | 500ms | | :watch: | :watch: | | 600ms | | :watch: | :watch: | | 700ms | | :watch: | :watch: | | 800ms | | :fireworks: | :fireworks: |

useScheduled does not suffer from this fate. It keeps a record of the last time it had scheduled an interval and on any updated inputs it will reschedule from that last time instead of the current time to keep the schedule stable.

Other interval implementations are fire and forget and only execute tasks asynchronously. This is good in some cases but may limit the usefulness in others.

Installation

npm install use-scheduled

Usage

import React, { useCallback } from 'react';
import useScheduled from 'use-scheduled';

export const App = () => {
  const [callCount, setCallCount] = useState(0);

  const { lastScheduleTime } = useScheduled(
    useCallback(
      () => setCallCount(n => n + 1),
      [setCallCount]
    ),
    5000
  );

  return (
    <div>
      Our Interval has been called {callCount} times.
      It was last called at {lastScheduleTime}.
    </div>
  );
};

Don't forget to use the useCallback hook to correctly refresh the interval callback value only when dependencies have changed.

API Reference

const returnObject = useScheduled(callback, delay, options);
  • callback is a function to be called on the schedule.
  • delay is the time in milliseconds to wait between calls to the scheduled callback.
  • options is an object to allow more detailed configuration of the useScheduled hook.

Options Object

| Key | Type | Default | Description | | --- | --- | --- | --- | | suspend | boolean | false | Will suspend the interval when set. When becoming unset it will reset the interval timer. Does not stop any tasks that have already started. | | allowConcurrent | boolean | true | Whether or not to allow multiple iterations of the interval to run at the same time. | | occurrences | number | Infinity | How many times to run the scheduled task. | | deadline | number | 1000 | If for any reason the scheduled task does not run it will wait pending at least deadline milliseconds. |

Return Object

This hook returns an object with the following predefined keys.

| Key | Type | Description | | --- | --- | --- | | reset | function | Resets the interval back to the original state - including occurence count, timeouts, etc. | | scheduledCount | number | How many times this interval has been scheduled. | | successCount | number | How many times the interval has completed successfully. | | failureCount | number | How many times the interval has completed with a failure. | | lastScheduleTime | number | When the last task was successfully started. |

License

MIT Licensed