Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hitorisensei/mutex-decorator

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hitorisensei/mutex-decorator

Mutex and Re-entrant Mutex Decorators for TypeScript

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Mutex Decorators Library

This library provides two TypeScript decorators, Mutex and ReentrantMutex, for locking methods to ensure mutual exclusion in asynchronous contexts.

Under the hood, the library uses composable-locks to provide the Mutex locking mechanism.

In contrast to composable-locks, the ReentrantMutex decorator doesn't depend on domain symbols use and instead uses AsyncLocalStorage to keep the recursive lock state.


NOTE: This library is intended to be used by TypeScript projects.
Projects must have the experimentalDecorators option enabled in the tsconfig.json file.

{
  "compilerOptions": {
    "experimentalDecorators": true,
  }
}

Installation

Install the library using npm:

npm install composable-locks

Usage

Mutex Decorator

The Mutex decorator ensures that the decorated method is locked with a mutex, allowing only one execution at a time. It queues concurrent calls and executes them sequentially.

Example
import { Mutex } from './mutex';

class ExampleClass {
  @Mutex
  async criticalSection() {
    // Your code here
  }
}

const instance = new ExampleClass();
instance.criticalSection();

ReentrantMutex Decorator

The ReentrantMutex decorator allows reentrant locking in the same asynchronous context. It uses AsyncLocalStorage to check if the lock is already acquired in the current async context.

Because the use of AsyncLocalStorage is used, a minimal performance and/or memory overhead is expected. Thus, it is recommended to use ReentrantMutex only when recursive calls are expected.

Example
import { ReentrantMutex } from './reentrantMutex';

class ExampleClass {
  @ReentrantMutex
  async criticalSection() {
    // Your code here
  }
}

const instance = new ExampleClass();
instance.criticalSection();

Testing

The library includes tests for both decorators using Jest. To run the tests, use the following command:

npm test

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 02 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc