1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +00:00

feat(packages/stdlib): add execute method for SyncMutex

This commit is contained in:
2025-05-09 13:12:07 +07:00
parent 8d6f08c332
commit 3994f349f4
2 changed files with 69 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { SyncMutex } from '.';
describe('SyncMutex', () => {
@@ -14,24 +14,28 @@ describe('SyncMutex', () => {
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);
});
@@ -42,4 +46,49 @@ describe('SyncMutex', () => {
mutex.unlock();
expect(mutex.isLocked).toBe(false);
});
it('execute a callback when unlocked', async () => {
const callback = vi.fn(() => 'done');
const result = await mutex.execute(callback);
expect(result).toBe('done');
expect(callback).toHaveBeenCalled();
});
it('execute a promise callback when unlocked', async () => {
const callback = vi.fn(() => Promise.resolve('done'));
const result = await mutex.execute(callback);
expect(result).toBe('done');
expect(callback).toHaveBeenCalled();
});
it('execute concurrent callbacks only one at a time', async () => {
const callback = vi.fn(() => Promise.resolve('done'));
const result = await Promise.all([
mutex.execute(callback),
mutex.execute(callback),
mutex.execute(callback),
]);
expect(result).toEqual(['done', undefined, undefined]);
expect(callback).toHaveBeenCalledTimes(1);
});
it('does not execute a callback when locked', async () => {
const callback = vi.fn(() => 'done');
mutex.lock();
const result = await mutex.execute(callback);
expect(result).toBeUndefined();
expect(callback).not.toHaveBeenCalled();
});
it('unlocks after executing a callback', async () => {
const callback = vi.fn(() => 'done');
await mutex.execute(callback);
expect(mutex.isLocked).toBe(false);
});
});

View File

@@ -1,3 +1,5 @@
import type { MaybePromise } from "../../types";
/**
* @name SyncMutex
* @category Utils
@@ -10,10 +12,15 @@
*
* mutex.unlock();
*
* const result = await mutex.execute(() => {
* // do something
* return Promise.resolve('done');
* });
*
* @since 0.0.5
*/
export class SyncMutex {
private state: boolean = false;
private state = false;
public get isLocked() {
return this.state;
@@ -26,4 +33,15 @@ export class SyncMutex {
public unlock() {
this.state = false;
}
public async execute<T>(callback: () => T) {
if (this.isLocked)
return;
this.lock();
const result = await callback();
this.unlock();
return result;
}
}