From b4f83e05aea0e1171b9222fc5a1076ba1a9f5c98 Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Mon, 11 May 2020 12:58:38 -0700 Subject: [PATCH 1/3] Use spec file for DevSettings NativeModule --- .../Modules/DevSettingsModule.cpp | 68 +++++++++++-------- .../Modules/DevSettingsModule.h | 36 +++++----- .../ReactHost/ReactInstanceWin.cpp | 25 ++++--- 3 files changed, 72 insertions(+), 57 deletions(-) diff --git a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp index 6d4312211c7..e437ce7e526 100644 --- a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp +++ b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp @@ -6,53 +6,65 @@ namespace Microsoft::ReactNative { -auto DevSettingsModule::getConstants() -> std::map { - return {}; +void DevSettings::Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept { + m_context = reactContext.Handle(); } -const char *DevSettingsModule::name = "DevSettings"; -std::string DevSettingsModule::getName() { - return name; +struct ReloadFunctor + : winrt::implements> { + ReloadFunctor(Mso::VoidFunctor &&func) : m_func(std::move(func)) {} + + void operator()() { + m_func(); + } + + private: + Mso::VoidFunctor m_func; +}; + +/*static*/ void DevSettings::SetReload(Mso::React::ReactOptions const &options, Mso::VoidFunctor &&func) noexcept { + options.Properties.Set(ReloadProperty(), winrt::make(std::move(func))); } -DevSettingsModule::DevSettingsModule(Mso::VoidFunctor &&reload) : m_reload(std::move(reload)) {} +/*static*/ winrt::Microsoft::ReactNative::IReactPropertyName DevSettings::ReloadProperty() noexcept { + return winrt::Microsoft::ReactNative::ReactPropertyBagHelper::GetName( + winrt::Microsoft::ReactNative::ReactPropertyBagHelper::GetNamespace(L"DevSettings"), L"Reload"); +} -void DevSettingsModule::reload() { - m_reload(); +void DevSettings::reload() noexcept { + (*winrt::get_self(m_context.Properties().Get(ReloadProperty())))(); } -void DevSettingsModule::setHotLoadingEnabled(bool /*isHotLoadingEnabled*/) { - assert(false); + +void DevSettings::reloadWithReason(std::string /*reason*/) noexcept { + reload(); } -void DevSettingsModule::setIsDebuggingRemotely(bool /*isDebuggingRemotelyEnabled*/) { + +void DevSettings::onFastRefresh() noexcept { + // noop +} + +void DevSettings::setHotLoadingEnabled(bool isHotLoadingEnabled) noexcept { assert(false); } -void DevSettingsModule::setLiveReloadEnabled(bool /*setLiveReloadEnabled*/) { + +void DevSettings::setIsDebuggingRemotely(bool isDebuggingRemotelyEnabled) noexcept { assert(false); } -void DevSettingsModule::setProfilingEnabled(bool /*setProfilingEnabled*/) { + +void DevSettings::setProfilingEnabled(bool isProfilingEnabled) noexcept { assert(false); } -void DevSettingsModule::toggleElementInspector() { + +void DevSettings::toggleElementInspector() noexcept { assert(false); } -// iOS only. -void DevSettingsModule::setIsShakeToShowDevMenuEnabled(bool /*enabled*/) { +void DevSettings::addMenuItem(std::string title) noexcept { assert(false); } -auto DevSettingsModule::getMethods() -> std::vector { - return { - Method("reload", [this](folly::dynamic args) { reload(); }), - Method("setHotLoadingEnabled", [this](folly::dynamic args) { setHotLoadingEnabled(args[0].getBool()); }), - Method("setIsDebuggingRemotely", [this](folly::dynamic args) { setIsDebuggingRemotely(args[0].getBool()); }), - Method("setLiveReloadEnabled", [this](folly::dynamic args) { setLiveReloadEnabled(args[0].getBool()); }), - Method("setProfilingEnabled", [this](folly::dynamic args) { setProfilingEnabled(args[0].getBool()); }), - Method("toggleElementInspector", [this](folly::dynamic args) { toggleElementInspector(); }), - Method( - "setIsShakeToShowDevMenuEnabled", - [this](folly::dynamic args) { setIsShakeToShowDevMenuEnabled(args[0].getBool()); }), - }; +void DevSettings::setIsShakeToShowDevMenuEnabled(bool enabled) noexcept { + assert(false); } } // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h index 531aec8024a..c39f788d637 100644 --- a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h +++ b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h @@ -5,30 +5,30 @@ #include #include -namespace Microsoft::ReactNative { - -class DevSettingsModule : public facebook::xplat::module::CxxModule { - public: - DevSettingsModule(Mso::VoidFunctor &&reload); +#include +#include - static const char *name; +namespace Microsoft::ReactNative { - std::string getName() override; - std::map getConstants() override; - std::vector getMethods() override; +REACT_MODULE(DevSettings) +struct DevSettings { + REACT_INIT(Initialize) void Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept; - void reload(); - void setHotLoadingEnabled(bool isHotLoadingEnabled); - void setIsDebuggingRemotely(bool isDebuggingRemotelyEnabled); - void setLiveReloadEnabled(bool setLiveReloadEnabled); - void setProfilingEnabled(bool setProfilingEnabled); - void toggleElementInspector(); + REACT_METHOD(reload) void reload() noexcept; + REACT_METHOD(reloadWithReason) void reloadWithReason(std::string reason) noexcept; + REACT_METHOD(onFastRefresh) void onFastRefresh() noexcept; + REACT_METHOD(setHotLoadingEnabled) void setHotLoadingEnabled(bool isHotLoadingEnabled) noexcept; + REACT_METHOD(setIsDebuggingRemotely) void setIsDebuggingRemotely(bool isDebuggingRemotelyEnabled) noexcept; + REACT_METHOD(setProfilingEnabled) void setProfilingEnabled(bool isProfilingEnabled) noexcept; + REACT_METHOD(toggleElementInspector) void toggleElementInspector() noexcept; + REACT_METHOD(addMenuItem) void addMenuItem(std::string title) noexcept; + REACT_METHOD(setIsShakeToShowDevMenuEnabled) void setIsShakeToShowDevMenuEnabled(bool enabled) noexcept; - // iOS only. - void setIsShakeToShowDevMenuEnabled(bool enabled); + static void SetReload(Mso::React::ReactOptions const &options, Mso::VoidFunctor &&func) noexcept; private: - Mso::VoidFunctor m_reload; + static winrt::Microsoft::ReactNative::IReactPropertyName ReloadProperty() noexcept; + winrt::Microsoft::ReactNative::IReactContext m_context; }; } // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp b/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp index 957aca066f2..b1e995a93c5 100644 --- a/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +++ b/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp @@ -14,6 +14,7 @@ #include "Microsoft.ReactNative/Threading/MessageQueueThreadFactory.h" #include "../../codegen/NativeClipboardSpec.g.h" +#include "../../codegen/NativeDevSettingsSpec.g.h" #include "NativeModules.h" #include "NativeModulesProvider.h" #include "Unicode.h" @@ -218,21 +219,23 @@ void ReactInstanceWin::Initialize() noexcept { ::Microsoft::ReactNative::Clipboard, ::Microsoft::ReactNativeSpecs::ClipboardSpec>()); + ::Microsoft::ReactNative::DevSettings::SetReload( + strongThis->Options(), [weakReactHost = m_weakReactHost]() noexcept { + if (auto reactHost = weakReactHost.GetStrongPtr()) { + reactHost->ReloadInstance(); + } + }); + + nmp->AddModuleProvider( + L"DevSettings", + winrt::Microsoft::ReactNative::MakeTurboModuleProvider< + ::Microsoft::ReactNative::DevSettings, + ::Microsoft::ReactNativeSpecs::DevSettingsSpec>()); + auto modules = nmp->GetModules(m_reactContext, m_batchingUIThread); cxxModules.insert( cxxModules.end(), std::make_move_iterator(modules.begin()), std::make_move_iterator(modules.end())); - cxxModules.emplace_back( - Microsoft::ReactNative::DevSettingsModule::name, - [weakReactHost = strongThis->m_weakReactHost]() { - return std::make_unique([weakReactHost]() noexcept { - if (auto reactHost = weakReactHost.GetStrongPtr()) { - reactHost->ReloadInstance(); - } - }); - }, - m_batchingUIThread); - if (m_options.ModuleProvider != nullptr) { std::vector customCxxModules = m_options.ModuleProvider->GetModules(m_reactContext, m_batchingUIThread); From 02b7435387919da9c02cc1de06e66c54e02db31e Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Mon, 11 May 2020 13:26:08 -0700 Subject: [PATCH 2/3] Change files --- ...ative-windows-2020-05-11-13-26-08-devsettingsspec.json | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 change/react-native-windows-2020-05-11-13-26-08-devsettingsspec.json diff --git a/change/react-native-windows-2020-05-11-13-26-08-devsettingsspec.json b/change/react-native-windows-2020-05-11-13-26-08-devsettingsspec.json new file mode 100644 index 00000000000..bf2f07e8b92 --- /dev/null +++ b/change/react-native-windows-2020-05-11-13-26-08-devsettingsspec.json @@ -0,0 +1,8 @@ +{ + "type": "prerelease", + "comment": "Use spec file for DevSettings NativeModule", + "packageName": "react-native-windows", + "email": "acoates@microsoft.com", + "dependentChangeType": "patch", + "date": "2020-05-11T20:26:08.523Z" +} From 5822159c5b3541e727b72d685ec6ee83380a9076 Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Mon, 11 May 2020 21:23:53 -0700 Subject: [PATCH 3/3] minor change --- vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp | 5 +++-- vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp index e437ce7e526..90a86c23900 100644 --- a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp +++ b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp @@ -7,7 +7,7 @@ namespace Microsoft::ReactNative { void DevSettings::Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept { - m_context = reactContext.Handle(); + m_context = reactContext; } struct ReloadFunctor @@ -32,7 +32,8 @@ struct ReloadFunctor } void DevSettings::reload() noexcept { - (*winrt::get_self(m_context.Properties().Get(ReloadProperty())))(); + (*winrt::get_self(m_context.Properties().Get( + winrt::Microsoft::ReactNative::ReactPropertyId(ReloadProperty()))))(); } void DevSettings::reloadWithReason(std::string /*reason*/) noexcept { diff --git a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h index c39f788d637..aeddf6a7c3d 100644 --- a/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h +++ b/vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h @@ -28,7 +28,7 @@ struct DevSettings { private: static winrt::Microsoft::ReactNative::IReactPropertyName ReloadProperty() noexcept; - winrt::Microsoft::ReactNative::IReactContext m_context; + winrt::Microsoft::ReactNative::ReactContext m_context; }; } // namespace Microsoft::ReactNative