Skip to content

Commit

Permalink
Added a convenient "throwOnError" method
Browse files Browse the repository at this point in the history
  • Loading branch information
yvann committed Nov 7, 2023
1 parent 1af7482 commit 79fab0a
Show file tree
Hide file tree
Showing 79 changed files with 824 additions and 1,098 deletions.
678 changes: 256 additions & 422 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/ms-npm-2.1.3-81ff3cfac1-aa92de6080.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/nopt-npm-6.0.0-5ea8050815-3c1128e07c.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
646 changes: 323 additions & 323 deletions .yarn/releases/yarn-4.0.0.cjs → .yarn/releases/yarn-4.0.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ supportedArchitectures:
os:
- linux

yarnPath: .yarn/releases/yarn-4.0.0.cjs
yarnPath: .yarn/releases/yarn-4.0.1.cjs
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
},
"devDependencies": {
"@jest/globals": "29.7.0",
"@swc/core": "1.3.95",
"@swc/core": "1.3.96",
"@swc/jest": "0.2.29",
"@tsconfig/node18": "18.2.2",
"@types/node": "18.18.7",
"@types/node": "18.18.8",
"@yarnpkg/sdks": "3.0.0",
"graphql": "16.8.1",
"jest": "29.7.0",
Expand All @@ -40,5 +40,5 @@
"type-fest": "4.6.0",
"typescript": "5.2.2"
},
"packageManager": "[email protected].0"
"packageManager": "[email protected].1"
}
10 changes: 10 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ describe('EventEmitter', () => {
expect(ee.eventNames()).toEqual([]);
});

it('throw on error', async () => {
const ee = new AsyncEventEmitter<{ [EventName.Pre]: {} }>();

await expect(
Promise.all([ee.throwOnError(), ee.emit('error', new Error('KO'))]),
).rejects.toThrowError('KO');

expect(ee.eventNames()).toEqual([]);
});

it('race works', async () => {
const ee = new AsyncEventEmitter<{
[EventName.Pre]: {};
Expand Down
25 changes: 25 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,31 @@ export class AsyncEventEmitter<TDataByName extends EventDataByName = any> {
});
}

public async throwOnError(
maybeSignal?: AbortSignal | number | null,
): Promise<void> {
const signal =
typeof maybeSignal === 'number'
? AbortSignal.timeout(maybeSignal)
: maybeSignal || undefined;

signal?.throwIfAborted();

let error: any;

try {
error = await this.wait('error', signal);
} catch (error) {
if (error instanceof AbortError) {
return;
}

throw error;
}

throw error;
}

public async race<TName extends EventName<TDataByName>>(
eventNames: ReadonlyArray<TName>,
maybeSignal?: AbortSignal | number | null,
Expand Down
Loading

0 comments on commit 79fab0a

Please sign in to comment.