epic-locks

1.0.2 • Public • Published

epic-locks

A project meant to contain multiple different type of mutex locks.

Usage

ReadersWriterLock

  1. Multiple readers
  2. Single writer
  3. Prioritises reads
  4. Processes queued jobs at the end of the event queue to let the original callers finish work before the lock processes the next queued item
  5. Queues reads while writing

Initialize

const {ReadersWriterLock} = require("epic-locks")
const lock = new ReadersWriterLock()

Reading

lock.read(() => {
  console.log("Reading!")
})

Writing

lock.write(() => {
  console.log("Writing!")
})

Advanced

const {ReadersWriterLock} = require("epic-locks")
const lock = new ReadersWriterLock()
const promises = []
const result = []

promises.push(
  lock.read(async () => {
    await awaitTimeout(50)
    result.push(3)
  })
)
promises.push(
  lock.write(async () => {
    await awaitTimeout(10)

    lock.read(async () => {
      result.push(5)
    })

    result.push(4)
  })
)
promises.push(
  lock.read(async () => {
    await awaitTimeout(40)
    result.push(2)
  })
)
promises.push(
  lock.write(async () => {
    await awaitTimeout(20)
    result.push(6)
  })
)
promises.push(
  lock.read(async () => {
    await awaitTimeout(30)
    result.push(1)
  })
)

await Promise.all(promises)

expect(result).toEqual([1, 2, 3, 4, 5, 6])

Readme

Keywords

none

Package Sidebar

Install

npm i epic-locks

Weekly Downloads

784

Version

1.0.2

License

ISC

Unpacked Size

6.54 kB

Total Files

9

Last publish

Collaborators

  • kaspernj