From 50aa7e6e1dd004a3e3ce1184a6dda5dccfa9d5a4 Mon Sep 17 00:00:00 2001 From: JuanSe Cardenas Rodriguez Date: Mon, 2 Sep 2024 16:57:24 -0500 Subject: [PATCH] Add timestamp for each message request. With this new timestamped messaged, the host can properly compare and evaluate how much time the request got delayed to an issue on a busy event loop. --- packages/teams-js/src/internal/communication.ts | 6 +++--- packages/teams-js/src/internal/messageObjects.ts | 1 + packages/teams-js/src/internal/utils.ts | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/teams-js/src/internal/communication.ts b/packages/teams-js/src/internal/communication.ts index 3f32f1fa81..097bbb8acd 100644 --- a/packages/teams-js/src/internal/communication.ts +++ b/packages/teams-js/src/internal/communication.ts @@ -29,7 +29,7 @@ import { tryPolyfillWithNestedAppAuthBridge, } from './nestedAppAuthUtils'; import { getLogger, isFollowingApiVersionTagFormat } from './telemetry'; -import { ssrSafeWindow } from './utils'; +import { getCurrentTimestamp, ssrSafeWindow } from './utils'; import { UUID as MessageUUID } from './uuidObject'; import { validateOrigin } from './validOrigins'; @@ -956,7 +956,7 @@ function createMessageRequest( id: messageId, uuid: messageUuid, func: func, - timestamp: Date.now(), + timestamp: getCurrentTimestamp(), args: args || [], apiVersionTag: apiVersionTag, }; @@ -981,7 +981,7 @@ function createNestedAppAuthRequest(message: string): NestedAppAuthRequest { id: messageId, uuid: messageUuid, func: 'nestedAppAuth.execute', - timestamp: Date.now(), + timestamp: getCurrentTimestamp(), // Since this is a nested app auth request, we don't need to send any args. // We avoid overloading the args array with the message to avoid potential issues processing of these messages on the hubSDK. args: [], diff --git a/packages/teams-js/src/internal/messageObjects.ts b/packages/teams-js/src/internal/messageObjects.ts index 84f5f10619..bef5a4107e 100644 --- a/packages/teams-js/src/internal/messageObjects.ts +++ b/packages/teams-js/src/internal/messageObjects.ts @@ -58,6 +58,7 @@ export interface MessageResponse { uuid?: MessageUUID; // eslint-disable-next-line @typescript-eslint/no-explicit-any args?: any[]; + timestamp?: number; isPartialResponse?: boolean; // If the message is partial, then there will be more future responses for the given message ID. } diff --git a/packages/teams-js/src/internal/utils.ts b/packages/teams-js/src/internal/utils.ts index 03b12c82e8..aea8f21878 100644 --- a/packages/teams-js/src/internal/utils.ts +++ b/packages/teams-js/src/internal/utils.ts @@ -528,3 +528,17 @@ export function validateUuid(id: string | undefined | null): void { throw new Error('id must be a valid UUID'); } } + +/** + * Cache if performance timers are available to avoid redoing this on each function call. + */ +const supportsPerformanceTimers = 'performance' in window && 'now' in window.performance; + +/** + * @internal + * Limited to Microsoft-internal use + * @returns current timestamp in milliseconds + */ +export function getCurrentTimestamp(): number { + return supportsPerformanceTimers ? window.performance.now() + window.performance.timeOrigin : new Date().getTime(); +}