Skip to content

Commit

Permalink
more changes to hot reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mpm-os committed Oct 31, 2023
1 parent 998aaf4 commit 6f6ae74
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 44 deletions.
4 changes: 2 additions & 2 deletions ReactViewControl.Avalonia/FileDependenciesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public FileDependenciesProvider(string sourcePath) {
this.sourcePath = sourcePath;
}

public string[] GetCssDependencies(string filename) => DependencyJsSourcesCache.Value;
public string[] GetCssDependencies(string filename) => CssSourcesCache.Value;

public string[] GetJsDependencies(string filename) => CssSourcesCache.Value;
public string[] GetJsDependencies(string filename) => DependencyJsSourcesCache.Value;

private Lazy<string[]> DependencyJsSourcesCache { get; }
private Lazy<string[]> CssSourcesCache { get; }
Expand Down
2 changes: 0 additions & 2 deletions ReactViewControl/IChildViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ public interface IChildViewHost {
bool AddChildView(IViewModule childView, string frameName);

ReactView Host { get; }

bool IsHotReloadEnabled { get; }
}
}
2 changes: 1 addition & 1 deletion ReactViewControl/ReactView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract partial class ReactView : IDisposable {

private static ReactViewRender CreateReactViewInstance(ReactViewFactory factory) {
ReactViewRender InnerCreateView() {
var view = new ReactViewRender(factory.DefaultStyleSheet, () => factory.InitializePlugins(), factory.EnableViewPreload, factory.EnableDebugMode, factory.DevServerURI, factory.ModuleDependenciesProvider);
var view = new ReactViewRender(factory.DefaultStyleSheet, () => factory.InitializePlugins(), factory.EnableViewPreload, factory.EnableDebugMode, factory.ModuleDependenciesProvider);
if (factory.ShowDeveloperTools) {
view.ShowDeveloperTools();
}
Expand Down
5 changes: 0 additions & 5 deletions ReactViewControl/ReactViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public class ReactViewFactory {
/// </summary>
public virtual bool EnableViewPreload => true;

/// <summary>
/// Webpack dev server url. Setting this value will enable hot reload. eg: new Uri("http://localhost:8080")
/// </summary>
public virtual Uri DevServerURI => null;

/// <summary>
/// Module dependencies provider.
/// </summary>
Expand Down
27 changes: 9 additions & 18 deletions ReactViewControl/ReactViewRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal partial class ReactViewRender : IChildViewHost, IDisposable {
private ResourceUrl defaultStyleSheet;
private bool isInputDisabled; // used primarly to control the intention to disable input (before the browser is ready)

public ReactViewRender(ResourceUrl defaultStyleSheet, Func<IViewModule[]> initializePlugins, bool preloadWebView, bool enableDebugMode, Uri devServerUri = null, IModuleDependenciesProvider moduleDependenciesProvider = null) {
public ReactViewRender(ResourceUrl defaultStyleSheet, Func<IViewModule[]> initializePlugins, bool preloadWebView, bool enableDebugMode, IModuleDependenciesProvider moduleDependenciesProvider = null) {
UserCallingAssembly = GetUserCallingMethod().ReflectedType.Assembly;

// must useSharedDomain for the local storage to be shared
Expand All @@ -53,8 +53,10 @@ public ReactViewRender(ResourceUrl defaultStyleSheet, Func<IViewModule[]> initia
DefaultStyleSheet = defaultStyleSheet;
PluginsFactory = initializePlugins;
EnableDebugMode = enableDebugMode;
DevServerUri = devServerUri;
ModuleDependenciesProvider = moduleDependenciesProvider;

if (moduleDependenciesProvider != null) {
ModuleDependenciesProvider = moduleDependenciesProvider;
}

GetOrCreateFrame(FrameInfo.MainViewFrameName); // creates the main frame

Expand Down Expand Up @@ -91,11 +93,6 @@ public ReactViewRender(ResourceUrl defaultStyleSheet, Func<IViewModule[]> initia

public ReactView Host { get; set; }

/// <summary>
/// True when hot reload is enabled.
/// </summary>
public bool IsHotReloadEnabled => DevServerUri != null;

public bool IsDisposing => WebView.IsDisposing;

/// <summary>
Expand Down Expand Up @@ -126,14 +123,8 @@ public bool EnableDebugMode {
}
}

/// <summary>
/// Gets webpack dev server url.
/// </summary>
public Uri DevServerUri { get; }

public IModuleDependenciesProvider ModuleDependenciesProvider { get; }


/// <summary>
/// Gets or sets the webview zoom percentage (1 = 100%)
/// </summary>
Expand Down Expand Up @@ -563,11 +554,11 @@ private string ToFullUrl(string url) {
if (url.OrdinalContains(Uri.SchemeDelimiter)) {
return url;
} else if (url.OrdinalStartsWith(ResourceUrl.PathSeparator)) {
if (IsHotReloadEnabled) {
return new Uri(DevServerUri, url).ToString();
} else {
// if (IsHotReloadEnabled) {
// return new Uri(DevServerUri, url).ToString();
// } else {
return new ResourceUrl(ResourceUrl.EmbeddedScheme, url).ToString();
}
// }
} else {
return new ResourceUrl(UserCallingAssembly, url).ToString();
}
Expand Down
10 changes: 5 additions & 5 deletions ReactViewControl/ViewModuleContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
namespace ReactViewControl {

public abstract class ViewModuleContainer : IViewModule {

private const string JsEntryFileExtension = ".js.entry";
private const string CssEntryFileExtension = ".css.entry";

private IFrame frame;
private IChildViewHost childViewHost;
private IModuleDependenciesProvider dependenciesProvider;

public ViewModuleContainer() {
frame = new FrameInfo("dummy");
dependenciesProvider = new FileDependenciesProvider(MainJsSource);
}

public virtual IModuleDependenciesProvider DependenciesProvider {
get { return dependenciesProvider ??= new FileDependenciesProvider(MainJsSource); }
set { dependenciesProvider = value; }
set { if(value != null) {
dependenciesProvider = value;
}
}
}

protected virtual string MainJsSource => null;
Expand Down
9 changes: 6 additions & 3 deletions Sample.Avalonia/ExtendedReactView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ public abstract class ExtendedReactView : ReactView {

public ExtendedReactView(IViewModule mainModule) : base(mainModule) {
Settings.ThemeChanged += OnStylePreferenceChanged;
EmbeddedResourceRequested += OnEmbeddedResourceRequested;
mainModule.DependenciesProvider = Factory.ModuleDependenciesProvider;

if (Factory.ModuleDependenciesProvider != null) {
mainModule.DependenciesProvider = Factory.ModuleDependenciesProvider;
EmbeddedResourceRequested += OnEmbeddedResourceRequested;
}
}

protected override void InnerDispose() {
Expand All @@ -30,7 +33,7 @@ private void OnEmbeddedResourceRequested(WebViewControl.ResourceHandler resource
}

resourceUrl = new Uri(resourceUrl).PathAndQuery;
var devServerHost = new Uri(Factory.DevServerURI.GetLeftPart(UriPartial.Authority));
var devServerHost = new Uri("http://localhost:8080/"); //new Uri(Factory.DevServerURI.GetLeftPart(UriPartial.Authority));
resourceHandler.Redirect(new Uri(devServerHost, resourceUrl).ToString());
}
}
Expand Down
6 changes: 2 additions & 4 deletions Sample.Avalonia/ExtendedReactViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal class ExtendedReactViewFactory : ReactViewFactory {
public override IViewModule[] InitializePlugins() {
var viewPlugin = new ViewPlugin();
#if DEBUG
if (DevServerURI != null) {
viewPlugin.DependenciesProvider = ModuleDependenciesProvider;
if (provider != null) {
viewPlugin.DependenciesProvider = provider;
}
#endif
return new[]{
Expand All @@ -29,8 +29,6 @@ public override IViewModule[] InitializePlugins() {
#if DEBUG
public override bool EnableDebugMode => true;

public override Uri DevServerURI => new Uri("http://localhost:8080/Sample.Avalonia/");

public override IModuleDependenciesProvider ModuleDependenciesProvider =>
provider;
#endif
Expand Down
5 changes: 1 addition & 4 deletions Sample.Avalonia/WebpackDevDependenciesProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using ReactViewControl;

Expand All @@ -9,8 +8,6 @@ namespace Sample.Avalonia;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
//using Newtonsoft.Json;

public class WebPackDependenciesProvider : IModuleDependenciesProvider {

Expand Down Expand Up @@ -73,7 +70,7 @@ public void RefreshDependencies() {
if (shouldRefresh) {
using var httpClient = new HttpClient();
var assembly = typeof(Program).Assembly.GetName().Name;
// var json = httpClient.GetStringAsync(new Uri(uri, $"{assembly}/{ManifestPath}"));
// var json = httpClient.GetStringAsync(new Uri(uri, $"{assembly}/{ManifestPath}"));
var json = httpClient.GetStringAsync(new Uri(uri, ManifestPath));
json.Wait();

Expand Down

0 comments on commit 6f6ae74

Please sign in to comment.