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

deferer-queue

v1.2.13

Published

A queue mananger for async operating.

Downloads

21

Readme

DEFERER QUEUE

A deferer queue manager.

Install

npm i deferer-queue

Usage

import DefererQueue from 'deferer-queue'
const queue = new DefererQueue(options)
const defer1 = () => new Promise(...)
const defer2 = () => new Promise(...)

const queue = new DefererQueue({ mode: 'parallel' })
queue.push(defer1).then(callback1)
queue.push(defer2).then(callback2)

Notice: a defer is a function. After calling push the queue will run automaticly.

API

constructor(options)

options:

  • mode: how to run defer, default in parallel, optional
    • parallel: run defer immediately after pushing
    • serial: defer will run one by one after each deferer resolved
    • switch: always use the last defer, when push a new defer, the old deferers will be dropped
    • shift: use first defer at the start of queue, then use the latest defer after the previous defer finish
  • autoStart: whether to run queue immediately when invoke push
  • delay: number, default 0, delay to start the queue when push() or start() at the first time
  • debounce: number, default 0, debounce come before delay and throttle
  • throttle: number, default 0, throttle come before delay

It recommands that: debounce works with switch; delay works with parallel, serial and shift; throttle works with serial switch and shift. Notice that, when the queue is running, delay will not work, it only works with a static queue which is going to start.

push(defer, success, fail, cancel)

  • defer: a function which return an instance of Promise
  • success: invoke after deferer resolved
  • fail: invoke after deferer rejected
  • cancel: invoke when a defer is going to be canceled

All of these parameters are functions.

queue.push(defer1).then(callback).catch(fallback)
// is like:
queue.push(defer1, callback, fallback)
// however, success/fail come before callback/fallback in then/catch
// and success/fail are run in sync in process, and then/catch run in async

How to use axios with cancel?

const CancelToken = axios.CancelToken

let cancel = null
let token = new CancelToken(c => { cancel = c })

const defer = () => {
  return axios.get(url, { cancelToken: token })
}

const queue = new DefererQueue()
queue.push(defer, null, null, cancel).then(res => console.log(res))
queue.cancel(defer)

How to use XHR with abort?

const defer = () => {
  let xhr = new XMLHttpRequest()
  let method = "GET"
  let url = "https://developer.mozilla.org/"
  xhr.open(method, url, true)
  xhr.send()

  const deferer = new Promise(...)
  deferer.cancel = () => xhr.abort()
  return deferer
}
queue.push(defer, null, null, deferer => deferer.cancel())

start()

Start the queue when you set autoStart to be false. Or after stopped by stop, use start() to restart the queue.

When the queue is running, start will do nothing.

clear()

Clear the queue. The left defers will not run any more.

cancel(defer)

Cancel a certain defer. Notice, defer is the passed function.

At the same time, cancel which passed by push will be run too.

stop()

Stop the queue. onError callbacks will be invoked. However, defers which in the queue are not dropped, you can restart queue by using queue.start().

end()

Forcely end the queue.

difference between clear, cancel, stop and end

  • clear: just drop the un-run defers, not change the status of queue
  • cancel: just drop one defer
  • end: drop the un-run defers and change the status of queue, onEnd callbacks will be invoked
  • stop: not drop any defer, throw error manually, queue status changes, onError callbacks will be invoked, can be continue by start

onEnd(fn)

Pass a function which will be invoked when the queue finish normanlly. Use offEnd to unbind the callback.

queue.onEnd(fn)

el.addEventListener('click', () => {
  queue.push(defer)
  if (queue.status !== 1) {
    queue.offEnd(fn) // 注意,要保持fn的引用
    queue.start()
  }
})

onError(fn)

Pass a function which will be invoked when error appears. Use offError to unbind.

queue.onError(e => console.log(e))

destroy()

Destory the queue. ️⚠️ Never use it when queue is running.

After destory, the queue is broken, don't use it any more.

Development

npm i
npm test
npm run build