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

plastiq-throttle

v1.4.0

Published

Throttle calls to a function, by arguments, by time, and by promise completion.

Downloads

9

Readme

plastiq-throttle

Throttle calls to a function, by arguments, by time, and by promise completion.

example

var throttle = require('plastiq-throttle');

var searchForUsers = throttle(function (query) {
  return http.get('/search?q' + encodeURIComponent(query)).then(function (users) {
    model.users = users;
  });
});

searchForUsers('a');     // GET /search?q=a
searchForUsers('ad');    // -- skip --
searchForUsers('ada');   // -- skip --
searchForUsers('adam');  // GET /search?q=adam

It only calls the function if:

  • the arguments are different to the last completed call (compared using ===)
  • the promise returned from the last call has completed
  • last call was made more than options.throttle milliseconds ago

It will always eventually call the function with the last given arguments.

It is especially useful when synchronising client-side state with server-side resources or CPU intensive operations, some examples:

  • fetching search results for a given search string, but only if the search string changes, and not too often.
  • fetching an object from a server-side API by ID, but only if the ID changes, and not too often.
  • performing a heavy search across client-side data, but only if the criteria changes, and not too often.

Also, because it's intended to be used in plastiq apps, your page will refresh after the function completes (if it's throttled by time, or returns a promise).

api

var throttledFn = throttle([options], fn);

function render() {
  throttledFn(arg1, arg2);

  return vdom;
}
  • options.throttle - the throttle duration in milliseconds, default 140. if 0 then there is no time-based throttling (only based on arguments and promise completion)

  • fn - a function to be throttled, if it returns a promise, then the next call will be delayed until this promise completes.

    Why wait for promises to complete? To eliminate the risk of race-conditions. Imagine you search for users matching a first, then adam. If we do them concurrently, then the results for adam could come back before the results of a. model.users will be set to the results of adam first and then finally to the results of a, quite unexpected.

  • arg1, arg2, ... - arguments to be passed to fn. If no arguments are passed, then they are considered to be different, that is, the function is potentially executed (considering throttle-time has passed and the previous promise have completed.)

Note that in order to have the page refreshed when a promise completes, or a throttled call is executed, you must call throttledFn inside the render loop, i.e. have plastiq.html.refresh.

reset

throttledFn.reset();

Ensures that the next call to the function will execute, pending throttling and promise returns. Useful if you know that an underlying datasource has changed.