Skip to content

Commit

Permalink
chore: Tune contextUpdate event generation (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Dec 14, 2024
1 parent 28ada10 commit cbb42bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/commands/bidi/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const BIDI_EVENT_NAME = 'bidiEvent';
export const CONTEXT_UPDATED_EVENT = 'appium.contextUpdated';
export const CONTEXT_UPDATED_EVENT = (domain: string) => `appium:${domain}.contextUpdated`;
/** @deprecated Use {@link CONTEXT_UPDATED_EVENT} instead */
export const CONTEXT_UPDATED_EVENT_OBSOLETE = 'appium.contextUpdated';
export const LOG_ENTRY_ADDED_EVENT = 'log.entryAdded';
21 changes: 18 additions & 3 deletions lib/commands/bidi/models.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import type { LogEntryAddedEvent, ContextUpdatedEvent } from './types';
import { NATIVE_WIN } from '../context/helpers';
import { CONTEXT_UPDATED_EVENT, LOG_ENTRY_ADDED_EVENT } from './constants';
import {
CONTEXT_UPDATED_EVENT,
LOG_ENTRY_ADDED_EVENT,
CONTEXT_UPDATED_EVENT_OBSOLETE,
} from './constants';
import type { LogcatRecord as LogEntry } from 'appium-adb';

export function makeContextUpdatedEvent(contextName: string): ContextUpdatedEvent {
function toContextUpdatedEvent(method: string, contextName: string): ContextUpdatedEvent {
return {
method: CONTEXT_UPDATED_EVENT,
method,
params: {
name: contextName,
type: contextName === NATIVE_WIN ? 'NATIVE' : 'WEB',
},
};
}

export const makeContextUpdatedEvent = (contextName: string, domain: string) => toContextUpdatedEvent(
CONTEXT_UPDATED_EVENT(domain), contextName
);

/**
* @deprecated Use {@link makeContextUpdatedEvent} instead
*/
export const makeObsoleteContextUpdatedEvent = (contextName: string) => toContextUpdatedEvent(
CONTEXT_UPDATED_EVENT_OBSOLETE, contextName
);

export function makeLogEntryAddedEvent(entry: LogEntry, context: string, type: string): LogEntryAddedEvent {
return {
context,
Expand Down
5 changes: 3 additions & 2 deletions lib/commands/context/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from './helpers';
import {APP_STATE} from '../app-management';
import { BIDI_EVENT_NAME } from '../bidi/constants';
import { makeContextUpdatedEvent } from '../bidi/models';
import { makeContextUpdatedEvent, makeObsoleteContextUpdatedEvent } from '../bidi/models';

// https://github.com/appium/appium/issues/20710
const DEFAULT_NATIVE_WINDOW_HANDLE = '1';
Expand Down Expand Up @@ -382,7 +382,8 @@ export function isChromedriverContext(viewName) {
*/
export async function notifyBiDiContextChange() {
const name = await this.getCurrentContext();
this.eventEmitter.emit(BIDI_EVENT_NAME, makeContextUpdatedEvent(name));
this.eventEmitter.emit(BIDI_EVENT_NAME, makeContextUpdatedEvent(_.toLower(String(this.opts.automationName)), name));
this.eventEmitter.emit(BIDI_EVENT_NAME, makeObsoleteContextUpdatedEvent(name));
}

/**
Expand Down

0 comments on commit cbb42bb

Please sign in to comment.