Skip to content

Commit e73a55e

Browse files
vmorozNickGerleman
authored andcommitted
Implemented PropertyBag for ReactContext and ReactInstanceSettings (#4817)
* Implemented PropertyBag for ReactContext and ReactInstanceSettings * Change files * yarn format * Delete the old PropertyBag files. * Make PropertyBag's ToObject and FromObject methods private. * Renamed some classes and methods. * Fixed sample project compilation * VS updates the sln file * VS automatically updated solutions
1 parent 283b06c commit e73a55e

40 files changed

+1322
-1021
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Implemented PropertyBag for ReactContext and ReactInstanceSettings",
4+
"packageName": "react-native-windows",
5+
"email": "vmorozov@microsoft.com",
6+
"dependentChangeType": "patch",
7+
"date": "2020-05-07T03:33:40.086Z"
8+
}

packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "App.h"
77
#include "ReactPackageProvider.h"
8+
#include "ReactPropertyBag.h"
89
#include "winrt/SampleLibraryCS.h"
910
#include "winrt/SampleLibraryCpp.h"
1011

@@ -34,6 +35,9 @@ App::App() noexcept {
3435
InstanceSettings().EnableDeveloperMenu(false);
3536
#endif
3637

38+
ReactPropertyBag::Set(InstanceSettings().Properties(), ReactPropertyId<int>{L"Prop1"}, 42);
39+
ReactPropertyBag::Set(InstanceSettings().Properties(), ReactPropertyId<hstring>{L"Prop2"}, L"Hello World!");
40+
3741
PackageProviders().Append(make<ReactPackageProvider>()); // Includes all modules in this project
3842
PackageProviders().Append(winrt::SampleLibraryCpp::ReactPackageProvider());
3943
PackageProviders().Append(winrt::SampleLibraryCS::ReactPackageProvider());

packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public App()
3636
InstanceSettings.EnableDeveloperMenu = false;
3737
#endif
3838

39+
InstanceSettings.Properties.Set(ReactPropertyBagHelper.GetName(null, "Prop1"), 43);
40+
InstanceSettings.Properties.Set(ReactPropertyBagHelper.GetName(null, "Prop2"), "Hello RNW!");
41+
3942
PackageProviders.Add(new Microsoft.ReactNative.Managed.ReactPackageProvider()); // Includes any modules in this project
4043
PackageProviders.Add(new SampleLibraryCS.ReactPackageProvider());
4144
PackageProviders.Add(new SampleLibraryCpp.ReactPackageProvider());

packages/microsoft-reactnative-sampleapps/windows/SampleApps.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Global
5151
GlobalSection(SharedMSBuildProjectFiles) = preSolution
5252
..\..\..\vnext\Microsoft.ReactNative.SharedManaged\Microsoft.ReactNative.SharedManaged.projitems*{09f4e6c1-2d12-4059-aa96-0b190861fd6a}*SharedItemsImports = 4
5353
..\..\..\vnext\JSI\Shared\JSI.Shared.vcxitems*{0cc28589-39e4-4288-b162-97b959f8b843}*SharedItemsImports = 9
54+
..\..\..\vnext\ReactWindowsCore\ReactWindowsCore.vcxitems*{11c084a3-a57c-4296-a679-cac17b603144}*SharedItemsImports = 4
5455
..\..\..\vnext\Shared\Shared.vcxitems*{2049dbe9-8d13-42c9-ae4b-413ae38fffd0}*SharedItemsImports = 9
5556
..\..\..\vnext\Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems*{47eec7f3-40d3-49ba-82c1-eaf103b54215}*SharedItemsImports = 4
5657
..\..\..\vnext\Microsoft.ReactNative.SharedManaged\Microsoft.ReactNative.SharedManaged.projitems*{67a1076f-7790-4203-86ea-4402ccb5e782}*SharedItemsImports = 13

packages/microsoft-reactnative-sampleapps/windows/SampleLibraryCPP/SampleModuleCPP.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ struct SampleModuleCppImpl {
3333
#pragma region Initialization
3434

3535
REACT_INIT(Initialize)
36-
void Initialize(ReactContext const & /*reactContext*/) noexcept {
36+
void Initialize(ReactContext const &reactContext) noexcept {
37+
const ReactPropertyId<int> myProp1{L"Prop1"};
38+
const ReactPropertyId<winrt::hstring> myProp2{L"Prop2"};
39+
DEBUG_OUTPUT("globalProps.Prop1:", *reactContext.Properties().Get(myProp1));
40+
DEBUG_OUTPUT("instanceProps.Prop2:", winrt::to_string(*reactContext.Properties().Get(myProp2)));
41+
3742
m_timer = winrt::Windows::System::Threading::ThreadPoolTimer::CreatePeriodicTimer(
3843
[this](const winrt::Windows::System::Threading::ThreadPoolTimer) noexcept {
3944
TimedEvent(++m_timerCount);

packages/microsoft-reactnative-sampleapps/windows/SampleLibraryCS/SampleModuleCS.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ internal sealed class SampleModuleCS
2525
#region Initializer
2626

2727
[ReactInitializer]
28-
public void Initialize(ReactContext _)
28+
public void Initialize(ReactContext reactContext)
2929
{
30+
Debug.WriteLine($"C# globalProps.Prop1: {reactContext.Handle.Properties.Get(ReactPropertyBagHelper.GetName(null, "Prop1"))}");
31+
Debug.WriteLine($"C# instanceProps.Prop2: {reactContext.Handle.Properties.Get(ReactPropertyBagHelper.GetName(null, "Prop2"))}");
32+
3033
_timer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler((timer) =>
3134
{
3235
TimedEvent?.Invoke(++_timerCount);

vnext/Microsoft.ReactNative.Cxx.UnitTests/Microsoft.ReactNative.Cxx.UnitTests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
<Midl Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\IReactModuleBuilder.idl" />
145145
<Midl Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\IReactPackageBuilder.idl" />
146146
<Midl Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\IViewManager.idl" />
147+
<Midl Include="..\Microsoft.ReactNative\IReactPropertyBag.idl" />
147148
<Midl Include="..\Microsoft.ReactNative\NoExceptionAttribute.idl" />
148149
</ItemGroup>
149150
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

vnext/Microsoft.ReactNative.Cxx.UnitTests/NativeModuleTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ TEST_CLASS (NativeModuleTest) {
599599
auto provider = React::MakeModuleProvider<SimpleNativeModule>();
600600
m_moduleObject = m_builderMock.CreateModule(provider, m_moduleBuilder);
601601
auto reactModule = m_moduleObject.as<React::IBoxedValue>();
602-
m_module = &React::BoxedValue<SimpleNativeModule>::GetImpl(reactModule);
602+
m_module = &React::BoxedValue<SimpleNativeModule>::GetValueUnsafe(reactModule);
603603
}
604604

605605
TEST_METHOD(TestMethodCall_Add) {

vnext/Microsoft.ReactNative.Cxx.UnitTests/NoAttributeNativeModuleTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ TEST_CLASS (NoAttributeNativeModuleTest) {
604604
auto provider = React::MakeModuleProvider<SimpleNativeModule2>();
605605
m_moduleObject = m_builderMock.CreateModule(provider, m_moduleBuilder);
606606
auto reactModule = m_moduleObject.as<React::IBoxedValue>();
607-
m_module = &React::BoxedValue<SimpleNativeModule2>::GetImpl(reactModule);
607+
m_module = &React::BoxedValue<SimpleNativeModule2>::GetValueUnsafe(reactModule);
608608
}
609609

610610
TEST_METHOD(TestMethodCall_Add) {

vnext/Microsoft.ReactNative.Cxx.UnitTests/ReactModuleBuilderMock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ struct ReactModuleBuilderMock {
121121
struct ReactContextMock : implements<ReactContextMock, IReactContext> {
122122
ReactContextMock(ReactModuleBuilderMock *builderMock) noexcept;
123123

124+
IReactPropertyBag Properties() noexcept {
125+
VerifyElseCrashSz(false, "Not implemented");
126+
}
127+
124128
void DispatchEvent(
125129
FrameworkElement const & /*view*/,
126130
hstring const & /*eventName*/,

vnext/Microsoft.ReactNative.Cxx.UnitTests/TurboModuleTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ TEST_CLASS (TurboModuleTest) {
11181118
auto provider = winrt::Microsoft::ReactNative::MakeTurboModuleProvider<MyTurboModule, MyTurboModuleSpec>();
11191119
m_moduleObject = m_builderMock.CreateModule(provider, m_moduleBuilder);
11201120
auto reactModule = m_moduleObject.as<winrt::Microsoft::ReactNative::IBoxedValue>();
1121-
m_module = &winrt::Microsoft::ReactNative::BoxedValue<MyTurboModule>::GetImpl(reactModule);
1121+
m_module = &winrt::Microsoft::ReactNative::BoxedValue<MyTurboModule>::GetValueUnsafe(reactModule);
11221122
}
11231123

11241124
TEST_METHOD(TestMethodCall_Add) {

vnext/Microsoft.ReactNative.Cxx.UnitTests/pch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <unknwn.h>
1010

11+
#undef GetCurrentTime
12+
1113
#include <winrt/Windows.Foundation.Collections.h>
1214
#include <winrt/Windows.Foundation.h>
1315
#include "winrt/Microsoft.ReactNative.h"

vnext/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ClInclude Include="$(MSBuildThisFileDirectory)JSValueXaml.h" />
2424
<ClInclude Include="$(MSBuildThisFileDirectory)ModuleRegistration.h" />
2525
<ClInclude Include="$(MSBuildThisFileDirectory)NativeModules.h" />
26+
<ClInclude Include="$(MSBuildThisFileDirectory)ReactPropertyBag.h" />
2627
<ClInclude Include="$(MSBuildThisFileDirectory)ReactContext.h" />
2728
<ClInclude Include="$(MSBuildThisFileDirectory)ReactError.h" />
2829
<ClInclude Include="$(MSBuildThisFileDirectory)ReactPromise.h" />

vnext/Microsoft.ReactNative.Cxx/NativeModules.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,28 +1107,26 @@ struct TurboModuleSpec {
11071107

11081108
template <class T>
11091109
struct BoxedValue : implements<BoxedValue<T>, IBoxedValue> {
1110-
BoxedValue() noexcept {}
1110+
template <class... TArgs>
1111+
BoxedValue(TArgs &&... args) noexcept : m_value(std::forward<TArgs>(args)...) {}
11111112

11121113
int64_t GetPtr() noexcept {
11131114
return reinterpret_cast<int64_t>(&m_value);
11141115
}
11151116

1116-
static T &GetImpl(IBoxedValue &module) noexcept;
1117+
static T &GetValueUnsafe(IBoxedValue const &boxedValue) noexcept {
1118+
return *reinterpret_cast<T *>(boxedValue.GetPtr());
1119+
}
11171120

11181121
private:
11191122
T m_value{};
11201123
};
11211124

1122-
template <class T>
1123-
inline T &BoxedValue<T>::GetImpl(IBoxedValue &module) noexcept {
1124-
return *reinterpret_cast<T *>(module.GetPtr());
1125-
}
1126-
11271125
template <class TModule>
11281126
inline ReactModuleProvider MakeModuleProvider() noexcept {
11291127
return [](IReactModuleBuilder const &moduleBuilder) noexcept {
11301128
auto moduleObject = make<BoxedValue<TModule>>();
1131-
auto module = &BoxedValue<TModule>::GetImpl(moduleObject);
1129+
auto module = &BoxedValue<TModule>::GetValueUnsafe(moduleObject);
11321130
ReactModuleBuilder builder{module, moduleBuilder};
11331131
GetReactModuleInfo(module, builder);
11341132
builder.CompleteRegistration();
@@ -1141,7 +1139,7 @@ inline ReactModuleProvider MakeTurboModuleProvider() noexcept {
11411139
TModuleSpec::template ValidateModule<TModule>();
11421140
return [](IReactModuleBuilder const &moduleBuilder) noexcept {
11431141
auto moduleObject = make<BoxedValue<TModule>>();
1144-
auto module = &BoxedValue<TModule>::GetImpl(moduleObject);
1142+
auto module = &BoxedValue<TModule>::GetValueUnsafe(moduleObject);
11451143
ReactModuleBuilder builder{module, moduleBuilder};
11461144
GetReactModuleInfo(module, builder);
11471145
builder.CompleteRegistration();

vnext/Microsoft.ReactNative.Cxx/ReactContext.h

Lines changed: 31 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,106 +7,71 @@
77

88
#include <string_view>
99
#include "JSValueWriter.h"
10-
#include "winrt/Microsoft.ReactNative.h"
10+
#include "ReactPropertyBag.h"
1111

1212
namespace winrt::Microsoft::ReactNative {
1313

1414
// Represents a context of execution for the Native Module.
1515
// It wraps up the IReactContext and adds convenience methods for
1616
// working with C++ types.
1717
struct ReactContext {
18-
ReactContext(IReactContext const &context) noexcept;
18+
ReactContext(IReactContext const &handle) noexcept : m_handle{handle} {}
1919

20-
IReactContext const &ContextAbi() const noexcept;
20+
IReactContext const &Handle() const noexcept {
21+
return m_handle;
22+
}
2123

22-
explicit operator bool() noexcept;
24+
explicit operator bool() const noexcept {
25+
return static_cast<bool>(m_handle);
26+
}
27+
28+
ReactPropertyBag Properties() const noexcept {
29+
return ReactPropertyBag{m_handle.Properties()};
30+
}
2331

2432
template <class... TArgs>
25-
void CallJSFunction(std::wstring_view moduleName, std::wstring_view methodName, TArgs &&... args) noexcept;
33+
void CallJSFunction(std::wstring_view moduleName, std::wstring_view methodName, TArgs &&... args) const noexcept {
34+
m_handle.CallJSFunction(moduleName, methodName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
35+
}
2636

2737
void CallJSFunction(
2838
std::wstring_view moduleName,
2939
std::wstring_view methodName,
30-
JSValueArgWriter const &paramsArgWriter) noexcept;
40+
JSValueArgWriter const &paramsArgWriter) const noexcept {
41+
m_handle.CallJSFunction(moduleName, methodName, paramsArgWriter);
42+
}
3143

3244
template <class... TArgs>
33-
void EmitJSEvent(std::wstring_view eventEmitterName, std::wstring_view eventName, TArgs &&... args) noexcept;
45+
void EmitJSEvent(std::wstring_view eventEmitterName, std::wstring_view eventName, TArgs &&... args) const noexcept {
46+
m_handle.EmitJSEvent(eventEmitterName, eventName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
47+
}
3448

3549
void EmitJSEvent(
3650
std::wstring_view eventEmitterName,
3751
std::wstring_view eventName,
38-
JSValueArgWriter const &paramsArgWriter) noexcept;
52+
JSValueArgWriter const &paramsArgWriter) const noexcept {
53+
m_handle.EmitJSEvent(eventEmitterName, eventName, paramsArgWriter);
54+
}
3955

4056
template <class... TArgs>
4157
void DispatchEvent(
4258
winrt::Windows::UI::Xaml::FrameworkElement const &view,
4359
std::wstring_view eventName,
44-
TArgs &&... args) noexcept;
60+
TArgs &&... args) const noexcept {
61+
m_handle.DispatchEvent(view, eventName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
62+
}
4563

4664
void DispatchEvent(
4765
winrt::Windows::UI::Xaml::FrameworkElement const &view,
4866
std::wstring_view eventName,
49-
JSValueArgWriter const &paramsArgWriter) noexcept;
67+
JSValueArgWriter const &paramsArgWriter) const noexcept {
68+
m_handle.DispatchEvent(view, eventName, paramsArgWriter);
69+
}
5070

5171
private:
52-
const IReactContext m_context;
72+
const IReactContext m_handle;
5373
};
5474

55-
//==============================================================================
56-
// ReactContext inline implementation
57-
//==============================================================================
58-
59-
inline ReactContext::ReactContext(IReactContext const &context) noexcept : m_context{context} {}
60-
61-
inline IReactContext const &ReactContext::ContextAbi() const noexcept {
62-
return m_context;
63-
}
64-
65-
inline ReactContext::operator bool() noexcept {
66-
return m_context != nullptr;
67-
}
68-
69-
template <class... TArgs>
70-
inline void
71-
ReactContext::CallJSFunction(std::wstring_view moduleName, std::wstring_view methodName, TArgs &&... args) noexcept {
72-
m_context.CallJSFunction(moduleName, methodName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
73-
}
74-
75-
inline void ReactContext::CallJSFunction(
76-
std::wstring_view moduleName,
77-
std::wstring_view methodName,
78-
JSValueArgWriter const &paramsArgWriter) noexcept {
79-
m_context.CallJSFunction(moduleName, methodName, paramsArgWriter);
80-
}
81-
82-
template <class... TArgs>
83-
inline void
84-
ReactContext::EmitJSEvent(std::wstring_view eventEmitterName, std::wstring_view eventName, TArgs &&... args) noexcept {
85-
m_context.EmitJSEvent(eventEmitterName, eventName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
86-
}
87-
88-
inline void ReactContext::EmitJSEvent(
89-
std::wstring_view eventEmitterName,
90-
std::wstring_view eventName,
91-
JSValueArgWriter const &paramsArgWriter) noexcept {
92-
m_context.EmitJSEvent(eventEmitterName, eventName, paramsArgWriter);
93-
}
94-
95-
template <class... TArgs>
96-
inline void ReactContext::DispatchEvent(
97-
winrt::Windows::UI::Xaml::FrameworkElement const &view,
98-
std::wstring_view eventName,
99-
TArgs &&... args) noexcept {
100-
m_context.DispatchEvent(view, eventName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
101-
}
102-
103-
inline void ReactContext::DispatchEvent(
104-
winrt::Windows::UI::Xaml::FrameworkElement const &view,
105-
std::wstring_view eventName,
106-
JSValueArgWriter const &paramsArgWriter) noexcept {
107-
m_context.DispatchEvent(view, eventName, paramsArgWriter);
108-
}
109-
11075
} // namespace winrt::Microsoft::ReactNative
11176

11277
#endif // MICROSOFT_REACTNATIVE_REACTCONTEXT

0 commit comments

Comments
 (0)