Skip to content
Merged
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
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Use spec file for DevSettings NativeModule",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-05-11T20:26:08.523Z"
}
69 changes: 41 additions & 28 deletions vnext/Microsoft.ReactNative/Modules/DevSettingsModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,66 @@

namespace Microsoft::ReactNative {

auto DevSettingsModule::getConstants() -> std::map<std::string, folly::dynamic> {
return {};
void DevSettings::Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept {
m_context = reactContext;
}

const char *DevSettingsModule::name = "DevSettings";
std::string DevSettingsModule::getName() {
return name;
struct ReloadFunctor
: winrt::implements<ReloadFunctor, winrt::default_interface<winrt::Windows::Foundation::IInspectable>> {
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<ReloadFunctor>(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<ReloadFunctor>(m_context.Properties().Get(
winrt::Microsoft::ReactNative::ReactPropertyId<winrt::Windows::Foundation::IInspectable>(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<facebook::xplat::module::CxxModule::Method> {
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
36 changes: 18 additions & 18 deletions vnext/Microsoft.ReactNative/Modules/DevSettingsModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
#include <cxxreact/CxxModule.h>
#include <functional/functor.h>

namespace Microsoft::ReactNative {

class DevSettingsModule : public facebook::xplat::module::CxxModule {
public:
DevSettingsModule(Mso::VoidFunctor &&reload);
#include <NativeModules.h>
#include <ReactHost/React.h>

static const char *name;
namespace Microsoft::ReactNative {

std::string getName() override;
std::map<std::string, folly::dynamic> getConstants() override;
std::vector<Method> 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

winrt::Microsoft::ReactNative::IReactPropertyName [](start = 9, length = 49)

Since this method is private, consider using the ReactPropertyId instead to have a strongly typed access.

winrt::Microsoft::ReactNative::ReactContext m_context;
};

} // namespace Microsoft::ReactNative
25 changes: 14 additions & 11 deletions vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<Microsoft::ReactNative::DevSettingsModule>([weakReactHost]() noexcept {
if (auto reactHost = weakReactHost.GetStrongPtr()) {
reactHost->ReloadInstance();
}
});
},
m_batchingUIThread);

if (m_options.ModuleProvider != nullptr) {
std::vector<facebook::react::NativeModuleDescription> customCxxModules =
m_options.ModuleProvider->GetModules(m_reactContext, m_batchingUIThread);
Expand Down