-
-
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.
Merge branch 'main' into antonis/3706-enable-native-spotlight
# Conflicts: # CHANGELOG.md
- Loading branch information
Showing
17 changed files
with
510 additions
and
57 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
42 changes: 42 additions & 0 deletions
42
packages/core/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,42 @@ | ||
package io.sentry.react; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.view.Choreographer; | ||
import com.facebook.react.bridge.Promise; | ||
import io.sentry.SentryDate; | ||
import io.sentry.SentryDateProvider; | ||
|
||
public final class RNSentryTimeToDisplay { | ||
|
||
private RNSentryTimeToDisplay() {} | ||
|
||
public static void getTimeToDisplay(Promise promise, SentryDateProvider dateProvider) { | ||
Looper mainLooper = Looper.getMainLooper(); | ||
|
||
if (mainLooper == null) { | ||
promise.reject( | ||
"GetTimeToDisplay is not able to measure the time to display: Main looper not" | ||
+ " available."); | ||
return; | ||
} | ||
|
||
// Ensure the code runs on the main thread | ||
new Handler(mainLooper) | ||
.post( | ||
() -> { | ||
try { | ||
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); | ||
}); | ||
} catch (Exception exception) { | ||
promise.reject("Failed to receive the instance of Choreographer", exception); | ||
} | ||
}); | ||
} | ||
} |
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,98 @@ | ||
import { logger, timestampInSeconds } from '@sentry/utils'; | ||
|
||
import { NATIVE } from '../wrapper'; | ||
import type { NewFrameEvent, SentryEventEmitter } from './sentryeventemitter'; | ||
import { createSentryEventEmitter, NewFrameEventName } from './sentryeventemitter'; | ||
|
||
export const FALLBACK_TIMEOUT_MS = 10_000; | ||
|
||
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; | ||
onceNewFrame: (listener: (event: FallBackNewFrameEvent) => void) => void; | ||
} | ||
|
||
/** | ||
* Creates emitter that allows to listen to UI Frame events when ready. | ||
*/ | ||
export function createSentryFallbackEventEmitter( | ||
emitter: SentryEventEmitter = createSentryEventEmitter(), | ||
fallbackTimeoutMs = FALLBACK_TIMEOUT_MS, | ||
): SentryEventEmitterFallback { | ||
let fallbackTimeout: ReturnType<typeof setTimeout> | undefined; | ||
let animationFrameTimestampSeconds: number | undefined; | ||
let nativeNewFrameTimestampSeconds: number | undefined; | ||
|
||
function getAnimationFrameTimestampSeconds(): 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 (fallbackTimeout === undefined) { | ||
return; | ||
} | ||
animationFrameTimestampSeconds = timestampInSeconds(); | ||
}); | ||
} | ||
|
||
function getNativeNewFrameTimestampSeconds(): void { | ||
NATIVE.getNewScreenTimeToDisplay() | ||
.then(resolve => { | ||
if (fallbackTimeout === undefined) { | ||
return; | ||
} | ||
nativeNewFrameTimestampSeconds = resolve ?? undefined; | ||
}) | ||
.catch(reason => { | ||
logger.error('Failed to receive Native fallback timestamp.', reason); | ||
}); | ||
} | ||
|
||
return { | ||
initAsync() { | ||
emitter.initAsync(NewFrameEventName); | ||
}, | ||
|
||
onceNewFrame(listener: (event: FallBackNewFrameEvent) => void) { | ||
animationFrameTimestampSeconds = undefined; | ||
nativeNewFrameTimestampSeconds = undefined; | ||
|
||
const internalListener = (event: NewFrameEvent): void => { | ||
if (fallbackTimeout !== undefined) { | ||
clearTimeout(fallbackTimeout); | ||
fallbackTimeout = undefined; | ||
} | ||
animationFrameTimestampSeconds = undefined; | ||
nativeNewFrameTimestampSeconds = undefined; | ||
listener(event); | ||
}; | ||
fallbackTimeout = setTimeout(() => { | ||
if (nativeNewFrameTimestampSeconds) { | ||
logger.log('Native event emitter did not reply in time'); | ||
return listener({ | ||
newFrameTimestampInSeconds: nativeNewFrameTimestampSeconds, | ||
isFallback: true, | ||
}); | ||
} else if (animationFrameTimestampSeconds) { | ||
logger.log('[Sentry] Native event emitter did not reply in time. Using JavaScript fallback emitter.'); | ||
return listener({ | ||
newFrameTimestampInSeconds: animationFrameTimestampSeconds, | ||
isFallback: true, | ||
}); | ||
} else { | ||
emitter.removeListener(NewFrameEventName, internalListener); | ||
logger.error('Failed to receive any fallback timestamp.'); | ||
} | ||
}, fallbackTimeoutMs); | ||
|
||
getNativeNewFrameTimestampSeconds(); | ||
getAnimationFrameTimestampSeconds(); | ||
emitter.once(NewFrameEventName, internalListener); | ||
}, | ||
}; | ||
} |
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.