From df13e624b776421ffb51b430a65ae414de591f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramirez=20Vargas=2C=20Jos=C3=A9=20Pablo?= Date: Sun, 6 Sep 2026 19:45:48 -0600 Subject: [PATCH] feat!: Rename wait -> waitSync and waitAsync -> wait --- pages/src/lib/Instructions.svelte | 2 +- pages/src/workers/exampleWorker.ts | 2 +- src/sync/AutoResetEvent.ts | 4 ++-- src/sync/ManualResetEvent.ts | 12 ++++++------ tests/ut/helpers/test-wait-worker.ts | 4 ++-- tests/ut/sync/AutoResetEvent.test.ts | 20 ++++++++++---------- tests/ut/sync/ManualResetEvent.test.ts | 18 +++++++++--------- 7 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pages/src/lib/Instructions.svelte b/pages/src/lib/Instructions.svelte index 520ae45..bb58fc8 100644 --- a/pages/src/lib/Instructions.svelte +++ b/pages/src/lib/Instructions.svelte @@ -18,7 +18,7 @@ in this demo page does polling to condition the wait as opposed to blindly waiting:

if (!ManualResetEvent.isSignaled(pause)) {
-    ManualResetEvent.wait(pause);
+    ManualResetEvent.waitSync(pause);
 }

