Skip to content

[FIX] Fixed RumSessionStarted listener implementation#839

Merged
marco-saia-datadog merged 1 commit into
developfrom
marcosaia/fix/native-event-emitter
Apr 3, 2025
Merged

[FIX] Fixed RumSessionStarted listener implementation#839
marco-saia-datadog merged 1 commit into
developfrom
marcosaia/fix/native-event-emitter

Conversation

@marco-saia-datadog
Copy link
Copy Markdown
Member

@marco-saia-datadog marco-saia-datadog commented Apr 3, 2025

What does this PR do?

Fixes the RumSessionStarted native event listener implementation.

What changed

  • FIXED: The Session ID was not propagated correctly as the listener was only registered after the SDK initialization
  • FIXED: Error raised when running tests, as NativeModules.DdSdk is undefined and NativeEventEmitter constructor requires a non-null argument (ISSUE: Invariant Violation: new NativeEventEmitter() requires a non-null argument. #832)
  • INTERNAL CHANGE: Calls to DdRum.getCurrentSessionId() will overwrite the cached RUM Session ID.
  • Implemented a more generalized approach for creating Native Event Emitters, with safe-guards to avoid adding multiple listeners to the same native event
  • Added unit tests.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)
  • If this PR is auto-generated, please make sure also to manually update the code related to the change

@marco-saia-datadog marco-saia-datadog requested a review from a team as a code owner April 3, 2025 14:43
Comment thread packages/core/src/__tests__/DdSdkReactNative.test.tsx Outdated
return;
}

eventEmitter.addListener(RUM_SESSION_STARTED_EVENT_KEY, (event: any) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Unexpected any. Specify a different type. (...read more)

Do not use the any type, as it is too broad and can lead to unexpected behavior.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

@marco-saia-datadog marco-saia-datadog Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


it('M registerRumSessionIdListener correctly registers a listener for RumSessionStarted', () => {
// GIVEN
const { registerRumSessionIdListener } = require('../sessionIdHelper');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

it('M createNativeEventEmitterForModule initializes NativeEventEmitter only once W { platform = Android }', () => {
// GIVEN
mockPlatform('android');
const createNativeEventEmitterForModule = require('../EventEmitter')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

const {
registerRumSessionIdListener,
removeRumSessionIdListeners
} = require('../sessionIdHelper');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

Comment thread packages/core/__mocks__/react-native.ts Outdated
Comment thread packages/core/src/sdk/EventEmitter/__tests__/EventEmitter.test.ts Outdated
Comment thread packages/core/src/sdk/EventEmitter/EventEmitter.ts Outdated
Comment thread packages/core/src/sdk/EventEmitter/EventEmitter.ts Outdated
Comment thread packages/core/src/sdk/EventEmitter/__tests__/EventEmitter.test.ts Outdated
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/fix/native-event-emitter branch from 4776906 to 8fe4cf7 Compare April 3, 2025 14:44
const {
setCachedRumSessionId,
getCachedRumSessionId
} = require('../sessionIdHelper');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

it('M createNativeEventEmitterForModule returns valid event emitter W { platform = Android, NativeModules = undefined }', () => {
// GIVEN
mockPlatform('android');
const createNativeEventEmitterForModule = require('../EventEmitter')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.


it('M registerRumSessionIdListener does not register more than one listener for RumSessionStarted', () => {
// GIVEN
const { registerRumSessionIdListener } = require('../sessionIdHelper');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/fix/native-event-emitter branch from 5c49257 to 511c41b Compare April 3, 2025 14:53
Comment thread packages/core/src/rum/sessionId/__tests__/sessionIdHelper.test.ts Outdated
it('M createNativeEventEmitterForModule returns valid event emitter W { platform = iOS, NativeModules = undefined }', () => {
// GIVEN
mockPlatform('ios');
const createNativeEventEmitterForModule = require('../EventEmitter')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

Comment thread packages/core/src/rum/sessionId/__tests__/sessionIdHelper.test.ts Outdated
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/fix/native-event-emitter branch from d46cdfa to a0afcce Compare April 3, 2025 15:04
Comment thread packages/core/src/rum/sessionId/__tests__/sessionIdHelper.test.ts Outdated
it('M createNativeEventEmitterForModule returns valid event emitter W { platform = Android, NativeModules != undefined }', () => {
// GIVEN
mockPlatform('android');
const createNativeEventEmitterForModule = require('../EventEmitter')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

const {
registerRumSessionIdListener,
getCachedRumSessionId
} = require('../sessionIdHelper');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Require statement not part of import statement. (...read more)

Use ESM instead of CommonJS imports.

View in Datadog  Leave us feedback  Documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use require statements in these tests, to be able to use jest.resetModules() to restore the initial state of the modules.

@marco-saia-datadog marco-saia-datadog marked this pull request as draft April 3, 2025 15:12
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/fix/native-event-emitter branch from a0afcce to cb3722b Compare April 3, 2025 15:15
@marco-saia-datadog marco-saia-datadog marked this pull request as ready for review April 3, 2025 15:16
Comment thread packages/core/__mocks__/react-native.ts
@marco-saia-datadog marco-saia-datadog merged commit ed1a745 into develop Apr 3, 2025
8 checks passed
@marco-saia-datadog marco-saia-datadog deleted the marcosaia/fix/native-event-emitter branch April 3, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants