Skip to content

Commit

Permalink
Avoid circular import issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanscr committed Sep 23, 2024
1 parent 30984aa commit 6836ea0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 72 deletions.
5 changes: 3 additions & 2 deletions packages/teams-js/src/internal/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable strict-null-checks/all */

import { ApiName, ApiVersionNumber, getApiVersionTag, HostToAppMessageDelayTelemetry } from '../internal/telemetry';
import { ApiName, ApiVersionNumber, getApiVersionTag } from '../internal/telemetry';
import { FrameContexts } from '../public/constants';
import { SdkError } from '../public/interfaces';
import { latestRuntimeApiVersion } from '../public/runtime';
import { version } from '../public/version';
import { GlobalVars } from './globalVars';
import { callHandler } from './handlers';
import HostToAppMessageDelayTelemetry from './hostToAppTelemetry';
import { DOMMessageEvent, ExtendedWindow } from './interfaces';
import {
deserializeMessageRequest,
Expand Down Expand Up @@ -719,7 +720,7 @@ function handleIncomingMessageFromParent(evt: DOMMessageEvent): void {
if (callbackId) {
const callback = CommunicationPrivate.callbacks.get(callbackId);
logger('Received a response from parent for message %s', callbackId.toString());
HostToAppMessageDelayTelemetry.telemetryHostToAppPerformanceMetrics(callbackId, message, logger);
HostToAppMessageDelayTelemetry.handlePerformanceMetrics(callbackId, message, logger);
if (callback) {
logger(
'Invoking the registered callback for message %s with arguments %o',
Expand Down
66 changes: 66 additions & 0 deletions packages/teams-js/src/internal/hostToAppTelemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Debugger } from 'debug';

import { handleHostToAppPerformanceMetrics } from './handlers';
import { CallbackInformation } from './interfaces';
import { MessageResponse } from './messageObjects';
import { getCurrentTimestamp } from './utils';
import { UUID as MessageUUID } from './uuidObject';

/**
* @internal
* Limited to Microsoft-internal use
*/
export default class HostToAppMessageDelayTelemetry {
private static callbackInformation: Map<MessageUUID, CallbackInformation> = new Map();

/**
* @internal
* Limited to Microsoft-internal use
*
* Store information about a particular message.
* @param messageUUID The message id for the request.
* @param callbackInformation The information of the callback.
*/
public static storeCallbackInformation(messageUUID: MessageUUID, callbackInformation: CallbackInformation): void {
HostToAppMessageDelayTelemetry.callbackInformation.set(messageUUID, callbackInformation);
}

/**
* @internal
* Limited to Microsoft-internal use
*/
public static clearMessages(): void {
HostToAppMessageDelayTelemetry.callbackInformation.clear();
}

/**
* @internal
* Limited to Microsoft-internal use
*/
public static deleteMessageInformation(callbackId: MessageUUID): void {
HostToAppMessageDelayTelemetry.callbackInformation.delete(callbackId);
}

/**
* @internal
* Limited to Microsoft-internal use
*
* Executes telemetry actions related to host to app performance metrics.
* @param callbackId The message id for the request.
* @param message The response from the host.
* @param logger The logger in case an error occurs.
*/
public static handlePerformanceMetrics(callbackID: MessageUUID, message: MessageResponse, logger: Debugger): void {
const callbackInformation = HostToAppMessageDelayTelemetry.callbackInformation.get(callbackID);
if (callbackInformation && message.timestamp) {
handleHostToAppPerformanceMetrics({
actionName: callbackInformation.name,
messageDelay: getCurrentTimestamp() - message.timestamp,
messageWasCreatedAt: callbackInformation.calledAt,
});
HostToAppMessageDelayTelemetry.deleteMessageInformation(callbackID);
} else {
logger('Unable to send performance metrics for callback %i with arguments %o', callbackID, message.args);
}
}
}
69 changes: 0 additions & 69 deletions packages/teams-js/src/internal/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { debug as registerLogger, Debugger } from 'debug';

import { handleHostToAppPerformanceMetrics } from './handlers';
import { CallbackInformation } from './interfaces';
import { MessageResponse } from './messageObjects';
import { getCurrentTimestamp } from './utils';
import { UUID as MessageUUID } from './uuidObject';

const topLevelLogger = registerLogger('teamsJs');

/**
Expand Down Expand Up @@ -56,69 +50,6 @@ export const enum ApiVersionNumber {
V_3 = 'v3',
}

/**
* @internal
* Limited to Microsoft-internal use
*/
export class HostToAppMessageDelayTelemetry {
private static callbackInformation: Map<MessageUUID, CallbackInformation> = new Map();

/**
* @internal
* Limited to Microsoft-internal use
*
* Store information about a particular message.
* @param messageUUID The message id for the request.
* @param callbackInformation The information of the callback.
*/
public static storeCallbackInformation(messageUUID: MessageUUID, callbackInformation: CallbackInformation): void {
HostToAppMessageDelayTelemetry.callbackInformation.set(messageUUID, callbackInformation);
}

/**
* @internal
* Limited to Microsoft-internal use
*/
public static clearMessages(): void {
HostToAppMessageDelayTelemetry.callbackInformation.clear();
}

/**
* @internal
* Limited to Microsoft-internal use
*/
public static deleteMessageInformation(callbackId: MessageUUID): void {
HostToAppMessageDelayTelemetry.callbackInformation.delete(callbackId);
}

/**
* @internal
* Limited to Microsoft-internal use
*
* Executes telemetry actions related to host to app performance metrics.
* @param callbackId The message id for the request.
* @param message The response from the host.
* @param logger A logger for logging any possible error.
*/
public static telemetryHostToAppPerformanceMetrics(
callbackID: MessageUUID,
message: MessageResponse,
logger: debug.Debugger,
): void {
const callbackInformation = HostToAppMessageDelayTelemetry.callbackInformation.get(callbackID);
if (callbackInformation && message.timestamp) {
handleHostToAppPerformanceMetrics({
actionName: callbackInformation.name,
messageDelay: getCurrentTimestamp() - message.timestamp,
messageWasCreatedAt: callbackInformation.calledAt,
});
HostToAppMessageDelayTelemetry.deleteMessageInformation(callbackID);
} else {
logger('Unable to send performance metrics for callback %i with arguments %o', callbackID, message.args);
}
}
}

export const enum ApiName {
App_GetContext = 'app.getContext',
App_Initialize = 'app.initialize',
Expand Down
1 change: 0 additions & 1 deletion packages/teams-js/test/internal/communication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Utils } from '../utils';

jest.mock('../../src/internal/handlers', () => ({
callHandler: jest.fn(),
handleHostToAppPerformanceMetrics: jest.fn(),
}));

const testApiVersion = getApiVersionTag(ApiVersionNumber.V_1, 'mockedApiName' as ApiName);
Expand Down

0 comments on commit 6836ea0

Please sign in to comment.