UNPKG

@imqueue/rpc

Version:

RPC-like client-service implementation over messaging queue

93 lines 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lock = lock; /*! * IMQ-RPC Decorators: lock * * I'm Queue Software Project * Copyright (C) 2025 imqueue.com <support@imqueue.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * * If you want to use this code in a closed source (commercial) project, you can * purchase a proprietary commercial license. Please contact us at * <support@imqueue.com> to get commercial licensing options. */ const __1 = require(".."); /** * \@lock() decorator implementation * Will make all simultaneous similar method calls locked to be resolved with * the first obtained values. Similarity is identified by a bypassed method * argument values. * * @param {boolean|LockOptions} enabledOrOptions - whether to enable locks or not * @return {( * target: any, * methodName: (string), * descriptor: TypedPropertyDescriptor<(...args: any[]) => any> * ) => void} */ function lock(enabledOrOptions = true) { return function (target, methodName, descriptor) { const enabled = typeof enabledOrOptions === 'boolean' ? enabledOrOptions : !enabledOrOptions.disabled; const skipArgs = typeof enabledOrOptions === 'boolean' ? undefined : enabledOrOptions.skipArgs; const original = descriptor.value; const className = typeof target === 'function' ? target.name // static : target.constructor.name; // dynamic descriptor.value = async function (...args) { const withLocks = !parseInt(process.env['DISABLE_LOCKS'] + '') && enabled; let lock; let sig = ''; if (withLocks) { sig = (0, __1.signature)(className, methodName, skipArgs ? args.filter((_, index) => !~skipArgs.indexOf(index)) : args); lock = await __1.IMQLock.acquire(sig, undefined, { className, methodName, args, }); if (!__1.IMQLock.locked(sig)) { return lock; } } try { // istanbul ignore next let result = original ? original.apply(this, args) : undefined; if (result && result.then && typeof result.then === 'function') { result = await result; } if (withLocks) { __1.IMQLock.release(sig, result); } return result; } catch (err) { // istanbul ignore next if (withLocks) { __1.IMQLock.release(sig, null, err); } // istanbul ignore next throw err; } }; }; } //# sourceMappingURL=lock.js.map