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

jispatcher

v0.9.5

Published

A modern Asynchronous JavaScript Dispatcher that protect your transactions.

Downloads

2

Readme

⭐️ Jispatcher

A modern Asynchronous JavaScript task dispatcher that enables tasks to be atomic operation so that protect your data and code logic.

npm install --save jispatcher
# or
yarn add jispatcher
# or
pnpm add jispatcher

Getting Started

import { Disptacher } from "jispatcher";

const dispatcher = new Dispatcher();
function handler(): Promise<number> {
  // entire function that passed
  // to the invoke() will be executed
  // one by one. Only one task is
  // totally executed, the next
  // will be picked.
  return dispatcher.invoke(() => {
    // A series of async transactions¬
  });
}

🧐 Wait a moment, why we need this library?

Yes, I do of course know that JavaScript is a single thread language that there is no traditional thread safe problem. But does that really mean complete security?

Considering following situation: There is an api that used for registering a new user. After ensured that user name is not duplicated, this api will firstly do something and then create a new user at last.

The pseudo code are as followings:

/**
 * Register a new user.
 * @return Newly registered user's id
 */
async function register(userName: string, password: string): Promise<number> {
  if (await isUserNameOccupied()) {
    throw new Error("User name has been used, please choose another.");
  }
  const user = new User();
  user.name = userName;
  user.password = password;
  const insertedUser = await insertToDB(user);
  return insertedUser.id;
}

Nonetheless, above codes is bugged:

If there is more than one register request was received at almost same time, and both of them carrying same user name.

Assume that there is one task from all got the promise of isUserNameOccupied resolved, then it will create user entity and started to await for insertToDB.

When the network operation is processing by external native code. The event loop will goto take next task executed.

Bug is here: The next task that carrying same user name may not detecting the user name was occupied, then it will send another insert sql to DB.

At last, we got two user with same user name.

Even JavaScript is a thread-safe language, there still maybe some errors.

So we need a dispatcher to make the function register as atomic operation. Even one million request received, all of them will be handled one by one, and keep its async features.

😎 How to use this?

Make above example safe:

// Got a dispatcher for register transaction.
const dispatcher = new Dispatcher();
/**
 * Register a new user.
 * @return Newly registered user's id
 */
async function register(userName: string, password: string): Promise<number> {
  return dispatcher.invoke(async () => {
    if (await isUserNameOccupied()) {
      throw new Error("User name has been used, please choose another.");
    }

    const user = new User();
    user.name = userName;
    user.password = password;
    const insertedUser = await insertToDB(user);

    return insertedUser.id;
  });
}

😻 Contribute

All PRs are always welcomed.

git clone [email protected]:zsh2401/jispatcher.git
pnpm install