From 8c5252986e547a43937c1cbc5b8ef9cfa615811e Mon Sep 17 00:00:00 2001 From: robonen Date: Sun, 23 Feb 2025 00:44:14 +0700 Subject: [PATCH] feat(packages/stdlib): add SyncMutex --- packages/stdlib/src/index.ts | 1 + packages/stdlib/src/sync/index.ts | 1 + packages/stdlib/src/sync/mutex/index.test.ts | 45 ++++++++++++++++++++ packages/stdlib/src/sync/mutex/index.ts | 29 +++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 packages/stdlib/src/sync/index.ts create mode 100644 packages/stdlib/src/sync/mutex/index.test.ts create mode 100644 packages/stdlib/src/sync/mutex/index.ts diff --git a/packages/stdlib/src/index.ts b/packages/stdlib/src/index.ts index 9e936f1..62ebea0 100644 --- a/packages/stdlib/src/index.ts +++ b/packages/stdlib/src/index.ts @@ -5,6 +5,7 @@ export * from './math'; export * from './objects'; export * from './patterns'; export * from './structs'; +export * from './sync'; export * from './text'; export * from './types'; export * from './utils' diff --git a/packages/stdlib/src/sync/index.ts b/packages/stdlib/src/sync/index.ts new file mode 100644 index 0000000..b1ad937 --- /dev/null +++ b/packages/stdlib/src/sync/index.ts @@ -0,0 +1 @@ +export * from './mutex'; diff --git a/packages/stdlib/src/sync/mutex/index.test.ts b/packages/stdlib/src/sync/mutex/index.test.ts new file mode 100644 index 0000000..c50ae0b --- /dev/null +++ b/packages/stdlib/src/sync/mutex/index.test.ts @@ -0,0 +1,45 @@ +import { describe, it, expect, beforeEach } from 'vitest'; +import { SyncMutex } from '.'; + +describe('SyncMutex', () => { + let mutex: SyncMutex; + + beforeEach(() => { + mutex = new SyncMutex(); + }); + + it('unlocked by default', () => { + expect(mutex.isLocked).toBe(false); + }); + + it('lock the mutex', () => { + mutex.lock(); + expect(mutex.isLocked).toBe(true); + }); + + it('remain locked when locked multiple times', () => { + mutex.lock(); + mutex.lock(); + expect(mutex.isLocked).toBe(true); + }); + + it('unlock a locked mutex', () => { + mutex.lock(); + mutex.unlock(); + expect(mutex.isLocked).toBe(false); + }); + + it('remain unlocked when unlocked multiple times', () => { + mutex.unlock(); + mutex.unlock(); + expect(mutex.isLocked).toBe(false); + }); + + it('reflect the current lock state', () => { + expect(mutex.isLocked).toBe(false); + mutex.lock(); + expect(mutex.isLocked).toBe(true); + mutex.unlock(); + expect(mutex.isLocked).toBe(false); + }); +}); diff --git a/packages/stdlib/src/sync/mutex/index.ts b/packages/stdlib/src/sync/mutex/index.ts new file mode 100644 index 0000000..acb1610 --- /dev/null +++ b/packages/stdlib/src/sync/mutex/index.ts @@ -0,0 +1,29 @@ +/** + * @name SyncMutex + * @category Utils + * @description A simple synchronous mutex to provide more readable locking and unlocking of code blocks + * + * @example + * const mutex = new SyncMutex(); + * + * mutex.lock(); + * + * mutex.unlock(); + * + * @since 0.0.5 + */ +export class SyncMutex { + private state: boolean = false; + + public get isLocked() { + return this.state; + } + + public lock() { + this.state = true; + } + + public unlock() { + this.state = false; + } +}