Skip to content

Commit

Permalink
Add timestamp for each message request.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
juanscr committed Sep 11, 2024
1 parent 8377643 commit 50aa7e6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/teams-js/src/internal/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -956,7 +956,7 @@ function createMessageRequest(
id: messageId,
uuid: messageUuid,
func: func,
timestamp: Date.now(),
timestamp: getCurrentTimestamp(),
args: args || [],
apiVersionTag: apiVersionTag,
};
Expand All @@ -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: [],
Expand Down
1 change: 1 addition & 0 deletions packages/teams-js/src/internal/messageObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
14 changes: 14 additions & 0 deletions packages/teams-js/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 50aa7e6

Please sign in to comment.