Skip to content

🐱 async/await for optimistic concurrency control. (Last in wins.)

License

Notifications You must be signed in to change notification settings

hikouki/jackall

Repository files navigation

Jackall

CI

async/await for optimistic concurrency control. (Last in wins.)

Install

npm install jackall

Usage

Please refer to tests.

import { jackall } from "jackall";

// async tasks.
const task1 = async () => new Promise((ok) => setTimeout(ok, 200, "ping1"));
const task2 = async () => new Promise((ok) => setTimeout(ok, 100, "ping2"));

// jackall.acquire. (resource name is `tiger`)
const [act1, act2] = await Promise.all([
  jackall.acquire("tiger", task1()),
  jackall.acquire("tiger", task2()),
]);

// last in wins.
// act1 => { ok: false, code: JACKALL_ERROR_CODE.NOMATCH_VERSION, value: new Error(`Jackall Error: ${JACKALL_ERROR_CODE.NOMATCH_VERSION}`) }
// act2 => { ok: true, value: "ping2" }

If you want to specify a resource name.

import { makeJackall } from "jackall";

const jackall = makeJackall<"tiger">();

// jackall.acquire("tiger", task1()) => OK
// jackall.acquire("bear", task1()) => NG

Error Codes

import { JACKALL_ERROR_CODE } from "jackall";
option description
FAILED_ASYNCTASK Failed to Async task.
NOMATCH_VERSION Lost in the race conditions.(Last in Wins.)

License

Jackall is MIT licensed.