0.0.3 • Published 7 months ago

param-mutex v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

ParamMutex

GitHub

Mutex Library is a JavaScript library for managing mutex operations that are parameterized. This library is designed to address concurrency issues and prevent race conditions when accessing specific resources. Mutex is particularly useful for organizing and synchronizing asynchronous operations.

Table of Contents

Demo

Mutex Library Demo

Installation

npm install param-mutex

Usage

An example scenario of using the Mutex library:

const {Mutex} = require('param-mutex')
const mutex = new Mutex('EXAMPLE_CHANNEL')

const decreaseAmountFromWallet = async (userId, amount) => {
    const { release } = await mutex.lock([userId], ['userId']);

    try {
        const walletTotal = await getWallet(userId);

        if (walletTotal >= amount) {
            await updateWallet(userId, -1 * amount);
        } else {
            throw new Error("Wallet amount is not enough");
        }

        release();

        return 1;
    } catch (e) {
        release();
        return 0;
    }
}

License

This project is licensed under the terms of the MIT License

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago