modem-locks.js 379 Bytes
const locks = require('locks');

const mutexes = {};

exports.lock = (name) => new Promise((resolve) => {
    if (!mutexes[name]) {
        mutexes[name] = locks.createMutex();
    }

    mutexes[name].lock(() => {
        resolve(true);
    });
});

exports.unlock = (name) => {
    try {
        if (mutexes[name]) mutexes[name].unlock();
    } catch (e) {
        //
    }
};