mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 02:44:45 +00:00
feat(packages/stdlib): add execute method for SyncMutex
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest';
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||||
import { SyncMutex } from '.';
|
import { SyncMutex } from '.';
|
||||||
|
|
||||||
describe('SyncMutex', () => {
|
describe('SyncMutex', () => {
|
||||||
@@ -14,24 +14,28 @@ describe('SyncMutex', () => {
|
|||||||
|
|
||||||
it('lock the mutex', () => {
|
it('lock the mutex', () => {
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
|
|
||||||
expect(mutex.isLocked).toBe(true);
|
expect(mutex.isLocked).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('remain locked when locked multiple times', () => {
|
it('remain locked when locked multiple times', () => {
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
|
|
||||||
expect(mutex.isLocked).toBe(true);
|
expect(mutex.isLocked).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('unlock a locked mutex', () => {
|
it('unlock a locked mutex', () => {
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
|
||||||
expect(mutex.isLocked).toBe(false);
|
expect(mutex.isLocked).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('remain unlocked when unlocked multiple times', () => {
|
it('remain unlocked when unlocked multiple times', () => {
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
|
||||||
expect(mutex.isLocked).toBe(false);
|
expect(mutex.isLocked).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -42,4 +46,49 @@ describe('SyncMutex', () => {
|
|||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
expect(mutex.isLocked).toBe(false);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type { MaybePromise } from "../../types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name SyncMutex
|
* @name SyncMutex
|
||||||
* @category Utils
|
* @category Utils
|
||||||
@@ -10,10 +12,15 @@
|
|||||||
*
|
*
|
||||||
* mutex.unlock();
|
* mutex.unlock();
|
||||||
*
|
*
|
||||||
|
* const result = await mutex.execute(() => {
|
||||||
|
* // do something
|
||||||
|
* return Promise.resolve('done');
|
||||||
|
* });
|
||||||
|
*
|
||||||
* @since 0.0.5
|
* @since 0.0.5
|
||||||
*/
|
*/
|
||||||
export class SyncMutex {
|
export class SyncMutex {
|
||||||
private state: boolean = false;
|
private state = false;
|
||||||
|
|
||||||
public get isLocked() {
|
public get isLocked() {
|
||||||
return this.state;
|
return this.state;
|
||||||
@@ -26,4 +33,15 @@ export class SyncMutex {
|
|||||||
public unlock() {
|
public unlock() {
|
||||||
this.state = false;
|
this.state = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async execute<T>(callback: () => T) {
|
||||||
|
if (this.isLocked)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.lock();
|
||||||
|
const result = await callback();
|
||||||
|
this.unlock();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user