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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hardlydifficult/poller

v1.0.8

Published

A lightweight, generic polling utility with debounced triggers, overlapping request handling, and deep equality change detection.

Readme

@hardlydifficult/poller

A lightweight, generic polling utility with debounced triggers, overlapping request handling, and deep equality change detection.

Installation

npm install @hardlydifficult/poller

Quick Start

import { Poller } from "@hardlydifficult/poller";

// Define a fetch function (e.g., API call)
const fetchUser = async () => {
  const res = await fetch("https://api.example.com/user");
  return res.json();
};

// Create and start the poller
const poller = new Poller(
  fetchUser,
  (current, previous) => {
    console.log("User updated:", current);
  },
  5000 // Poll every 5 seconds
);

await poller.start();
// Polls every 5s, fires onChange only when JSON data changes

// Manually trigger a debounced poll (e.g., after user action)
poller.trigger(1000); // Waits 1s, then polls once

// Stop polling when no longer needed
poller.stop();

Polling Lifecycle

Start and Stop

  • start(): Begins polling at the configured interval. Idempotent—safe to call multiple times.
  • stop(): Cancels timers and clears any pending debounced trigger. Safe to call multiple times.
await poller.start();
await poller.start(); // No-op

poller.stop();
poller.stop(); // No-op

Change Detection

Deep Equality via JSON

Change detection uses JSON.stringify() comparison, enabling structural equality checks for objects and arrays—even when references differ.

const fetchFn = async () => ({ items: [1, 2, 3] });
const onChange = (current, previous) => {
  // Fires only when structure changes
};

const poller = new Poller(fetchFn, onChange, 1000);
// Even if new object reference returned, onChange won’t fire unless JSON differs

On Change Callback

onChange(current: T, previous: T | undefined): void
  • current: Most recently fetched value
  • previous: Prior value, or undefined on first poll

Error Handling

Optional Error Callback

Provide an onError handler to manage fetch failures; polling continues regardless.

const poller = new Poller(
  fetchFn,
  onChange,
  5000,
  (error) => {
    console.warn("Polling error:", error);
  }
);
  • Errors do not halt the polling interval.
  • If onError is omitted, errors are silently suppressed.

Manual Triggering

Debounced Triggers

  • trigger(debounceMs?: number): Schedules a one-time poll after a debounce delay (default: 1000ms).
  • Multiple rapid calls cancel previous timeouts—only the last one fires.
poller.trigger(2000); // Polls in 2s
poller.trigger(2000); // Cancelled, re-schedule to 2s from now
poller.trigger(2000); // Cancelled again, final delay applies
  • No-op if poller is not running.

Overlap Handling

  • Concurrent fetches are skipped—only one in-flight request is allowed at a time.
  • Prevents resource waste and race conditions during slow network calls.
// Interval fires every 5s; if fetch takes 6s:
// - Second interval fire is skipped
// - Third interval fire executes after first completes

API Reference

Poller<T>

| Parameter | Type | Description | |-----------|------|-------------| | fetchFn | () => Promise<T> | Async function to fetch data | | onChange | (current: T, previous: T \| undefined) => void | Callback on value change | | intervalMs | number | Polling interval in milliseconds | | onError? | (error: unknown) => void | Optional error handler |

Methods

| Method | Signature | Description | |--------|-----------|-------------| | start | (): Promise<void> | Begin polling immediately and then at intervalMs | | stop | (): void | Cancel all timers and pending triggers | | trigger | (debounceMs?: number) => void | Trigger a one-time poll after debounce delay |