Skip to content

Commit add1721

Browse files
committed
feat(wpt): add wpt event emitter with types
1 parent 72bf5fa commit add1721

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

src/wpt/Wpt.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import lighthouse from 'lighthouse';
22
import { RunnerResult } from 'lighthouse/types/externs';
33
import { launch } from 'chrome-launcher';
44
import { pipe, concurrent, toAsync, toArray, map } from '@fxts/core';
5+
import EventEmitter from 'events';
56

67
import { Reporter } from './tools';
78
import { WptContext, WptConfig, WptAuditPath } from './context';
89
import * as constants from './constants';
10+
import { WptEventHandlersEventMap } from './Wpt.types';
911

1012
export interface WebPerformanceTesterProps {
1113
reporter?: Reporter;
@@ -17,21 +19,30 @@ export interface WptAuditPathItem extends WptAuditPath {
1719

1820
const context = WptContext();
1921

20-
class WebPerformanceTester {
22+
class WebPerformanceTester extends EventEmitter {
2123
private reporter: Reporter;
2224

2325
constructor(props?: WebPerformanceTesterProps) {
26+
super();
2427
this.reporter = props?.reporter ?? new Reporter();
28+
this.typedEmit('onReadyWpt', { context });
29+
}
30+
31+
private typedEmit(
32+
eventName: keyof WptEventHandlersEventMap,
33+
eventArgs: WptEventHandlersEventMap[keyof WptEventHandlersEventMap],
34+
) {
35+
this.emit(eventName, eventArgs);
2536
}
2637

2738
private async createLighthouseReport(
2839
lighthouseResult: RunnerResult,
2940
auditPath: WptAuditPathItem,
3041
) {
31-
console.log(`Reporter:start - "${auditPath.name}"`);
42+
this.typedEmit('onReportStart', { auditPath, context });
3243
this.reporter.saveAuditsReportFile(lighthouseResult.report, auditPath.name);
3344
await this.reporter.createAuditsReport(lighthouseResult.lhr.audits);
34-
console.log(`Reporter:success - "${auditPath.name}"`);
45+
this.typedEmit('onReportEnd', { auditPath, context });
3546
}
3647

3748
private async runLighthouse(
@@ -44,7 +55,7 @@ class WebPerformanceTester {
4455
port: options.port + index,
4556
});
4657
try {
47-
console.log(`Lighthouse:start - "${auditPath.name}"`);
58+
this.typedEmit('onStartedLighthouse', { auditPath, context });
4859
const lighthouseResult: RunnerResult = await lighthouse(auditPath.url, {
4960
...options,
5061
screenEmulation:
@@ -54,22 +65,22 @@ class WebPerformanceTester {
5465
port: options.port + index,
5566
});
5667
await this.createLighthouseReport(lighthouseResult, auditPath);
57-
console.log(
58-
`Lighthouse:success - Finish the "${auditPath.name}" wpt test`,
59-
);
68+
this.typedEmit('onFinishedLighthouse', { auditPath, context });
6069
} catch (error) {
61-
console.error(`Lighthouse:fail - "${auditPath.name}"`);
62-
console.error('Lighthouse:fail - ', error);
70+
if (error instanceof Error) {
71+
this.typedEmit('onErrorLighthouse', { auditPath, context, error });
72+
} else {
73+
this.typedEmit('onErrorLighthouse', { auditPath, context });
74+
}
6375
} finally {
6476
chrome.kill();
6577
}
6678
}
6779

6880
async run() {
6981
try {
82+
this.typedEmit('onStartedWpt', { context });
7083
let index = 0;
71-
72-
console.log('Run:start');
7384
const { concurrency, options } = context.config;
7485
await pipe(
7586
context.auditPathsItems,
@@ -81,9 +92,13 @@ class WebPerformanceTester {
8192
concurrent(concurrency),
8293
(values) => toArray(values),
8394
);
84-
console.log('Run:success - All wpt tests were finished');
95+
this.typedEmit('onFinishedWpt', { context });
8596
} catch (error) {
86-
console.error('Run:fail - ', error);
97+
if (error instanceof Error) {
98+
this.typedEmit('onErrorWpt', { context, error });
99+
} else {
100+
this.typedEmit('onErrorWpt', { context });
101+
}
87102
}
88103
}
89104
}

src/wpt/Wpt.types.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { WptContextValue } from './context';
2+
import { WptAuditPathItem } from './Wpt';
3+
4+
export interface WptErrorEvent {
5+
error: Error;
6+
}
7+
8+
export interface WptEvent {
9+
context: WptContextValue;
10+
}
11+
12+
export interface WptLighthouseEvent extends WptEvent {
13+
auditPath: WptAuditPathItem;
14+
}
15+
16+
export interface WptReportEvent extends WptEvent {
17+
auditPath: WptAuditPathItem;
18+
}
19+
20+
export interface WptEventHandlersEventMap {
21+
onReadyWpt: WptEvent;
22+
onStartedWpt: WptEvent;
23+
onFinishedWpt: WptEvent;
24+
onErrorWpt: WptEvent & WptErrorEvent;
25+
onStartedLighthouse: WptLighthouseEvent;
26+
onFinishedLighthouse: WptLighthouseEvent;
27+
onErrorLighthouse: WptLighthouseEvent & WptErrorEvent;
28+
onReportStart: WptReportEvent;
29+
onReportEnd: WptReportEvent;
30+
}

0 commit comments

Comments
 (0)