Skip to content

Commit 79cf810

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Define Bridging protocol for HighResTimeStamp and HighResTimeDuration (#51253)
Summary: # Changelog: [Internal] Defines bridging template for `HighResTimeStamp` and `HighResTimeDuration`. When these values are passed to JavaScript over bridge, they will be converted to [`DOMHighResTimeStamp`](https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp). Also the other way around, when we list `HighResTimeStamp` or `HighResTimeDuration` as a type of TurboModule spec, it will expect number value from JavaScript. Reviewed By: rubennorte Differential Revision: D74506516
1 parent 9e5ff2a commit 79cf810

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

packages/react-native/ReactCommon/react/bridging/Bridging.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <react/bridging/Error.h>
1616
#include <react/bridging/EventEmitter.h>
1717
#include <react/bridging/Function.h>
18+
#include <react/bridging/HighResTimeStamp.h>
1819
#include <react/bridging/Number.h>
1920
#include <react/bridging/Object.h>
2021
#include <react/bridging/Promise.h>

packages/react-native/ReactCommon/react/bridging/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ add_library(react_bridging OBJECT ${react_bridging_SRC})
1414

1515
target_include_directories(react_bridging PUBLIC ${REACT_COMMON_DIR})
1616

17-
target_link_libraries(react_bridging jsi callinvoker)
17+
target_link_libraries(react_bridging jsi callinvoker react_timing)
1818
target_compile_reactnative_options(react_bridging PRIVATE "ReactNative")
1919
target_compile_options(react_bridging PRIVATE -Wpedantic)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/bridging/Base.h>
11+
#include <react/timing/primitives.h>
12+
13+
namespace facebook::react {
14+
15+
template <>
16+
struct Bridging<HighResTimeStamp> {
17+
static HighResTimeStamp fromJs(
18+
jsi::Runtime& /*rt*/,
19+
const jsi::Value& jsiValue) {
20+
return HighResTimeStamp::fromDOMHighResTimeStamp(jsiValue.asNumber());
21+
}
22+
23+
static double toJs(jsi::Runtime& /*rt*/, const HighResTimeStamp& value) {
24+
return value.toDOMHighResTimeStamp();
25+
}
26+
};
27+
28+
template <>
29+
struct Bridging<HighResDuration> {
30+
static HighResDuration fromJs(
31+
jsi::Runtime& /*rt*/,
32+
const jsi::Value& jsiValue) {
33+
return HighResDuration::fromDOMHighResTimeStamp(jsiValue.asNumber());
34+
}
35+
36+
static double toJs(jsi::Runtime& /*rt*/, const HighResDuration& value) {
37+
return value.toDOMHighResTimeStamp();
38+
}
39+
};
40+
41+
} // namespace facebook::react

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,13 @@ TEST_F(BridgingTest, supportTest) {
654654
EXPECT_FALSE((bridging::supportsFromJs<jsi::Function, jsi::Array>));
655655
EXPECT_FALSE((bridging::supportsFromJs<jsi::Function, jsi::Array&>));
656656

657+
// Ensure we can create HighResTimeStamp and HighResDuration from JSI
658+
// values.
659+
EXPECT_TRUE((bridging::supportsFromJs<HighResTimeStamp, jsi::Value>));
660+
EXPECT_TRUE((bridging::supportsFromJs<HighResTimeStamp, jsi::Value&>));
661+
EXPECT_TRUE((bridging::supportsFromJs<HighResDuration, jsi::Value>));
662+
EXPECT_TRUE((bridging::supportsFromJs<HighResDuration, jsi::Value&>));
663+
657664
// Ensure we can convert some basic types to JSI values.
658665
EXPECT_TRUE((bridging::supportsToJs<bool>));
659666
EXPECT_TRUE((bridging::supportsToJs<int>));
@@ -677,6 +684,11 @@ TEST_F(BridgingTest, supportTest) {
677684
EXPECT_FALSE((bridging::supportsToJs<double, jsi::Object>));
678685
EXPECT_FALSE((bridging::supportsToJs<std::string, jsi::Object>));
679686
EXPECT_FALSE((bridging::supportsToJs<std::vector<int>, jsi::Function>));
687+
688+
// Ensure we can convert HighResTimeStamp and HighResDuration to
689+
// DOMHighResTimeStamp (double).
690+
EXPECT_TRUE((bridging::supportsToJs<HighResTimeStamp, double>));
691+
EXPECT_TRUE((bridging::supportsToJs<HighResDuration, double>));
680692
}
681693

682694
TEST_F(BridgingTest, dynamicTest) {
@@ -765,4 +777,22 @@ TEST_F(BridgingTest, dynamicTest) {
765777
EXPECT_TRUE(undefinedFromJsResult.isNull());
766778
}
767779

780+
TEST_F(BridgingTest, highResTimeStampTest) {
781+
HighResTimeStamp timestamp = HighResTimeStamp::now();
782+
EXPECT_EQ(
783+
timestamp,
784+
bridging::fromJs<HighResTimeStamp>(
785+
rt, bridging::toJs(rt, timestamp), invoker));
786+
787+
HighResDuration duration = HighResDuration::fromNanoseconds(1);
788+
EXPECT_EQ(
789+
duration,
790+
bridging::fromJs<HighResDuration>(
791+
rt, bridging::toJs(rt, duration), invoker));
792+
793+
EXPECT_EQ(1.0, bridging::toJs(rt, HighResDuration::fromNanoseconds(1e6)));
794+
EXPECT_EQ(
795+
1.000001, bridging::toJs(rt, HighResDuration::fromNanoseconds(1e6 + 1)));
796+
}
797+
768798
} // namespace facebook::react

0 commit comments

Comments
 (0)