Skip to content

Commit 3c4b007

Browse files
committed
simplify asyncmutex definition -- don't support generic operations
anymore
1 parent 2d0135e commit 3c4b007

File tree

2 files changed

+9
-34
lines changed

2 files changed

+9
-34
lines changed

packages/unraid-shared/src/util/__tests__/processing.test.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,5 @@ describe('AsyncMutex', () => {
291291
expect(result).toBe('outer');
292292
expect(outerOp).toHaveBeenCalledTimes(1);
293293
});
294-
295-
it('should maintain type safety with generic operations', async () => {
296-
const mutex = new AsyncMutex<string>();
297-
298-
const stringOp = vi.fn().mockResolvedValue('string');
299-
const numberOp = vi.fn().mockResolvedValue(42);
300-
const booleanOp = vi.fn().mockResolvedValue(true);
301-
302-
const stringResult: string = await mutex.do(stringOp);
303-
const numberResult: number = await mutex.do(numberOp);
304-
const booleanResult: boolean = await mutex.do(booleanOp);
305-
306-
expect(stringResult).toBe('string');
307-
expect(numberResult).toBe(42);
308-
expect(booleanResult).toBe(true);
309-
});
310294
});
311-
});
295+
});

packages/unraid-shared/src/util/processing.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type AsyncOperation<T> = () => Promise<T>;
6666
* const data2 = await dataLoader.do(); // If first promise is finished, a new fetch is executed
6767
*/
6868
export class AsyncMutex<T = unknown> {
69-
private currentOperation: Promise<any> | null = null;
69+
private currentOperation: Promise<T> | null = null;
7070
private defaultOperation?: AsyncOperation<T>;
7171

7272
/**
@@ -97,12 +97,6 @@ export class AsyncMutex<T = unknown> {
9797
this.defaultOperation = operation;
9898
}
9999

100-
/**
101-
* Executes the default operation if one was provided in the constructor.
102-
* @returns Promise that resolves with the result of the default operation
103-
* @throws Error if no default operation was set in the constructor
104-
*/
105-
do(): Promise<T>;
106100
/**
107101
* Executes the provided operation, ensuring only one runs at a time.
108102
*
@@ -126,32 +120,29 @@ export class AsyncMutex<T = unknown> {
126120
* await promise1;
127121
* const newPromise = mutex.do(() => fetch('/api/new')); // This will execute
128122
*/
129-
do<U>(operation: AsyncOperation<U>): Promise<U>;
130-
do<U = T>(operation?: AsyncOperation<U>): Promise<U | T> {
131-
if (!operation && !this.defaultOperation) {
123+
do(operation?: AsyncOperation<T>): Promise<T> {
124+
if (this.currentOperation) {
125+
return this.currentOperation;
126+
}
127+
const op = operation ?? this.defaultOperation;
128+
if (!op) {
132129
return Promise.reject(
133130
new Error("No operation provided and no default operation set")
134131
);
135132
}
136-
137-
if (this.currentOperation) {
138-
return this.currentOperation;
139-
}
140-
141-
const op = (operation || this.defaultOperation) as AsyncOperation<U | T>;
142133
const safeOp = () => {
143134
try {
144135
return op();
145136
} catch (error) {
146137
return Promise.reject(error);
147138
}
148139
};
140+
149141
const promise = safeOp().finally(() => {
150142
if (this.currentOperation === promise) {
151143
this.currentOperation = null;
152144
}
153145
});
154-
155146
this.currentOperation = promise;
156147
return promise;
157148
}

0 commit comments

Comments
 (0)