Still, even with this modification, you'll see a big difference in times between a pausable and a diff --git a/pages/src/workers/exampleWorker.ts b/pages/src/workers/exampleWorker.ts index 11e6f54..d6fc23d 100644 --- a/pages/src/workers/exampleWorker.ts +++ b/pages/src/workers/exampleWorker.ts @@ -16,7 +16,7 @@ function isPrimePausable(n: number, pause: Token, cancelToken?: Token) { for (let i = 2; i <= n / 2; ++i) { CancellationSource.throwIfSignaled(cancelToken); if (!ManualResetEvent.isSignaled(pause)) { - ManualResetEvent.wait(pause); + ManualResetEvent.waitSync(pause); } if (n % i === 0) { return false; diff --git a/src/sync/AutoResetEvent.ts b/src/sync/AutoResetEvent.ts index f1fa9f5..11aefdf 100644 --- a/src/sync/AutoResetEvent.ts +++ b/src/sync/AutoResetEvent.ts @@ -32,7 +32,7 @@ export class AutoResetEvent extends Event { * @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the * specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place. */ - static wait(token: Token, timeout?: number) { + static waitSync(token: Token, timeout?: number) { checkToken(token, ...autoResetEventIdentityData); // Performance optimization: Blind attempt. if (Atomics.compareExchange(token, 0, 1, 0) === 1) { @@ -57,7 +57,7 @@ export class AutoResetEvent extends Event { * @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the * specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place. */ - static async waitAsync(token: Token, timeout?: number) { + static async wait(token: Token, timeout?: number) { checkToken(token, ...autoResetEventIdentityData); // Performance optimization: Blind attempt. if (Atomics.compareExchange(token, 0, 1, 0) === 1) { diff --git a/src/sync/ManualResetEvent.ts b/src/sync/ManualResetEvent.ts index 7d2bac4..2036f7b 100644 --- a/src/sync/ManualResetEvent.ts +++ b/src/sync/ManualResetEvent.ts @@ -7,12 +7,12 @@ export function isSignaled(identifierData: IdentifierData, token: Token) { return Atomics.load(token, 0) === 1; } -export function wait(identifierData: IdentifierData, token: Token, timeout?: number) { +export function waitSync(identifierData: IdentifierData, token: Token, timeout?: number) { checkToken(token, ...identifierData); return Atomics.wait(token, 0, 0, timeout); } -export async function waitAsync(identifierData: IdentifierData, token: Token, timeout?: number) { +export async function wait(identifierData: IdentifierData, token: Token, timeout?: number) { checkToken(token, ...identifierData); const result = Atomics.waitAsync(token, 0, 0, timeout); return result.async ? await result.value : result.value; @@ -52,8 +52,8 @@ export class ManualResetEvent extends Event { * @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the * specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place. */ - static wait(token: Token, timeout?: number) { - return wait(manualResetEventIdentityData, token, timeout); + static waitSync(token: Token, timeout?: number) { + return waitSync(manualResetEventIdentityData, token, timeout); } /** * Asynchronously waits on the specified manually-resettable token to be signaled. @@ -64,7 +64,7 @@ export class ManualResetEvent extends Event { * @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the * specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place. */ - static waitAsync(token: Token, timeout?: number) { - return waitAsync(manualResetEventIdentityData, token, timeout); + static wait(token: Token, timeout?: number) { + return wait(manualResetEventIdentityData, token, timeout); } }; diff --git a/tests/ut/helpers/test-wait-worker.ts b/tests/ut/helpers/test-wait-worker.ts index b6031f8..51a2dd1 100644 --- a/tests/ut/helpers/test-wait-worker.ts +++ b/tests/ut/helpers/test-wait-worker.ts @@ -7,9 +7,9 @@ self.onmessage = function(event) { let result; self.postMessage('running'); if (waitFunction === 'ManualResetEvent.wait') { - result = ManualResetEvent.wait(sharedBuffer, timeout); + result = ManualResetEvent.waitSync(sharedBuffer, timeout); } else if (waitFunction === 'AutoResetEvent.wait') { - result = AutoResetEvent.wait(sharedBuffer, timeout); + result = AutoResetEvent.waitSync(sharedBuffer, timeout); } else { throw new Error('Unknown wait function: ' + waitFunction); } diff --git a/tests/ut/sync/AutoResetEvent.test.ts b/tests/ut/sync/AutoResetEvent.test.ts index bdca6d5..7d218c7 100644 --- a/tests/ut/sync/AutoResetEvent.test.ts +++ b/tests/ut/sync/AutoResetEvent.test.ts @@ -60,22 +60,22 @@ describe('AutoResetEvent', () => { }); }); - describe('static wait', () => { + describe('static waitSync', () => { it('Should throw when the given token is not the token of an AutoResetEvent object.', () => { const foreignEvent = new ManualResetEvent(); - expect(() => AutoResetEvent.wait(foreignEvent.token)).toThrow(); + expect(() => AutoResetEvent.waitSync(foreignEvent.token)).toThrow(); }); it('Should handle timeout.', () => { - const result = AutoResetEvent.wait(eventObj.token, 10); + const result = AutoResetEvent.waitSync(eventObj.token, 10); expect(result).toBe('timed-out'); }); it('Should handle immediate success when already signaled.', () => { eventObj.signal(); - const result = AutoResetEvent.wait(eventObj.token); + const result = AutoResetEvent.waitSync(eventObj.token); expect(result).toBe('not-equal'); // Verify the event auto-reset (signal was consumed) @@ -102,11 +102,11 @@ describe('AutoResetEvent', () => { }); }); - describe('static waitAsync', () => { - it('Should throw when the given token is not the token of an AutoResetEvent object.', () => { + describe('static wait', () => { + it('Should throw when the given token is not the token of an AutoResetEvent object.', async () => { const foreignEvent = new ManualResetEvent(); - expect(() => AutoResetEvent.wait(foreignEvent.token)).toThrow(); + await expect(AutoResetEvent.wait(foreignEvent.token)).rejects.toThrow(); }); it('Should handle async wait result.', async () => { // Signal the event after a short delay to test async wait @@ -114,7 +114,7 @@ describe('AutoResetEvent', () => { eventObj.signal(); }, 0); - const result = await AutoResetEvent.waitAsync(eventObj.token, 1000); + const result = await AutoResetEvent.wait(eventObj.token, 1000); expect(result).toBe('ok'); expect(Atomics.load(eventObj.token, 0)).toBe(0); @@ -124,7 +124,7 @@ describe('AutoResetEvent', () => { // Pre-signal the event so waitAsync returns immediately eventObj.signal(); - const result = await AutoResetEvent.waitAsync(eventObj.token); + const result = await AutoResetEvent.wait(eventObj.token); expect(result).toBe('not-equal'); // Verify the event auto-reset (signal was consumed) @@ -132,7 +132,7 @@ describe('AutoResetEvent', () => { }); it('Should handle timeout in async wait.', async () => { - const result = await AutoResetEvent.waitAsync(eventObj.token, 10); + const result = await AutoResetEvent.wait(eventObj.token, 10); expect(result).toBe('timed-out'); }); diff --git a/tests/ut/sync/ManualResetEvent.test.ts b/tests/ut/sync/ManualResetEvent.test.ts index 0aea2ab..ff51a9d 100644 --- a/tests/ut/sync/ManualResetEvent.test.ts +++ b/tests/ut/sync/ManualResetEvent.test.ts @@ -60,18 +60,18 @@ describe('ManualResetEvent', () => { tokenTypeTest(AutoResetEvent, ManualResetEvent.isSignaled, ManualResetEvent); }); - describe('static wait', () => { - tokenTypeTest(AutoResetEvent, ManualResetEvent.wait, ManualResetEvent); + describe('static waitSync', () => { + tokenTypeTest(AutoResetEvent, ManualResetEvent.waitSync, ManualResetEvent); it('Should handle timeout.', () => { - const result = ManualResetEvent.wait(eventObj.token, 10); + const result = ManualResetEvent.waitSync(eventObj.token, 10); expect(result).toBe('timed-out'); }); it('Should handle immediate success when already signaled.', () => { eventObj.signal(); - const result = ManualResetEvent.wait(eventObj.token); + const result = ManualResetEvent.waitSync(eventObj.token); expect(result).toBe('not-equal'); }); @@ -94,15 +94,15 @@ describe('ManualResetEvent', () => { }); }); - describe('static waitAsync', () => { - tokenTypeTest(AutoResetEvent, ManualResetEvent.waitAsync, ManualResetEvent); + describe('static wait', () => { + tokenTypeTest(AutoResetEvent, ManualResetEvent.wait, ManualResetEvent); it('Should handle async wait result.', async () => { // Signal the event after a short delay to test async wait setTimeout(() => { eventObj.signal(); }, 0); - const result = await ManualResetEvent.waitAsync(eventObj.token, 10); + const result = await ManualResetEvent.wait(eventObj.token, 10); expect(result).toBe('ok'); }); @@ -111,13 +111,13 @@ describe('ManualResetEvent', () => { // Pre-signal the event so waitAsync returns immediately eventObj.signal(); - const result = await ManualResetEvent.waitAsync(eventObj.token); + const result = await ManualResetEvent.wait(eventObj.token); expect(result).toBe('not-equal'); }); it('Should handle timeout in async wait.', async () => { - const result = await ManualResetEvent.waitAsync(eventObj.token, 10); + const result = await ManualResetEvent.wait(eventObj.token, 10); expect(result).toBe('timed-out'); });