Skip to content

Commit 495dbad

Browse files
authored
Implement the SampleTurboModule module (#13541)
## Description This PR provides a proper implementation of the `SampleTurboModule` module and removes the proxy code in `TurboModuleManager` which instead substituted the old `SampleTurboCxxModule` module. ### Type of Change - New feature (non-breaking change which adds functionality) ### Why The APIs of `SampleTurboModule` are starting to deviate from the older `SampleTurboCxxModule`, specifically the addition of new `EventEmitter` members. So it's time we had a "real" implementation of `SampleTurboModule`. Closes #13531 ### What See above. ## Screenshots N/A ## Testing Verified tests still pass and the new module is being called. ## Changelog Should this change be included in the release notes: _yes_ Implement the SampleTurboModule module
1 parent e49762e commit 495dbad

File tree

7 files changed

+196
-3
lines changed

7 files changed

+196
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Implement SampleTurboModule",
4+
"packageName": "react-native-windows",
5+
"email": "jthysell@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#include "pch.h"
5+
#include "SampleTurboModule.h"
6+
7+
namespace Microsoft::ReactNative {
8+
9+
void SampleTurboModule::Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept {
10+
m_reactContext = reactContext;
11+
}
12+
13+
ReactNativeSpecs::SampleTurboModuleSpec_Constants SampleTurboModule::GetConstants() noexcept {
14+
ReactNativeSpecs::SampleTurboModuleSpec_Constants constants;
15+
constants.const1 = true;
16+
constants.const2 = 375;
17+
constants.const3 = "something";
18+
return constants;
19+
}
20+
21+
void SampleTurboModule::voidFunc() noexcept {}
22+
23+
bool SampleTurboModule::getBool(bool arg) noexcept {
24+
return arg;
25+
}
26+
27+
double SampleTurboModule::getEnum(double arg) noexcept {
28+
return arg;
29+
}
30+
31+
double SampleTurboModule::getNumber(double arg) noexcept {
32+
return arg;
33+
}
34+
35+
std::string SampleTurboModule::getString(std::string arg) noexcept {
36+
return std::string(arg);
37+
}
38+
39+
::React::JSValueArray SampleTurboModule::getArray(::React::JSValueArray &&arg) noexcept {
40+
return arg.Copy();
41+
}
42+
43+
::React::JSValue SampleTurboModule::getObject(::React::JSValue &&arg) noexcept {
44+
assert(arg.Type() == ::React::JSValueType::Object);
45+
return arg.Copy();
46+
}
47+
48+
::React::JSValue SampleTurboModule::getUnsafeObject(::React::JSValue &&arg) noexcept {
49+
assert(arg.Type() == ::React::JSValueType::Object);
50+
return arg.Copy();
51+
}
52+
53+
double SampleTurboModule::getRootTag(double arg) noexcept {
54+
// TODO: Proper impl
55+
return arg;
56+
}
57+
58+
::React::JSValue SampleTurboModule::getValue(double x, std::string y, ::React::JSValue &&z) noexcept {
59+
return ::React::JSValueObject{
60+
{"x", x},
61+
{"y", y},
62+
{"z", z.Copy()},
63+
};
64+
}
65+
66+
void SampleTurboModule::getValueWithCallback(std::function<void(std::string)> const &callback) noexcept {
67+
callback("value from callback!");
68+
}
69+
70+
void SampleTurboModule::getValueWithPromise(bool error, ::React::ReactPromise<std::string> &&result) noexcept {
71+
if (error) {
72+
result.Reject("intentional promise rejection");
73+
} else {
74+
result.Resolve("result!");
75+
}
76+
}
77+
78+
void SampleTurboModule::voidFuncThrows() noexcept {
79+
// TODO: Proper impl
80+
}
81+
82+
::React::JSValue SampleTurboModule::getObjectThrows(::React::JSValue &&arg) noexcept {
83+
// TODO: Proper impl
84+
return nullptr;
85+
}
86+
87+
void SampleTurboModule::promiseThrows(::React::ReactPromise<void> &&result) noexcept {
88+
// TODO: Proper impl
89+
}
90+
91+
void SampleTurboModule::voidFuncAssert() noexcept {
92+
// TODO: Proper impl
93+
}
94+
95+
::React::JSValue SampleTurboModule::getObjectAssert(::React::JSValue &&arg) noexcept {
96+
// TODO: Proper impl
97+
return nullptr;
98+
}
99+
100+
void SampleTurboModule::promiseAssert(::React::ReactPromise<void> &&result) noexcept {
101+
// TODO: Proper impl
102+
}
103+
104+
} // namespace Microsoft::ReactNative
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#pragma once
4+
5+
#include "codegen/NativeSampleTurboModuleSpec.g.h"
6+
#include <NativeModules.h>
7+
8+
namespace Microsoft::ReactNative {
9+
10+
REACT_MODULE(SampleTurboModule)
11+
struct SampleTurboModule {
12+
using ModuleSpec = ReactNativeSpecs::SampleTurboModuleSpec;
13+
14+
REACT_INIT(Initialize)
15+
void Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept;
16+
17+
REACT_GET_CONSTANTS(GetConstants)
18+
ReactNativeSpecs::SampleTurboModuleSpec_Constants GetConstants() noexcept;
19+
20+
REACT_METHOD(voidFunc)
21+
void voidFunc() noexcept;
22+
23+
REACT_SYNC_METHOD(getBool)
24+
bool getBool(bool arg) noexcept;
25+
26+
REACT_SYNC_METHOD(getEnum)
27+
double getEnum(double arg) noexcept;
28+
29+
REACT_SYNC_METHOD(getNumber)
30+
double getNumber(double arg) noexcept;
31+
32+
REACT_SYNC_METHOD(getString)
33+
std::string getString(std::string arg) noexcept;
34+
35+
REACT_SYNC_METHOD(getArray)
36+
::React::JSValueArray getArray(::React::JSValueArray &&arg) noexcept;
37+
38+
REACT_SYNC_METHOD(getObject)
39+
::React::JSValue getObject(::React::JSValue &&arg) noexcept;
40+
41+
REACT_SYNC_METHOD(getUnsafeObject)
42+
::React::JSValue getUnsafeObject(::React::JSValue &&arg) noexcept;
43+
44+
REACT_SYNC_METHOD(getRootTag)
45+
double getRootTag(double arg) noexcept;
46+
47+
REACT_SYNC_METHOD(getValue)
48+
::React::JSValue getValue(double x, std::string y, ::React::JSValue &&z) noexcept;
49+
50+
REACT_METHOD(getValueWithCallback)
51+
void getValueWithCallback(std::function<void(std::string)> const &callback) noexcept;
52+
53+
REACT_METHOD(getValueWithPromise)
54+
void getValueWithPromise(bool error, ::React::ReactPromise<std::string> &&result) noexcept;
55+
56+
REACT_METHOD(voidFuncThrows)
57+
void voidFuncThrows() noexcept;
58+
59+
REACT_SYNC_METHOD(getObjectThrows)
60+
::React::JSValue getObjectThrows(::React::JSValue &&arg) noexcept;
61+
62+
REACT_METHOD(promiseThrows)
63+
void promiseThrows(::React::ReactPromise<void> &&result) noexcept;
64+
65+
REACT_METHOD(voidFuncAssert)
66+
void voidFuncAssert() noexcept;
67+
68+
REACT_SYNC_METHOD(getObjectAssert)
69+
::React::JSValue getObjectAssert(::React::JSValue &&arg) noexcept;
70+
71+
REACT_METHOD(promiseAssert)
72+
void promiseAssert(::React::ReactPromise<void> &&result) noexcept;
73+
74+
private:
75+
winrt::Microsoft::ReactNative::ReactContext m_reactContext;
76+
};
77+
78+
} // namespace Microsoft::ReactNative

vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "Modules/ExceptionsManager.h"
4040
#include "Modules/PlatformConstantsWinModule.h"
4141
#include "Modules/ReactRootViewTagGenerator.h"
42+
#include "Modules/SampleTurboModule.h"
4243
#include "Modules/SourceCode.h"
4344
#include "Modules/StatusBarManager.h"
4445
#include "Modules/Timing.h"
@@ -411,6 +412,10 @@ void ReactInstanceWin::LoadModules(
411412
L"PlatformConstants",
412413
winrt::Microsoft::ReactNative::MakeTurboModuleProvider<::Microsoft::ReactNative::PlatformConstants>());
413414

415+
registerTurboModule(
416+
L"SampleTurboModule",
417+
winrt::Microsoft::ReactNative::MakeTurboModuleProvider<::Microsoft::ReactNative::SampleTurboModule>());
418+
414419
uint32_t hermesBytecodeVersion = 0;
415420
#if defined(USE_HERMES) && defined(ENABLE_DEVSERVER_HBCBUNDLES)
416421
hermesBytecodeVersion = ::hermes::hbc::BYTECODE_VERSION;

vnext/Shared/Shared.vcxitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@
481481
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\PlatformConstantsWinModule.cpp" />
482482
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\ExceptionsManager.cpp" />
483483
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\Timing.cpp" />
484+
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\SampleTurboModule.cpp" />
484485
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\SourceCode.cpp" />
485486
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\NativeModulesProvider.cpp" />
486487
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\ReactHost\AsyncActionQueue.cpp" />

vnext/Shared/Shared.vcxitems.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
<ClCompile Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\Fabric\AbiShadowNode.cpp" />
305305
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\PlatformConstantsWinModule.cpp" />
306306
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\ExceptionsManager.cpp" />
307+
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\SampleTurboModule.cpp" />
307308
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\SourceCode.cpp" />
308309
<ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\Timing.cpp" />
309310
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\featureflags\ReactNativeFeatureFlags.cpp" />

vnext/Shared/TurboModuleManager.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ std::shared_ptr<TurboModule> TurboModuleManager::getModule(const std::string &mo
3030
}
3131
}
3232

33-
if (moduleName.compare("SampleTurboModule") == 0) {
34-
return m_modules.emplace(moduleName, std::make_shared<SampleTurboCxxModule>(m_callInvoker)).first->second;
35-
}
3633
return nullptr;
3734
}
3835

0 commit comments

Comments
 (0)