Skip to content

Commit

Permalink
make type of eventName stronger, from string to SubscribeParametersEv…
Browse files Browse the repository at this point in the history
…ent (#566)

And use optional argument instead of `| undefined` for channel.
  • Loading branch information
Thiago Perrotta authored Mar 23, 2023
1 parent d2ed798 commit f71556f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
24 changes: 14 additions & 10 deletions src/bidiMapper/domains/events/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface IEventManager {
registerPromiseEvent(
event: Promise<Message.EventMessage>,
contextId: CommonDataTypes.BrowsingContext | null,
eventName: string
eventName: Message.EventNames
): void;

subscribe(
Expand Down Expand Up @@ -130,9 +130,9 @@ export class EventManager implements IEventManager {
* Returns consistent key to be used to access value maps.
*/
static #getMapKey(
eventName: string,
eventName: Message.EventNames,
browsingContext: CommonDataTypes.BrowsingContext | null,
channel: string | null | undefined = undefined
channel?: string | null
) {
return JSON.stringify({eventName, browsingContext, channel});
}
Expand All @@ -147,7 +147,7 @@ export class EventManager implements IEventManager {
registerPromiseEvent(
event: Promise<Message.EventMessage>,
contextId: CommonDataTypes.BrowsingContext | null,
eventName: Session.SubscribeParametersEvent
eventName: Message.EventNames
): void {
const eventWrapper = new EventWrapper(event, contextId);
const sortedChannels =
Expand Down Expand Up @@ -183,15 +183,19 @@ export class EventManager implements IEventManager {
await this.#handleDomains(eventName, contextId);
this.#subscriptionManager.subscribe(eventName, contextId, channel);
for (const eventWrapper of this.#getBufferedEvents(
eventName,
eventName as Message.EventNames,
contextId,
channel
)) {
// The order of the events is important.
this.#bidiServer.emitOutgoingMessage(
OutgoingBidiMessage.createFromPromise(eventWrapper.event, channel)
);
this.#markEventSent(eventWrapper, channel, eventName);
this.#markEventSent(
eventWrapper,
channel,
eventName as Message.EventNames
);
}
}
}
Expand All @@ -202,7 +206,7 @@ export class EventManager implements IEventManager {
* globally.
*/
async #handleDomains(
eventName: string,
eventName: Session.SubscribeParametersEvent,
contextId: CommonDataTypes.BrowsingContext | null
) {
// Enable network domain if user subscribed to any of network events.
Expand Down Expand Up @@ -236,7 +240,7 @@ export class EventManager implements IEventManager {
/**
* If the event is buffer-able, put it in the buffer.
*/
#bufferEvent(eventWrapper: EventWrapper, eventName: string) {
#bufferEvent(eventWrapper: EventWrapper, eventName: Message.EventNames) {
if (!eventBufferLength.has(eventName)) {
// Do nothing if the event is no buffer-able.
return;
Expand Down Expand Up @@ -265,7 +269,7 @@ export class EventManager implements IEventManager {
#markEventSent(
eventWrapper: EventWrapper,
channel: string | null,
eventName: string
eventName: Message.EventNames
) {
if (!eventBufferLength.has(eventName)) {
// Do nothing if the event is no buffer-able.
Expand All @@ -287,7 +291,7 @@ export class EventManager implements IEventManager {
* Returns events which are buffered and not yet sent to the given channel events.
*/
#getBufferedEvents(
eventName: string,
eventName: Message.EventNames,
contextId: CommonDataTypes.BrowsingContext | null,
channel: string | null
): EventWrapper[] {
Expand Down
23 changes: 16 additions & 7 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ export namespace Message {
| ErrorResult;

export type EventMessage =
// keep-sorted start
| BrowsingContext.Event
| Log.Event
| CDP.Event
| Log.Event
| Network.Event
| Script.Event;
// keep-sorted end;

export type ErrorCode =
| 'unknown error'
Expand All @@ -100,6 +102,15 @@ export namespace Message {
readonly stacktrace?: string;
};

export type EventNames =
// keep-sorted start
| BrowsingContext.EventNames
| CDP.EventNames
| Log.EventNames
| Network.EventNames
| Script.EventNames;
// keep-sorted end;

export class ErrorResponseClass implements Message.ErrorResult {
protected constructor(
error: Message.ErrorCode,
Expand Down Expand Up @@ -1091,16 +1102,14 @@ export namespace Session {
};

export type SubscribeParametersEvent =
| BrowsingContext.EventNames
// keep-sorted start
| Message.EventNames
| typeof BrowsingContext.AllEvents
| Log.EventNames
| typeof Log.AllEvents
| CDP.EventNames
| typeof CDP.AllEvents
| Network.EventNames
| typeof Log.AllEvents
| typeof Network.AllEvents
| Script.EventNames
| typeof Script.AllEvents;
// keep-sorted end;

// SessionSubscribeParameters = {
// events: [*text],
Expand Down

0 comments on commit f71556f

Please sign in to comment.