Skip to content

Commit 5dcd290

Browse files
author
Natallia Harshunova
committed
Emit Aggregated issue only for the first time
1 parent 997e787 commit 5dcd290

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/McpContext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import type {Context, DevToolsData} from './tools/ToolDefinition.js';
3131
import type {TraceResult} from './trace-processing/parse.js';
3232
import {WaitForHelper} from './WaitForHelper.js';
3333

34-
35-
3634
export interface TextSnapshotNode extends SerializedAXNode {
3735
id: string;
3836
backendNodeId?: number;

src/PageCollector.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,25 @@ export class PageCollector<T> {
9494
});
9595
}
9696

97-
public async addPage(page: Page) {
97+
public addPage(page: Page) {
9898
if (this.storage.has(page)) {
9999
return;
100100
}
101-
await this.#initializePage(page);
101+
this.#initializePage(page);
102102
}
103103

104-
async #initializePage(page: Page) {
104+
#initializePage(page: Page) {
105105
const idGenerator = createIdGenerator();
106106
const storedLists: Array<Array<WithSymbolId<T>>> = [[]];
107107
this.storage.set(page, storedLists);
108108

109-
const collector = (value: T) => {
109+
const listeners = this.#listenersInitializer(value => {
110110
const withId = value as WithSymbolId<T>;
111-
// Assign an ID only if it's a new item.
112-
if (!withId[stableIdSymbol]) {
113-
withId[stableIdSymbol] = idGenerator();
114-
}
111+
withId[stableIdSymbol] = idGenerator();
115112

116113
const navigations = this.storage.get(page) ?? [[]];
117-
const currentNavigation = navigations[0];
118-
119-
// The issues aggregator sends the same object instance for updates, so we just
120-
// need to ensure it's not in the list.
121-
if (!currentNavigation.includes(withId)) {
122-
currentNavigation.push(withId);
123-
}
124-
};
125-
126-
const listeners = this.#listenersInitializer(collector);
114+
navigations[0].push(withId);
115+
});
127116

128117
listeners['framenavigated'] = (frame: Frame) => {
129118
// Only split the storage on main frame navigation
@@ -145,6 +134,7 @@ export class PageCollector<T> {
145134
if (!navigations) {
146135
return;
147136
}
137+
// Add the latest navigation first
148138
navigations.unshift([]);
149139
navigations.splice(this.#maxNavigationSaved);
150140
}
@@ -221,27 +211,31 @@ export class ConsoleCollector extends PageCollector<ConsoleMessage | Error | Agg
221211
#issuesAggregators = new WeakMap<Page, IssueAggregator>();
222212
#mockIssuesManagers = new WeakMap<Page, FakeIssuesManager>();
223213

224-
override async addPage(page: Page) {
214+
override addPage(page: Page): void {
225215
if (this.storage.has(page)) {
226216
return;
227217
}
228-
await super.addPage(page);
229-
await this.subscribeForIssues(page);
218+
super.addPage(page);
219+
void this.subscribeForIssues(page);
230220
}
231221
async subscribeForIssues(page: Page) {
232222
if (!this.#seenIssueKeys.has(page)) {
233223
this.#seenIssueKeys.set(page, new Set());
234224
}
235225

236226
const mockManager = new FakeIssuesManager();
237-
// @ts-expect-error Aggregator receives partial IssuesManager
238227
const aggregator = new IssueAggregator(mockManager);
239228
this.#mockIssuesManagers.set(page, mockManager);
240229
this.#issuesAggregators.set(page, aggregator);
241230

242231
aggregator.addEventListener(
243232
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
244233
event => {
234+
const withId = event.data as WithSymbolId<AggregatedIssue>;
235+
// Emit aggregated issue only if it's a new one
236+
if (withId[stableIdSymbol]) {
237+
return;
238+
}
245239
page.emit('issue', event.data);
246240
},
247241
);
@@ -305,6 +299,9 @@ export class NetworkCollector extends PageCollector<HTTPRequest> {
305299
: false;
306300
});
307301

302+
// Keep all requests since the last navigation request including that
303+
// navigation request itself.
304+
// Keep the reference
308305
if (lastRequestIdx !== -1) {
309306
const fromCurrentNavigation = requests.splice(lastRequestIdx);
310307
navigations.unshift(fromCurrentNavigation);

0 commit comments

Comments
 (0)