-
-
Notifications
You must be signed in to change notification settings - Fork 342
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
16 changed files
with
455 additions
and
10 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
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 |
---|---|---|
|
@@ -55,5 +55,4 @@ public List<ViewManager> createViewManagers( | |
new RNSentryOnDrawReporterManager(reactContext) | ||
); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
package io.sentry.react; | ||
|
||
import com.facebook.react.bridge.Promise; | ||
|
||
import android.view.Choreographer; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import io.sentry.SentryDate; | ||
import io.sentry.SentryDateProvider; | ||
import io.sentry.android.core.SentryAndroidDateProvider; | ||
|
||
public class RNSentryTimeToDisplay { | ||
public static void GetTimeToDisplay(Promise promise, SentryDateProvider dateProvider) { | ||
Choreographer choreographer = Choreographer.getInstance(); | ||
|
||
// Invoke the callback after the frame is rendered | ||
choreographer.postFrameCallback(frameTimeNanos -> { | ||
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
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,93 @@ | ||
import { logger, timestampInSeconds } from '@sentry/utils'; | ||
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; | ||
startListenerAsync: () => void; | ||
} | ||
|
||
/** | ||
* Creates emitter that allows to listen to UI Frame events when ready. | ||
*/ | ||
export function createSentryFallbackEventEmitter(): SentryEventEmitterFallback { | ||
let NativeEmitterCalled: boolean = false; | ||
let isListening = false; | ||
|
||
function defaultFallbackEventEmitter(): void { | ||
// https://reactnative.dev/docs/timers#timers | ||
// NOTE: The current implementation of requestAnimationFrame is the same | ||
// as setTimeout(0). This isn't exactly how requestAnimationFrame | ||
// is supposed to work on web, so it doesn't get called when UI Frames are rendered.: https://github.com/facebook/react-native/blob/5106933c750fee2ce49fe1945c3e3763eebc92bc/packages/react-native/ReactCommon/react/runtime/TimerManager.cpp#L442-L443 | ||
requestAnimationFrame(() => { | ||
if (NativeEmitterCalled) { | ||
NativeEmitterCalled = false; | ||
isListening = false; | ||
return; | ||
} | ||
const seconds = timestampInSeconds(); | ||
waitForNativeResponseOrFallback(seconds, 'JavaScript'); | ||
}); | ||
} | ||
|
||
function waitForNativeResponseOrFallback(fallbackSeconds: number, origin: string): void { | ||
let firstAttemptCompleted = false; | ||
|
||
const checkNativeResponse = (): void => { | ||
if (NativeEmitterCalled) { | ||
NativeEmitterCalled = false; | ||
isListening = false; | ||
return; // Native Replied the bridge with a timestamp. | ||
} | ||
if (!firstAttemptCompleted) { | ||
firstAttemptCompleted = true; | ||
setTimeout(checkNativeResponse, 3_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 | ||
checkNativeResponse(); | ||
} | ||
|
||
return { | ||
initAsync() { | ||
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(); | ||
}); | ||
}, | ||
}; | ||
} |
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.