Skip to content

Commit

Permalink
fix java module name, add isAvailable to ignore macos
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-zimerman committed Sep 19, 2024
1 parent 1bb5c4d commit 6507648
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public class RNSentryTimeToDisplayModule extends NativeRNSentryTimeToDisplaySpec {

public static final String NAME = "RNSentryTimeToDisplayModule";
public static final String NAME = "RNSentryTimeToDisplay";

public RNSentryTimeToDisplayModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -40,4 +40,9 @@ public void doFrame(long frameTimeNanos) {
}
});
}

@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isAvailable() {
return true;
}
}
15 changes: 14 additions & 1 deletion ios/RNSentryTimeToDisplay.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ @implementation RNSentryTimeToDisplay
// Store the resolve block to use in the callback
resolveBlock = resolve;

#if TARGET_OS_IOS
// Create and add a display link to get the callback after the screen is rendered
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
#else
#endif
}

#if TARGET_OS_IOS
- (void)handleDisplayLink:(CADisplayLink *)link
{
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];

if (resolveBlock) {
resolveBlock(@(currentTime));
resolveBlock = nil;
Expand All @@ -34,5 +37,15 @@ - (void)handleDisplayLink:(CADisplayLink *)link
[displayLink invalidate];
displayLink = nil;
}
#endif

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isAvailable)
{
#if TARGET_OS_IOS
return @(YES);
#else
return @(NO); // MacOS
#endif
}

@end
1 change: 1 addition & 0 deletions src/js/NativeRNSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export type NativeScreenshot = {

export interface RNSentryTimeToDisplayModuleSpec {
requestAnimationFrame(): Promise<number>;
isAvailable(): boolean;
}

// The export must be here to pass codegen even if not used
Expand Down
1 change: 1 addition & 0 deletions src/js/NativeRNSentryTimeToDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TurboModuleRegistry } from 'react-native';
// Only extra allowed definitions are types (probably codegen bug)
export interface Spec extends TurboModule {
requestAnimationFrame(): Promise<number>;
isAvailable(): boolean;
}

// The export must be here to pass codegen even if not used
Expand Down
3 changes: 1 addition & 2 deletions src/js/utils/sentryeventemitterfallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { EmitterSubscription } from 'react-native';
import { DeviceEventEmitter, NativeModules } from 'react-native';

import type { Spec } from '../NativeRNSentryTimeToDisplay';
import { NATIVE } from '../wrapper';
import { isTurboModuleEnabled } from './environment';
import { ReactNativeLibraries } from './rnlibraries';
import { NewFrameEventName } from './sentryeventemitter';
Expand Down Expand Up @@ -93,7 +92,7 @@ export function createSentryFallbackEventEmitter(): SentryEventEmitterFallback {

startListenerAsync() {
isListening = true;
if (NATIVE.isNativeAvailable() && RNSentryTimeToDisplay !== undefined) {
if (RNSentryTimeToDisplay && RNSentryTimeToDisplay.isAvailable()) {
RNSentryTimeToDisplay.requestAnimationFrame()
.then((time: number) => {
waitForNativeResponseOrFallback(time, 'Native');
Expand Down
30 changes: 29 additions & 1 deletion test/utils/sentryeventemitterfallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createSentryFallbackEventEmitter } from '../../src/js/utils/sentryevent
jest.mock('react-native', () => {
const RNSentryTimeToDisplay: Spec = {
requestAnimationFrame: jest.fn(() => Promise.resolve(12345)),
isAvailable: () => true,
};
return {
DeviceEventEmitter: {
Expand Down Expand Up @@ -53,6 +54,7 @@ describe('SentryEventEmitterFallback', () => {
afterEach(() => {
// @ts-expect-error test
window.requestAnimationFrame.mockRestore();
RNSentryTimeToDisplay.isAvailable = () => true;
});

it('should initialize and add a listener', () => {
Expand Down Expand Up @@ -116,7 +118,33 @@ describe('SentryEventEmitterFallback', () => {
);
});

it('should start listener and call native when native is available', async () => {
it('should start listener and use fallback when native call is not available', async () => {
jest.useFakeTimers();
const fallbackTime = Date.now() / 1000;

RNSentryTimeToDisplay.isAvailable = () => false;
const animation = RNSentryTimeToDisplay.requestAnimationFrame as jest.Mock;

emitter.startListenerAsync();
await animation; // wait for the Native execution to be completed.

// Simulate retries and timer
jest.runAllTimers();

// Ensure fallback event is emitted
expect(DeviceEventEmitter.emit).toHaveBeenCalledWith(NewFrameEventName, {
newFrameTimestampInSeconds: fallbackTime,
isFallback: true,
});
expect(logger.log).toHaveBeenCalledWith(
expect.stringContaining(
'[Sentry] Native event emitter did not reply in time. Using JavaScript fallback emitter.',
),
);
});


it('should start listener and call native when native module is available', async () => {
const nativeTimestamp = 12345;

(RNSentryTimeToDisplay.requestAnimationFrame as jest.Mock).mockResolvedValueOnce(nativeTimestamp);
Expand Down

0 comments on commit 6507648

Please sign in to comment.