mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
refactor: change separate tools by category
This commit is contained in:
1
core/stdlib/src/sync/index.ts
Normal file
1
core/stdlib/src/sync/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './mutex';
|
||||
94
core/stdlib/src/sync/mutex/index.test.ts
Normal file
94
core/stdlib/src/sync/mutex/index.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { describe, it, expect, beforeEach, vi } 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);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
47
core/stdlib/src/sync/mutex/index.ts
Normal file
47
core/stdlib/src/sync/mutex/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { MaybePromise } from "../../types";
|
||||
|
||||
/**
|
||||
* @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();
|
||||
*
|
||||
* const result = await mutex.execute(() => {
|
||||
* // do something
|
||||
* return Promise.resolve('done');
|
||||
* });
|
||||
*
|
||||
* @since 0.0.5
|
||||
*/
|
||||
export class SyncMutex {
|
||||
private state = false;
|
||||
|
||||
public get isLocked() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public lock() {
|
||||
this.state = true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user