epic-locks
v1.0.2
Published
A project meant to contain multiple different type of mutex locks.
Downloads
340
Readme
epic-locks
A project meant to contain multiple different type of mutex locks.
Usage
ReadersWriterLock
- Multiple readers
- Single writer
- Prioritises reads
- 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
- 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])