Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ namespace facebook::react {

namespace {

/**
* Default `durationThreshold` (in milliseconds) applied to `event` entries when
* the JS API does not provide an explicit value. Per the W3C Event Timing
* spec, observers for `event` entries default to a 104ms threshold:
* https://www.w3.org/TR/event-timing/#sec-modifications-perf-timeline
*/
constexpr HighResDuration DEFAULT_EVENT_DURATION_THRESHOLD =
HighResDuration::fromMilliseconds(104);

class PerformanceObserverWrapper : public jsi::NativeState {
public:
explicit PerformanceObserverWrapper(
Expand Down Expand Up @@ -337,7 +346,7 @@ void NativePerformance::observe(
}

auto durationThreshold =
options.durationThreshold.value_or(HighResDuration::zero());
options.durationThreshold.value_or(DEFAULT_EVENT_DURATION_THRESHOLD);

// observer of type multiple
if (options.entryTypes.has_value()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Event Timing API', () => {
const callback = jest.fn();

const observer = new PerformanceObserver(callback);
observer.observe({entryTypes: ['event']});
observer.observe({type: 'event', durationThreshold: 0});

const root = Fantom.createRoot();
Fantom.runTask(() => {
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Event Timing API', () => {
const callback = jest.fn();

const observer = new PerformanceObserver(callback);
observer.observe({entryTypes: ['event']});
observer.observe({type: 'event', durationThreshold: 0});

const SIMULATED_PROCESSING_DELAY = 50;

Expand Down Expand Up @@ -132,7 +132,7 @@ describe('Event Timing API', () => {
const callback = jest.fn();

const observer = new PerformanceObserver(callback);
observer.observe({entryTypes: ['event']});
observer.observe({type: 'event', durationThreshold: 0});

function MyComponent() {
const [count, setCount] = useState(0);
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('Event Timing API', () => {
const callback = jest.fn();

const observer = new PerformanceObserver(callback);
observer.observe({entryTypes: ['event']});
observer.observe({type: 'event', durationThreshold: 0});

let eventTimeStamp;

Expand Down Expand Up @@ -341,6 +341,52 @@ describe('Event Timing API', () => {
expect(callback).toHaveBeenCalledTimes(1);
});

it('defaults to 104ms for event entries per W3C spec', () => {
const callback = jest.fn();

const observer = new PerformanceObserver(callback);
observer.observe({type: 'event'});

let forceDelay = false;

function MyComponent() {
const [count, setCount] = useState(0);

return (
<View
onClick={event => {
if (forceDelay) {
sleep(104);
}
setCount(count + 1);
}}>
<Text>{count}</Text>
</View>
);
}

const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(<MyComponent />);
});

const element = nullthrows(
root.document.documentElement.firstElementChild,
);

expect(callback).not.toHaveBeenCalled();

Fantom.dispatchNativeEvent(element, 'click');

expect(callback).toHaveBeenCalledTimes(0);

forceDelay = true;

Fantom.dispatchNativeEvent(element, 'click');

expect(callback).toHaveBeenCalledTimes(1);
});

it('throws when used together with `entryTypes`', () => {
const observer = new PerformanceObserver(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function PerformanceObserverEventTimingExample(): React.Node {
setEntries(newEntries);
});

observer.observe({type: 'event'});
observer.observe({type: 'event', durationThreshold: 0});

return () => observer.disconnect();
}, []);
Expand Down
Loading