Skip to content

Commit

Permalink
Apply changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasia Orishchenko committed Jul 16, 2020
1 parent 15ab648 commit a6b522d
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 362 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
xcuserdata/
yarn-error.log*
.vs/
msbuild.binlog
29 changes: 14 additions & 15 deletions example/react-native.config.js
@@ -1,18 +1,17 @@
if (process.argv.includes("--config=metro.config.macos.js")) {
module.exports = {
reactNativePath: "node_modules/react-native-macos",
};
}
else if (process.argv.includes("--config=metro.config.windows.js")) {
module.exports = {
reactNativePath: "node_modules/react-native-windows",
};
module.exports = {
reactNativePath: "node_modules/react-native-macos",
};
} else if (process.argv.includes("--config=metro.config.windows.js")) {
module.exports = {
reactNativePath: "node_modules/react-native-windows",
};
} else {
module.exports = {
project: {
ios: {
project: "ios/ReactTestApp-Dummy.xcodeproj",
},
},
};
module.exports = {
project: {
ios: {
project: "ios/ReactTestApp-Dummy.xcodeproj",
},
},
};
}
Binary file removed msbuild.binlog
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -43,7 +43,7 @@
"react": "~16.8.6 || ~16.9.0 || ~16.11.0 || ~16.13.1",
"react-native": "^0.60.6 || ^0.61.5 || ^0.62.2 || ^0.63 || 1000.0.0",
"react-native-macos": "^0.60 || ^0.61.39",
"react-native-windows": "^0.62.1"
"react-native-windows": "^0.62.2"
},
"devDependencies": {
"eslint": "^7.4.0",
Expand All @@ -52,7 +52,7 @@
"prettier": "2.0.5",
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-windows": "0.62.1",
"react-native-windows": "0.62.2",
"semantic-release": "^17.0.0"
},
"release": {
Expand Down
4 changes: 2 additions & 2 deletions windows/ReactTestApp/App.cpp
Expand Up @@ -11,8 +11,8 @@ using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;
using namespace ReactTestApp;
using namespace ReactTestApp::implementation;
using namespace winrt::ReactTestApp;
using namespace winrt::ReactTestApp::implementation;

/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
Expand Down
77 changes: 13 additions & 64 deletions windows/ReactTestApp/MainPage.cpp
Expand Up @@ -5,22 +5,15 @@
#include "ComponentViewModel.h"
#include "MainPage.g.cpp"
#include "Manifest.h"
#include "filesystem"

using namespace winrt::Microsoft::ReactNative;
using namespace winrt::Windows::UI::Xaml::Controls;

namespace winrt::ReactTestApp::implementation
{
MainPage::MainPage()
{
InitializeComponent();
SetComponents();
InitReact();
}

void MainPage::SetComponents()
{
auto menuItems = MenuFlyout().Items();
std::optional<::ReactTestApp::Manifest> manifest = ::ReactTestApp::GetManifest();
if (!manifest.has_value()) {
Expand All @@ -29,65 +22,41 @@ namespace winrt::ReactTestApp::implementation
newMenuItem.IsEnabled(false);
menuItems.Append(newMenuItem);
} else {
for (auto &&c : manifest.value().components) {
auto &components = manifest.value().components;
for (auto &&c : components) {
hstring componentDisplayName = to_hstring(c.displayName.value_or(c.appKey));
hstring componentName = to_hstring(c.appKey);
hstring appKey = to_hstring(c.appKey);
ReactTestApp::ComponentViewModel newComponent =
winrt::make<ComponentViewModel>(componentName, componentDisplayName);
m_components.push_back(newComponent);
winrt::make<ComponentViewModel>(appKey, componentDisplayName);

MenuFlyoutItem newMenuItem;
newMenuItem.CommandParameter(newComponent);
newMenuItem.Text(newComponent.DisplayName());
newMenuItem.Click({this, &MainPage::SetReactComponentName});
menuItems.Append(newMenuItem);
}
}
}

void MainPage::InitReact()
{
// TODO fallback to JS bundle
LoadJSBundleFrom(JSBundleSource::Embedded);

// If only one component is present load it automatically
if (m_components.size() == 1) {
ReactRootView().ComponentName(m_components.at(0).AppKey());
}

ReactRootView().ReactNativeHost(m_reactNativeHost);
}

void MainPage::LoadJSBundleFrom(JSBundleSource source)
{
m_reactNativeHost.InstanceSettings().UseLiveReload(source == JSBundleSource::DevServer);
m_reactNativeHost.InstanceSettings().UseWebDebugger(source == JSBundleSource::DevServer);
m_reactNativeHost.InstanceSettings().UseFastRefresh(source == JSBundleSource::DevServer);
// If only one component is present load it automatically
if (components.size() == 1) {
ReactRootView().ComponentName(to_hstring(components.at(0).appKey));
}
// TODO fallback to JS bundle
reactInstance_.LoadJSBundleFrom(::ReactTestApp::JSBundleSource::Embedded);

switch (source) {
case JSBundleSource::DevServer:
m_reactNativeHost.InstanceSettings().JavaScriptMainModuleName(L"index");
m_reactNativeHost.InstanceSettings().JavaScriptBundleFile(L"");
break;
case JSBundleSource::Embedded:
hstring bundleFileName = to_hstring(GetBundleName());
m_reactNativeHost.InstanceSettings().JavaScriptBundleFile(bundleFileName);
break;
ReactRootView().ReactNativeHost(reactInstance_.ReactHost());
}

m_reactNativeHost.ReloadInstance();
}

void MainPage::LoadFromJSBundle(Windows::Foundation::IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs)
{
LoadJSBundleFrom(JSBundleSource::Embedded);
reactInstance_.LoadJSBundleFrom(::ReactTestApp::JSBundleSource::Embedded);
}

void MainPage::LoadFromDevServer(Windows::Foundation::IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs)
{
LoadJSBundleFrom(JSBundleSource::DevServer);
reactInstance_.LoadJSBundleFrom(::ReactTestApp::JSBundleSource::DevServer);
}

void MainPage::SetReactComponentName(Windows::Foundation::IInspectable const &sender,
Expand All @@ -96,24 +65,4 @@ namespace winrt::ReactTestApp::implementation
auto s = sender.as<MenuFlyoutItem>().CommandParameter();
ReactRootView().ComponentName(s.as<ComponentViewModel>()->AppKey());
}

std::string MainPage::GetBundleName()
{
std::vector entryFileNames = {"index.windows",
"main.windows",
"index.native",
"main.native",
"index"
"main"};

for (std::string &&n : entryFileNames) {
std::string path = "Bundle\\" + n + ".bundle";
if (std::filesystem::exists(path)) {
return n;
}
}

return ""; //TODO handle bundle not present
}

} // namespace winrt::ReactTestApp::implementation
14 changes: 2 additions & 12 deletions windows/ReactTestApp/MainPage.h
@@ -1,33 +1,23 @@
#pragma once

#include "MainPage.g.h"
#include "ReactInstance.h"

namespace winrt::ReactTestApp::implementation
{
enum class JSBundleSource {
DevServer,
Embedded,
};

struct MainPage : MainPageT<MainPage> {
public:
MainPage();

void LoadFromJSBundle(Windows::Foundation::IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs);
void LoadFromDevServer(Windows::Foundation::IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs);

private:
winrt::Microsoft::ReactNative::ReactNativeHost m_reactNativeHost;
std::vector<ReactTestApp::ComponentViewModel> m_components;
::ReactTestApp::ReactInstance reactInstance_;

void LoadJSBundleFrom(JSBundleSource source);
void SetComponents();
void InitReact();
void SetReactComponentName(Windows::Foundation::IInspectable const &,
Windows::UI::Xaml::RoutedEventArgs e);
std::string GetBundleName();
};
} // namespace winrt::ReactTestApp::implementation

Expand Down
53 changes: 53 additions & 0 deletions windows/ReactTestApp/ReactInstance.cpp
@@ -0,0 +1,53 @@
#include "pch.h"

#include "ReactInstance.h"

#include <filesystem>

namespace ReactTestApp
{
void ReactInstance::LoadJSBundleFrom(JSBundleSource source)
{
auto instanceSettings = reactNativeHost_.InstanceSettings();
instanceSettings.UseLiveReload(source == JSBundleSource::DevServer);
instanceSettings.UseWebDebugger(source == JSBundleSource::DevServer);
instanceSettings.UseFastRefresh(source == JSBundleSource::DevServer);

switch (source) {
case JSBundleSource::DevServer:
instanceSettings.JavaScriptMainModuleName(L"index");
instanceSettings.JavaScriptBundleFile(L"");
break;
case JSBundleSource::Embedded:
winrt::hstring bundleFileName = winrt::to_hstring(GetBundleName());
instanceSettings.JavaScriptBundleFile(bundleFileName);
break;
}

reactNativeHost_.ReloadInstance();
}

winrt::Microsoft::ReactNative::ReactNativeHost &ReactInstance::ReactHost()
{
return reactNativeHost_;
}

std::string GetBundleName()
{
std::vector entryFileNames = {"index.windows",
"main.windows",
"index.native",
"main.native",
"index"
"main"};

for (std::string &&n : entryFileNames) {
std::string path = "Bundle\\" + n + ".bundle";
if (std::filesystem::exists(path)) {
return n;
}
}

return ""; // TODO handle bundle not present
}
} // namespace ReactTestApp
23 changes: 23 additions & 0 deletions windows/ReactTestApp/ReactInstance.h
@@ -0,0 +1,23 @@
#pragma once
#include <string>

namespace ReactTestApp
{
enum class JSBundleSource {
DevServer,
Embedded,
};

class ReactInstance
{
public:
winrt::Microsoft::ReactNative::ReactNativeHost &ReactHost();
void LoadJSBundleFrom(JSBundleSource source);

private:
winrt::Microsoft::ReactNative::ReactNativeHost reactNativeHost_;
};

std::string GetBundleName();

} // namespace ReactTestApp

0 comments on commit a6b522d

Please sign in to comment.