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

consumer-x

v1.1.9

Published

A simple consumer built with Rxjs

Downloads

2

Readme

ConsumerX

An asynchronous jobs (Promise | Async Function) consumer which can deal with priority based on RxJS, having error-handling and retry mechanism and can specify callback on success and failure. Can be used in browser and node environment.

FOSSA Status Weekly Downloads

Quick Start

Install

consumer-x

$ yarn add consumer-x

Overview

Introduction

ConsumerX is a simple asynchronous priority jobs consumer with error-handling and retry mechanism built with TypeScript and RxJS, which can run on Node.JS environment and browser.

Basic Usage

import ConsumerX from 'consumer-x';

const consumer = new ConsumerX(2, 100); // Parameters have default value: retryCount = 0, intervalTime = 200

// promise as job
const testPromiseResolve = () => {
  return new Promise(resolve => {
    resolve('resolve from promise');
  })
}

const testPromiseReject = () => {
  console.log('called!');
  return new Promise((_resolve, reject) => {
    reject('reject from promise');
  })
}

// You can also push async function as job
const testAsync = async() => {
  try {
    const res = await testPromiseResolve();
    return res + '?No, is from async';
  } catch(err) {
    throw Error('error');
  }
}

consumer.push({
  task: testPromiseResolve,
  success: (res) => {
    console.log('Success~~ ', res);
  },
  failure: (err) => {
    console.log('Failure ', err);
  }
}, 3) // second argument is priority

consumer.push({
  task: testPromiseReject,
  success: (res) => {
    console.log('Success ', res);
  },
  failure: (err) => {
    console.log('Failure ', err);
  }
}, 2)

consumer.push({
  task: testAsync,
  success: (res) => {
    console.log('Success!! ', res);
  },
  failure: (err) => {
    console.log('Failure ', err);
  }
}) // If you don't specify the priority, the default value will be 5. 

consumer.push({
  task: testAsync,
  success: (res) => {
    console.log('Success$$ ', res);
  },
  failure: (err) => {
    console.log('Failure ', err);
  }
})

/*
The console will log:

called!
called!
called!
Failure  reject from promise
(Because the testPromiseReject's priority is highest, so it run first, and because we set the retryCount to 2 when we created the instance, so it will retry two time (the initial call + 2 retry count, so totally 3 times.) If the last retry also fail, it will call the failure callback.)

Success~~  resolve from promise

Success!!  resolve from promise?No, is from async
(Default priority is 5, which is the lowest in this case.)

Success$$  resolve from promise?No, is from async
(If tasks have same priority, it will work like normal queue (FIFO).)
*/

Running tests

$ yarn test

Parameters

| Param | Type | Description | |--------------|----------|--------------------------------------------------------------------| | retryCount | number | The retry count when job encounter failure. (Default Value = 0) | | intervalTime | number | The interval delay between each two tasks. (Default Value = 200ms) | | | | |

Priority Queue

ConsumerX implement a linear data structure priority queue instead of heap based priority to make sure the order of tasks with same priority.(It's relatively difficult to achieve this in heap-based priority queue.)

| Method | Big O | |----------------|----------| | push | O(1) | | size | O(1) | | isEmpty | O(1) | | pop | O(n) |

Priority

You can specify priority of each task while pushing the task

consumer.push({
  task: testPromise,
  success: (res) => {
    console.log('Success ', res);
  },
  failure: (err) => {
    console.log('Failure ', err);
  }
}, 2);

The lower number has a higher priority than the bigger number, the default priority is 5.

If the tasks have same priorities, it will be implemented with pattern of First In First Out (FIFO).

Defer

Use RxJS defer to achieve lazy execution.

QueueEntity

interface IQueueEntity<Result> {
  task: () => Promise<Result>;
  success?: (res: Result) => void;
  failure?: (err: unknown) => void;
}

currently accept promise and async function as job, will probably support other types in the future.

Roadmap

  • [X] Implement priority queue
  • [X] Setup lint-stage
  • [X] Add Prettier
  • [ ] Increase unit test and maintain high testing coverage
  • [ ] CI/CD pipeline
  • [ ] Take advantage of more RxJS features

License

FOSSA Status