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

@hazae41/mutex

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hazae41/mutex - npm Package Compare versions

Comparing version 1.2.12 to 2.0.0

2

dist/types/index.d.ts

@@ -1,1 +0,1 @@

export { Lock, Mutex, MutexError, MutexLockError } from './mods/mutex/mutex.js';
export { Lock, LockedError, Mutex, Semaphore } from './mods/mutex/mutex.js';

@@ -1,3 +0,3 @@

type Promiseable<T> = T | Promise<T>;
type Awaitable<T> = T | Promise<T>;
export type { Promiseable };
export type { Awaitable };
import { Result } from '@hazae41/result';
import { Promiseable } from '../../libs/promises/promises.js';
import { Awaitable } from '../../libs/promises/promises.js';
type MutexError = MutexLockError;
declare class MutexLockError extends Error {
declare class LockedError extends Error {
#private;

@@ -10,31 +9,66 @@ readonly name: string;

}
declare class Mutex<T> {
/**
* A releasable object
*/
declare class Lock<T> {
readonly inner: T;
promise?: Promise<void>;
readonly release: () => void;
constructor(inner: T, release: () => void);
[Symbol.dispose](): void;
get(): T;
}
/**
* A semaphore with some reference and some capacity
*/
declare class Semaphore<T, N extends number = number> {
#private;
readonly inner: T;
readonly capacity: N;
constructor(inner: T, capacity: N);
static void<N extends number>(capacity: N): Semaphore<void, N>;
get locked(): boolean;
get count(): number;
/**
* Just a mutex
* Lock or throw an error
* @param callback
*/
constructor(inner: T);
get locked(): boolean;
acquire(): Promiseable<Lock<T>>;
lockOrThrow<R>(callback: (inner: T) => Awaitable<R>): Promise<R>;
/**
* Lock this mutex
* Lock or return an error
* @param callback
* @returns
*/
lock<R>(callback: (inner: T) => Promise<R>): Promise<R>;
tryLock<R>(callback: (inner: T) => Awaitable<R>): Result<Promise<R>, LockedError>;
/**
* Try to lock this mutex
* Lock or wait
* @param callback
*/
lock<R>(callback: (inner: T) => Awaitable<R>): Promise<R>;
/**
* Just wait
* @returns
*/
tryLock<R>(callback: (inner: T) => Promise<R>): Result<Promise<R>, MutexLockError>;
wait(): Promise<void>;
/**
* Lock and return a disposable object
* @returns
*/
acquire(): Promise<Lock<T>>;
}
declare class Lock<T> {
/**
* A semaphore but with a capacity of 1
*/
declare class Mutex<T> {
#private;
readonly inner: T;
readonly release: () => void;
constructor(inner: T, release: () => void);
[Symbol.dispose](): void;
constructor(inner: T);
static void(): Mutex<void>;
get locked(): boolean;
lockOrThrow<R>(callback: (inner: T) => Awaitable<R>): Promise<R>;
tryLock<R>(callback: (inner: T) => Awaitable<R>): Result<Promise<R>, LockedError>;
lock<R>(callback: (inner: T) => Awaitable<R>): Promise<R>;
wait(): Promise<void>;
acquire(): Promise<Lock<T>>;
}
export { Lock, Mutex, type MutexError, MutexLockError };
export { Lock, LockedError, Mutex, Semaphore };
{
"type": "module",
"name": "@hazae41/mutex",
"version": "1.2.12",
"version": "2.0.0",
"description": "Rust-like Mutex for TypeScript",

@@ -6,0 +6,0 @@ "homepage": "https://github.com/hazae41/mutex",

# Mutex
Rust-like Mutex for TypeScript
Rust-like Mutex and Semaphore for TypeScript

@@ -19,2 +19,3 @@ ```bash

- Unit-tested
- Memory-safe
- Uses Result from `@hazae41/result`

@@ -24,2 +25,4 @@

### Mutex
```typescript

@@ -30,23 +33,64 @@ import { Mutex } from "@hazae41/mutex"

async function first() {
await mutex.lock(async (x) => {
// do stuff with x
})
/**
* You can queue a callback
*/
async function lockOrWait() {
await mutex.lock(async (x) => /* do stuff with x */)
}
async function second() {
await mutex.lock(async (x) => {
// do stuff with x
})
/**
* You can return something from the callback
*/
async function lockOrWaitAndLog() {
const x = await mutex.lock(async (x) => x)
console.log(x)
}
async function third() {
await mutex.tryLock(async (x) => {
// do stuff with x
}).unwrap()
/**
* You can throw if the mutex is already locked
*/
async function lockOrThrow() {
await mutex.lockOrThrow(async (x) => /* do stuff with x */)
}
first()
second()
third()
/**
* You can return a result if the mutex is already locked
*/
async function tryLock() {
const result = mutex.tryLock(async (x) => /* do stuff with x */)
if (result.isErr()) // if couldn't lock
throw result.getErr()
await result.get() // wait task
}
/**
* You can just wait unlock without locking
*/
async function wait() {
await mutex.wait()
}
/**
* You can acquire and release when you want
*/
async function acquire() {
const lock = await mutex.acquire()
console.log(lock.get())
lock.release()
}
/**
* You can acquire and release with `using`
*/
async function acquireAndUse() {
using lock = await mutex.acquire()
console.log(lock.get())
}
```
### Semaphore
Same functions as Mutex but you can specify a capacity

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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