Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Rust-like Mutex and Semaphore for TypeScript

NotificationsYou must be signed in to change notification settings

hazae41/mutex

Repository files navigation

Rust-like Mutex and Semaphore for TypeScript

npm i @hazae41/mutex

Node Package 📦

Features

Current features

  • 100% TypeScript and ESM
  • No external dependencies
  • Similar to Rust
  • Can hold data
  • Unit-tested
  • Memory-safe

Usage

Mutex

import{Mutex}from"@hazae41/mutex"constmutex=newMutex(123)/** * You can queue a callback */asyncfunctionrunOrWait(){awaitmutex.runOrWait((x)=>/* do (async) stuff with x */)}/** * You can throw if the mutex is already locked */asyncfunctionrunOrThrow(){awaitmutex.runOrThrow((x)=>/* do stuff with x */)}/** * You can return something from the callback */asyncfunctionrunOrWait2(){consty=awaitmutex.runOrWait((x)=>x*2)}/** * You can use async code */asyncfunctionrunOrWait3(){consty=awaitmutex.runOrWait(async(x)=>awaitf(x))}/** * You can acquire and release when you want */asyncfunctiongetOrWait(){constx=awaitmutex.getOrWait()consty=x.get()*2x.release()}/** * You can acquire and release with `using` */asyncfunctiongetOrWait2(){  usingx=awaitmutex.getOrWait()consty=x.get()*2}

Semaphore

Same functions as Mutex but you can specify a capacity

constsemaphore=newSemaphore(123,3)consta=awaitsemaphore.getOrThrow()constb=awaitsemaphore.getOrThrow()constc=awaitsemaphore.getOrThrow()constd=awaitsemaphore.getOrThrow()// will throw

You can see it likeMutex<T> = Semaphore<T, 1>

About

Rust-like Mutex and Semaphore for TypeScript

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp