Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core/src/logs/DdLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { DdNativeLogsType } from '../nativeModulesTypes';
import { DdAttributes } from '../rum/DdAttributes';
import type { ErrorSource } from '../rum/types';
import { validateContext } from '../utils/argsUtils';
import { getGlobalInstance } from '../utils/singletonUtils';

import { generateEventMapper } from './eventMapper';
import type {
Expand All @@ -21,6 +22,7 @@ import type {
RawLogWithError
} from './types';

const LOGS_MODULE = 'com.datadog.reactnative.logs';
const SDK_NOT_INITIALIZED_MESSAGE = 'DD_INTERNAL_LOG_SENT_BEFORE_SDK_INIT';

const generateEmptyPromise = () => new Promise<void>(resolve => resolve());
Expand Down Expand Up @@ -240,4 +242,4 @@ class DdLogsWrapper implements DdLogsType {
}
}

export const DdLogs = new DdLogsWrapper();
export const DdLogs = getGlobalInstance(LOGS_MODULE, () => new DdLogsWrapper());
5 changes: 4 additions & 1 deletion packages/core/src/rum/DdRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DdSdk } from '../sdk/DdSdk';
import { GlobalState } from '../sdk/GlobalState/GlobalState';
import { validateContext } from '../utils/argsUtils';
import { getErrorContext } from '../utils/errorUtils';
import { getGlobalInstance } from '../utils/singletonUtils';
import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider';
import type { TimeProvider } from '../utils/time-provider/TimeProvider';

Expand Down Expand Up @@ -43,6 +44,8 @@ import type {
PropagatorType
} from './types';

const RUM_MODULE = 'com.datadog.reactnative.rum';

const generateEmptyPromise = () => new Promise<void>(resolve => resolve());

class DdRumWrapper implements DdRumType {
Expand Down Expand Up @@ -501,4 +504,4 @@ const isOldStopActionAPI = (
return typeof args[0] === 'object' || typeof args[0] === 'undefined';
};

export const DdRum = new DdRumWrapper();
export const DdRum = getGlobalInstance(RUM_MODULE, () => new DdRumWrapper());
10 changes: 7 additions & 3 deletions packages/core/src/trace/DdTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import {
} from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
import type { DdTraceType } from '../types';
import { validateContext } from '../utils/argsUtils';
import { getGlobalInstance } from '../utils/singletonUtils';
import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider';

const TRACE_MODULE = 'com.datadog.reactnative.trace';

const timeProvider = new DefaultTimeProvider();

class DdTraceWrapper implements DdTraceType {
Expand Down Expand Up @@ -59,6 +62,7 @@ class DdTraceWrapper implements DdTraceType {
};
}

const DdTrace: DdTraceType = new DdTraceWrapper();

export { DdTrace };
export const DdTrace: DdTraceType = getGlobalInstance(
TRACE_MODULE,
() => new DdTraceWrapper()
);
75 changes: 75 additions & 0 deletions packages/core/src/utils/__tests__/singletonUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { getGlobalInstance } from '../singletonUtils';

describe('singletonUtils', () => {
const createdSymbols: symbol[] = [];
const g = (globalThis as unknown) as Record<PropertyKey, unknown>;

afterEach(() => {
for (const symbol of createdSymbols) {
delete g[symbol];
}

createdSymbols.length = 0;
jest.restoreAllMocks();
});

it('only creates one instance for the same key', () => {
const key = 'com.datadog.reactnative.test';
const symbol = Symbol.for(key);
createdSymbols.push(symbol);

const objectConstructor = jest.fn(() => ({ id: 1 }));
const a = getGlobalInstance(key, objectConstructor);
const b = getGlobalInstance(key, objectConstructor);

expect(a).toBe(b);
expect(objectConstructor).toHaveBeenCalledTimes(1);
expect(g[symbol]).toBe(a);
});

it('returns a pre-existing instance without creating a new one for the same key', () => {
const key = 'com.datadog.reactnative.test';
const symbol = Symbol.for(key);
createdSymbols.push(symbol);

const existing = { pre: true };
g[symbol] = existing;

const objectConstructor = jest.fn(() => ({ created: true }));
const result = getGlobalInstance(key, objectConstructor);

expect(result).toBe(existing);
expect(objectConstructor).not.toHaveBeenCalled();
});

it('creates a new instance for a different key', () => {
const keyA = 'com.datadog.reactnative.test.a';
const keyB = 'com.datadog.reactnative.test.b';
const symbolA = Symbol.for(keyA);
const symbolB = Symbol.for(keyB);
createdSymbols.push(symbolA, symbolB);

const a = getGlobalInstance(keyA, () => ({ id: 'A' }));
const b = getGlobalInstance(keyB, () => ({ id: 'B' }));

expect(a).not.toBe(b);
expect((a as any).id).toBe('A');
expect((b as any).id).toBe('B');
});

it('does not overwrite existing instance if called with a different constructor', () => {
const key = 'com.datadog.reactnative.test';
const symbol = Symbol.for(key);
createdSymbols.push(symbol);

const firstObjectConstructor = jest.fn(() => ({ id: 1 }));
const first = getGlobalInstance(key, firstObjectConstructor);

const secondObjectConstructor = jest.fn(() => ({ id: 2 }));
const second = getGlobalInstance(key, secondObjectConstructor);

expect(first).toBe(second);
expect(firstObjectConstructor).toHaveBeenCalledTimes(1);
expect(secondObjectConstructor).not.toHaveBeenCalled();
});
});
12 changes: 12 additions & 0 deletions packages/core/src/utils/singletonUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const getGlobalInstance = <T>(
key: string,
objectConstructor: () => T
): T => {
const symbol = Symbol.for(key);
const g = (globalThis as unknown) as Record<PropertyKey, unknown>;

if (!(symbol in g)) {
g[symbol] = objectConstructor();
}
return g[symbol] as T;
};
Loading