Skip to content

Commit

Permalink
fix: clear the buffered logs when browsing context is destroyed (#2592)
Browse files Browse the repository at this point in the history
Closes #475
  • Loading branch information
Lightning00Blade authored Sep 5, 2024
1 parent 0830f00 commit 36fb707
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/bidiMapper/modules/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ export class BrowsingContextImpl {
this.id
);
}

this.#eventManager.clearBufferedEvents(this.id);

this.#browsingContextStorage.deleteContextById(this.id);
}

Expand Down
41 changes: 25 additions & 16 deletions src/bidiMapper/modules/session/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export class EventManager extends EventEmitter<EventManagerEventsMap> {
*/
#eventBuffers = new Map<string, Buffer<EventWrapper>>();
/**
* Maps `eventName` + `browsingContext` + `channel` to last sent event id.
* Maps `eventName` + `browsingContext` to Map of channel to last id
* Used to avoid sending duplicated events when user
* subscribes -> unsubscribes -> subscribes.
*/
#lastMessageSent = new Map<string, number>();
#lastMessageSent = new Map<string, Map<string | null, number>>();
#subscriptionManager: SubscriptionManager;
#browsingContextStorage: BrowsingContextStorage;
/**
Expand All @@ -130,10 +130,9 @@ export class EventManager extends EventEmitter<EventManagerEventsMap> {
*/
static #getMapKey(
eventName: ChromiumBidi.EventNames,
browsingContext: BrowsingContext.BrowsingContext | null,
channel?: BidiPlusChannel
browsingContext: BrowsingContext.BrowsingContext | null
) {
return JSON.stringify({eventName, browsingContext, channel});
return JSON.stringify({eventName, browsingContext});
}

addSubscribeHook(
Expand Down Expand Up @@ -258,6 +257,14 @@ export class EventManager extends EventEmitter<EventManagerEventsMap> {
);
}

clearBufferedEvents(contextId: string): void {
for (const eventName of eventBufferLength.keys()) {
const bufferMapKey = EventManager.#getMapKey(eventName, contextId);

this.#eventBuffers.delete(bufferMapKey);
}
}

/**
* If the event is buffer-able, put it in the buffer.
*/
Expand Down Expand Up @@ -296,13 +303,20 @@ export class EventManager extends EventEmitter<EventManagerEventsMap> {

const lastSentMapKey = EventManager.#getMapKey(
eventName,
eventWrapper.contextId,
channel
eventWrapper.contextId
);
this.#lastMessageSent.set(
lastSentMapKey,
Math.max(this.#lastMessageSent.get(lastSentMapKey) ?? 0, eventWrapper.id)

const lastId = Math.max(
this.#lastMessageSent.get(lastSentMapKey)?.get(channel) ?? 0,
eventWrapper.id
);

const channelMap = this.#lastMessageSent.get(lastSentMapKey);
if (channelMap) {
channelMap.set(channel, lastId);
} else {
this.#lastMessageSent.set(lastSentMapKey, new Map([[channel, lastId]]));
}
}

/**
Expand All @@ -314,13 +328,8 @@ export class EventManager extends EventEmitter<EventManagerEventsMap> {
channel: BidiPlusChannel
): EventWrapper[] {
const bufferMapKey = EventManager.#getMapKey(eventName, contextId);
const lastSentMapKey = EventManager.#getMapKey(
eventName,
contextId,
channel
);
const lastSentMessageId =
this.#lastMessageSent.get(lastSentMapKey) ?? -Infinity;
this.#lastMessageSent.get(bufferMapKey)?.get(channel) ?? -Infinity;

const result: EventWrapper[] =
this.#eventBuffers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@

[test_close_iframe]
expected: FAIL

[test_with_new_navigation]
expected: [PASS, FAIL]
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@

[test_close_iframe]
expected: FAIL

[test_with_new_navigation]
expected: [PASS, FAIL]

0 comments on commit 36fb707

Please sign in to comment.