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

@pomle/throb

v2.0.0

Published

Higher order functions to avoid race conditions, hammering, etc.

Downloads

38

Readme

Throb

Higher order functions to avoid race conditions, hammering, etc.

debounce

Ensures a function is only executed if it has not been called again during some time.

Usage

In the example below, we search as the user types. We only want to make a network request if the user stops typing, so that there is not one request per keystroke.

import { debounce } from "@pomle/throb";

function search(query: string) {
  return fetch("http://search.com/query=" + query).then((result) => {
    console.log(result);
  });
}

const handleType = debounce(search, 500);

document.querySelector("#search").addEventListener("input", (event) => {
  handleType(event.target.value);
});

derace

Ensures that only the most up to date call to a resolved promise is used.

Usage

In the example below, we call a server to get the current time to display. If we did not use derace, an earlier call to getTime could resolve after the last, and show an outdated result. If an outdated promise resolves, the sync flag will be false, and the result can be ignored and the desync logged.

import { derace } from "@pomle/throb";

function getTime() {
  return fetch("http://time/now");
}

const updateTime = derace(getTime);

setInterval(() => {
  updateTime()
    .then(([time, sync]) => {
      if (sync) {
        window.title = time;
      } else {
        // Unsynced resolves may also be ignored or logged.
        throw new Error("Out of sync");
      }
    })
    .catch((error) => {
      console.info(error);
    });
}, 1000);

knock

Prevents calling function until it has been attempted to be called a number of times.

Usage

Example below shows an overlay when the user moves pointer over screen. In order to avoid being overly sensitive to movement, we ignore the 5 first events. We then forget that movement after 2000 ms of inactivity.

import { knock } from "@pomle/throb";

function handlePointer() {
  document.body.classList.add("overlay");
}

const threshold = 5;
const remember = 2000;

const onPointerMove = knock(handlePointer, threshold, remember);

window.addEventListener("pointermove", onPointerMove);

throttle

Ensures a function is executed exactly once per interval if called.

Usage

In the example below, we log the pointer position every time it is moved, and send to a server every half second at most.

import { throttle } from "@pomle/throb";

const buffer: number[] = [];

function flush() {
  const data = [...buffer];
  buffer.length = 0;

  return fetch("http://backend.com/data", {
    method: "POST",
    body: JSON.stringify(data),
  });
}

const readyToSend = throttle(flush, 500);

document.addEventListener("pointermove", (event) => {
  buffer.push(event.clientX);
  readyToSend();
});

turnstyle

Synchronously ensure only a single asynchronous call is in flight at any given time. Returns Promise if the call was allowed, otherwise undefined.

Usage

When saving data to a backend, you want to avoid multiple requests originating from user error. Accidentally clicking twice on a button could produce an error. Use turnstyle wrapper to ignore calls to a function while a Promise is in flight.

import { turnstyle } from "@pomle/throb";

function saveData(data: any) {
  return fetch("http://backend.com/data", {
    method: "POST",
    body: JSON.stringify(data),
  });
}

const saveSafe = turnstyle(saveData);

document.querySelector("button").addEventListener("click", (event) => {
  const promise = saveSafe({ name: "Turtles" });
  if (!promise) {
    alert("Take it easy");
    return;
  }

  promise
    .then(() => {
      alert("Data was saved");
    })
    .catch(() => {
      alert("Data saved failed");
    });
});