0.0.2 • Published 3 years ago

@fiahfy/semaphore v0.0.2

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

semaphore

badge

Semaphore implementation in JavaScript.

Installation

npm install @fiahfy/semaphore

Usage

Mutex

import fs from 'fs'
import { semaphore } from '@fiahfy/semaphore'

const s = semaphore()

const heavyFunc = () => {
  return s.acquire().then(() => {
    console.log('heavy process')
    s.release()
    console.log('released')
  })
}

Mutex with callback (auto release)

import fs from 'fs'
import { semaphore } from '@fiahfy/semaphore'

const s = semaphore()

const heavyFunc = () => {
  return s
    .acquire(() => {
      console.log('heavy process')
    })
    .then(() => {
      console.log('released')
    })
}

Zero permits

import fs from 'fs'
import { semaphore } from '@fiahfy/semaphore'

const s = semaphore(0)

console.log('first')

setTimeout(() => {
  console.log('second')
  s.release() // increment permit
}, 100)

s.acquire().then(() => {
  // wait until permit is available
  console.log('third')
})

API

semaphore(permits)

Creates a Semaphore with the given number of permits.

permits

Type: number
Default: 1

The initial number of permits available.
This value may be negative, in which case releases must occur before any acquires will be granted.

semaphore.acquire(callback)

Acquires a permit from this semaphore, blocking until one is available.
If a callback is given, a permit is released automatically after a given callback is finished.

callback

Type: Function
Default: undefined

Holds a permit until a callback is finished.

semaphore.release()

Releases a permit, returning it to the semaphore.

0.0.2

3 years ago

0.0.1

4 years ago