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

xhr-queue

v0.1.0

Published

[![NPM version](https://badge.fury.io/js/xhr-queue.svg)](http://badge.fury.io/js/xhr-queue) [![devDependencies Status](https://david-dm.org/remix/xhr-queue/dev-status.svg)](https://david-dm.org/remix/xhr-queue?type=dev) [![Open Source Love](https://badges

Downloads

7

Readme

xhrQueue

NPM version devDependencies Status Open Source Love Build Status

A queue for HTTP requests, to guarantee a sequential order. This prevents conflicts between requests. It also removes the need for some callbacks in the frontend code. And it removes the need for throttling/debouncing, leading to a snappier user experience. Finally, it allows for retrying failed requests when the connection drops.

Check out this blog post for more details.

Example:

Before:

const saveBusStops = debounce((project_id, stops) => {
  xhr({
    method: 'POST',
    url: `${project_id}/bus_stops`,
    json: {stops},
  }).then(() =>
    xhr({ url: `${project_id}/stats`})
      .then((body) => updateStats(body))
  );
}, 3000);

After:

function saveBusStops(project_id, stops) {
  window.xhrQueue.xhr({
    method: 'POST',
    url: `${project_id}/bus_stops`,
    json: {stops},
    ignorePreviousByUrl: true,
  });
  window.xhrQueue.xhr({
    url: `${project_id}/stats`,
    ignorePreviousByUrl: true,
  }, (error, response, body) => updateStats(body));
}

Usage

At Remix, we use this with the xhr package. We set it up something like this:

window.xhrQueue = require('xhr-queue')({
  xhrFunc: require('xhr'),
  connectionCallback: (type) => {
    if (type === 'connection_lost') {
      // show retry banner
      // when doing a retry, call window.xhrQueue.retry()
    } else {
      // hide retry banner
    }
  },
  enableDebugging: localStorage.xhrQueueEnableDebugging,
});

We then write HTTP requests like this:

window.xhrQueue.xhr({
  url: `${project_id}/stats`,
  ignorePreviousByUrl: true,
}, (error, response, body) => updateStats(body));

The ignorePreviousByUrl will remove any existing requests in the queue that have the same URL.

By default, we will any requests with method other than GET as a "write request", which means they cannot be executed in parallel with other requests:

window.xhrQueue.xhr({
  method: 'POST',
  url: `${project_id}/bus_stops`,
  json: {stops},
  ignorePreviousByUrl: true,
});

Overriding request types

If you need to mark a GET request as a write request, or a POST request as a read request (e.g. when you have to send a bunch of data that can't fit into a GET request), you can override it using overrideQueueItemType:

window.xhrQueue.xhr({
  method: 'POST',
  url: `${project_id}/demographics_stats`,
  json: {stops},
  ignorePreviousByUrl: true,
  overrideQueueItemType: 'read',
});

Other ways to cancel requests

In addition to cancelling requests using ignorePreviousByUrl, you can also use the ignorePreviousBy option to specify custom logic for matching previous requests against the current one:

window.xhrQueue.xhr({
  url: `${project_id}/stats`,
  query: { type: 'skub' },
  ignorePreviousBy(previousRequest, currentRequest) {
    return (
      previousRequest.url === currentRequest.url &&
      previousRequest.query &&
      currentRequest.query &&
      previousRequest.query.type === currentRequest.query.type
    )
  },
}, (error, response, body) => updateStats(body));

You can also manually cancel a request:

const request = window.xhrQueue.xhr({
  url: `${project_id}/stats`,
  ignorePreviousByUrl: true,
});

setTimeout(() => request.cancel(), 1000);

Note that when a write request is cancelled while it is already in flight (by using cancel(), ignorePreviousByUrl, or ignorePreviousBy), the queue will still wait until the request is finished. In case of read requests, we attempt to abort the request, but allow continuing the queue.

When using ignorePreviousByUrl or ignorePreviousBy, the callback from the previous request will be ignored (it will not be called).

Other methods

To see which URLs are currently queued, use window.xhrQueue.getQueuedUrls().

To retry failed requests because of dropped connection (requests with status code 0), use window.xhrQueue.retry(). Also see the setup example above.

To visualise to the console what's happening in the queue, initialise the queue with enableDebugging: true. To distinguish multiple queues, initialise with debugName: 'my queue name'.

Integration tests

In integration tests, it can be more robust to use the xhrQueue to determine when requests have finished, instead of just looking at in-flight requests. For example, at Remix we use transactional-capybara, which we monkey-patch like this:

module TransactionalCapybara
  module AjaxHelpers
    class PageWaiting
      def finished_ajax_requests?
        run_js("window.xhrQueue.getQueuedUrls().length").zero?
      end
    end
  end
end

License

MIT