-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
483 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
android/src/main/java/io/sentry/react/RNSentryTimeToDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.sentry.react; | ||
|
||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule; | ||
|
||
import android.view.Choreographer; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import io.sentry.SentryDate; | ||
import io.sentry.SentryDateProvider; | ||
import io.sentry.android.core.SentryAndroidDateProvider; | ||
|
||
|
||
public class RNSentryTimeToDisplay { | ||
|
||
public void GetTimeToDisplay(Promise promise) { | ||
Choreographer choreographer = Choreographer.getInstance(); | ||
|
||
// Invoke the callback after the frame is rendered | ||
choreographer.postFrameCallback(new Choreographer.FrameCallback() { | ||
@Override | ||
public void doFrame(long frameTimeNanos) { | ||
final @NotNull SentryDateProvider dateProvider = new SentryAndroidDateProvider(); | ||
|
||
final SentryDate endDate = dateProvider.now(); | ||
|
||
promise.resolve(endDate.nanoTimestamp() / 1e9); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#import <React/RCTBridgeModule.h> | ||
|
||
@interface RNSentryTimeToDisplay : NSObject | ||
|
||
- (void)getTimeToDisplay:(RCTResponseSenderBlock)callback; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#import "RNSentryTimeToDisplay.h" | ||
#import <QuartzCore/QuartzCore.h> | ||
#import <React/RCTLog.h> | ||
|
||
@implementation RNSentryTimeToDisplay | ||
{ | ||
CADisplayLink *displayLink; | ||
RCTResponseSenderBlock resolveBlock; | ||
} | ||
|
||
// Rename requestAnimationFrame to getTimeToDisplay | ||
- (void)getTimeToDisplay:(RCTResponseSenderBlock)callback | ||
{ | ||
// Store the resolve block to use in the callback. | ||
resolveBlock = callback; | ||
|
||
#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 | ||
resolveBlock(@[]); // Return nothing if not iOS. | ||
#endif | ||
} | ||
|
||
#if TARGET_OS_IOS | ||
- (void)handleDisplayLink:(CADisplayLink *)link { | ||
// Get the current time | ||
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970] * 1000.0; // Convert to milliseconds | ||
|
||
// Ensure the callback is valid and pass the current time back | ||
if (resolveBlock) { | ||
resolveBlock(@[@(currentTime)]); // Call the callback with the current time | ||
resolveBlock = nil; // Clear the block after it's called | ||
} | ||
|
||
// Invalidate the display link to stop future callbacks | ||
[displayLink invalidate]; | ||
displayLink = nil; | ||
} | ||
#endif | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { logger } from '@sentry/utils'; | ||
import type { EmitterSubscription } from 'react-native'; | ||
import { DeviceEventEmitter } from 'react-native'; | ||
|
||
import { NATIVE } from '../wrapper'; | ||
import { NewFrameEventName } from './sentryeventemitter'; | ||
|
||
export type FallBackNewFrameEvent = { newFrameTimestampInSeconds: number; isFallback?: boolean }; | ||
export interface SentryEventEmitterFallback { | ||
/** | ||
* Initializes the fallback event emitter | ||
* This method is synchronous in JS but the event emitter starts asynchronously. | ||
*/ | ||
initAsync: () => void; | ||
closeAll: () => void; | ||
startListenerAsync: () => void; | ||
} | ||
|
||
function timeNowNanosecond(): number { | ||
return Date.now() / 1000; // Convert to nanoseconds | ||
} | ||
|
||
/** | ||
* Creates emitter that allows to listen to UI Frame events when ready. | ||
*/ | ||
export function createSentryFallbackEventEmitter(): SentryEventEmitterFallback { | ||
let NativeEmitterCalled: boolean = false; | ||
let subscription: EmitterSubscription | undefined = undefined; | ||
let isListening = false; | ||
|
||
function defaultFallbackEventEmitter(): void { | ||
// Schedule the callback to be executed when all UI Frames have flushed. | ||
requestAnimationFrame(() => { | ||
if (NativeEmitterCalled) { | ||
NativeEmitterCalled = false; | ||
isListening = false; | ||
return; | ||
} | ||
const timestampInSeconds = timeNowNanosecond(); | ||
waitForNativeResponseOrFallback(timestampInSeconds, 'JavaScript'); | ||
}); | ||
} | ||
|
||
function waitForNativeResponseOrFallback(fallbackSeconds: number, origin: string): void { | ||
const maxRetries = 3; | ||
let retries = 0; | ||
|
||
const retryCheck = (): void => { | ||
if (NativeEmitterCalled) { | ||
NativeEmitterCalled = false; | ||
isListening = false; | ||
return; // Native Replied the bridge with a timestamp. | ||
} | ||
|
||
retries++; | ||
if (retries < maxRetries) { | ||
setTimeout(retryCheck, 1_000); | ||
} else { | ||
logger.log(`[Sentry] Native event emitter did not reply in time. Using ${origin} fallback emitter.`); | ||
isListening = false; | ||
DeviceEventEmitter.emit(NewFrameEventName, { | ||
newFrameTimestampInSeconds: fallbackSeconds, | ||
isFallback: true, | ||
}); | ||
} | ||
}; | ||
|
||
// Start the retry process | ||
retryCheck(); | ||
} | ||
|
||
return { | ||
initAsync() { | ||
subscription = DeviceEventEmitter.addListener(NewFrameEventName, () => { | ||
// Avoid noise from pages that we do not want to track. | ||
if (isListening) { | ||
NativeEmitterCalled = true; | ||
} | ||
}); | ||
}, | ||
|
||
startListenerAsync() { | ||
isListening = true; | ||
|
||
NATIVE.getNewScreenTimeToDisplay() | ||
.then(resolve => { | ||
if (resolve) { | ||
waitForNativeResponseOrFallback(resolve, 'Native'); | ||
} else { | ||
defaultFallbackEventEmitter(); | ||
} | ||
}) | ||
.catch((reason: Error) => { | ||
logger.error('Failed to recceive Native fallback timestamp.', reason); | ||
defaultFallbackEventEmitter(); | ||
}); | ||
}, | ||
|
||
closeAll() { | ||
subscription?.remove(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.