From 6f6ae7477d04456a86c624ca2de6b174a3b77ade Mon Sep 17 00:00:00 2001 From: Miguel Marques Date: Tue, 31 Oct 2023 19:34:30 +0000 Subject: [PATCH] more changes to hot reload --- .../FileDependenciesProvider.cs | 4 +-- ReactViewControl/IChildViewHost.cs | 2 -- ReactViewControl/ReactView.cs | 2 +- ReactViewControl/ReactViewFactory.cs | 5 ---- ReactViewControl/ReactViewRender.cs | 27 +++++++------------ ReactViewControl/ViewModuleContainer.cs | 10 +++---- Sample.Avalonia/ExtendedReactView.cs | 9 ++++--- Sample.Avalonia/ExtendedReactViewFactory.cs | 6 ++--- .../WebpackDevDependenciesProvider.cs | 5 +--- 9 files changed, 26 insertions(+), 44 deletions(-) diff --git a/ReactViewControl.Avalonia/FileDependenciesProvider.cs b/ReactViewControl.Avalonia/FileDependenciesProvider.cs index ad546507..85fa69d7 100644 --- a/ReactViewControl.Avalonia/FileDependenciesProvider.cs +++ b/ReactViewControl.Avalonia/FileDependenciesProvider.cs @@ -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 DependencyJsSourcesCache { get; } private Lazy CssSourcesCache { get; } diff --git a/ReactViewControl/IChildViewHost.cs b/ReactViewControl/IChildViewHost.cs index 793127fe..8f10eccb 100644 --- a/ReactViewControl/IChildViewHost.cs +++ b/ReactViewControl/IChildViewHost.cs @@ -8,7 +8,5 @@ public interface IChildViewHost { bool AddChildView(IViewModule childView, string frameName); ReactView Host { get; } - - bool IsHotReloadEnabled { get; } } } diff --git a/ReactViewControl/ReactView.cs b/ReactViewControl/ReactView.cs index cedce918..4ccc1ee9 100644 --- a/ReactViewControl/ReactView.cs +++ b/ReactViewControl/ReactView.cs @@ -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(); } diff --git a/ReactViewControl/ReactViewFactory.cs b/ReactViewControl/ReactViewFactory.cs index defc2f92..a3bc43c2 100644 --- a/ReactViewControl/ReactViewFactory.cs +++ b/ReactViewControl/ReactViewFactory.cs @@ -31,11 +31,6 @@ public class ReactViewFactory { /// public virtual bool EnableViewPreload => true; - /// - /// Webpack dev server url. Setting this value will enable hot reload. eg: new Uri("http://localhost:8080") - /// - public virtual Uri DevServerURI => null; - /// /// Module dependencies provider. /// diff --git a/ReactViewControl/ReactViewRender.cs b/ReactViewControl/ReactViewRender.cs index a3198557..873b3202 100644 --- a/ReactViewControl/ReactViewRender.cs +++ b/ReactViewControl/ReactViewRender.cs @@ -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 initializePlugins, bool preloadWebView, bool enableDebugMode, Uri devServerUri = null, IModuleDependenciesProvider moduleDependenciesProvider = null) { + public ReactViewRender(ResourceUrl defaultStyleSheet, Func initializePlugins, bool preloadWebView, bool enableDebugMode, IModuleDependenciesProvider moduleDependenciesProvider = null) { UserCallingAssembly = GetUserCallingMethod().ReflectedType.Assembly; // must useSharedDomain for the local storage to be shared @@ -53,8 +53,10 @@ public ReactViewRender(ResourceUrl defaultStyleSheet, Func initia DefaultStyleSheet = defaultStyleSheet; PluginsFactory = initializePlugins; EnableDebugMode = enableDebugMode; - DevServerUri = devServerUri; - ModuleDependenciesProvider = moduleDependenciesProvider; + + if (moduleDependenciesProvider != null) { + ModuleDependenciesProvider = moduleDependenciesProvider; + } GetOrCreateFrame(FrameInfo.MainViewFrameName); // creates the main frame @@ -91,11 +93,6 @@ public ReactViewRender(ResourceUrl defaultStyleSheet, Func initia public ReactView Host { get; set; } - /// - /// True when hot reload is enabled. - /// - public bool IsHotReloadEnabled => DevServerUri != null; - public bool IsDisposing => WebView.IsDisposing; /// @@ -126,14 +123,8 @@ public bool EnableDebugMode { } } - /// - /// Gets webpack dev server url. - /// - public Uri DevServerUri { get; } - public IModuleDependenciesProvider ModuleDependenciesProvider { get; } - /// /// Gets or sets the webview zoom percentage (1 = 100%) /// @@ -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(); } diff --git a/ReactViewControl/ViewModuleContainer.cs b/ReactViewControl/ViewModuleContainer.cs index 019a41ca..81ed267b 100644 --- a/ReactViewControl/ViewModuleContainer.cs +++ b/ReactViewControl/ViewModuleContainer.cs @@ -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; diff --git a/Sample.Avalonia/ExtendedReactView.cs b/Sample.Avalonia/ExtendedReactView.cs index 62c082b5..f50f6dc8 100644 --- a/Sample.Avalonia/ExtendedReactView.cs +++ b/Sample.Avalonia/ExtendedReactView.cs @@ -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() { @@ -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()); } } diff --git a/Sample.Avalonia/ExtendedReactViewFactory.cs b/Sample.Avalonia/ExtendedReactViewFactory.cs index 836c43af..41663edd 100644 --- a/Sample.Avalonia/ExtendedReactViewFactory.cs +++ b/Sample.Avalonia/ExtendedReactViewFactory.cs @@ -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[]{ @@ -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 diff --git a/Sample.Avalonia/WebpackDevDependenciesProvider.cs b/Sample.Avalonia/WebpackDevDependenciesProvider.cs index 7da81ffc..1a132cbf 100644 --- a/Sample.Avalonia/WebpackDevDependenciesProvider.cs +++ b/Sample.Avalonia/WebpackDevDependenciesProvider.cs @@ -1,5 +1,4 @@ using System.Net.Http; -using System.Reflection; using System.Text.Json; using ReactViewControl; @@ -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 { @@ -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();