Skip to content

Commit

Permalink
refactor(ReactInstanceManager): Support awaitable ReactContext
Browse files Browse the repository at this point in the history
Currently, the ReactInstanceManager kicks off the process to create the ReactContext in the background and its not awaitable. This ensures a single asynchronous thread is available from start to finish for ReactContext creation.

Fixes #1448
  • Loading branch information
rozele committed Oct 19, 2017
1 parent 9a1144b commit af1b893
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 350 deletions.
5 changes: 3 additions & 2 deletions ReactWindows/ReactNative.Net46/ReactPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ReactNative.Modules.Core;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -181,7 +182,7 @@ protected virtual ReactRootView CreateRootView()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnAcceleratorKeyActivated(object sender, KeyEventArgs e)
private async void OnAcceleratorKeyActivated(object sender, KeyEventArgs e)
{
if (ReactInstanceManager.DevSupportManager.IsEnabled)
{
Expand All @@ -196,7 +197,7 @@ private void OnAcceleratorKeyActivated(object sender, KeyEventArgs e)
// Ctrl+R
if (isCtrlKeyDown && e.Key == Key.R)
{
ReactInstanceManager.DevSupportManager.HandleReloadJavaScript();
await ReactInstanceManager.DevSupportManager.CreateReactContextFromPackagerAsync(CancellationToken.None);
}
}

Expand Down
16 changes: 9 additions & 7 deletions ReactWindows/ReactNative.Shared/Bridge/JavaScriptBundleLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
#if WINDOWS_UWP
using Windows.Storage;
Expand Down Expand Up @@ -27,8 +28,9 @@ public abstract class JavaScriptBundleLoader
/// Initializes the JavaScript bundle loader, typically making an
/// asynchronous call to cache the bundle in memory.
/// </summary>
/// <param name="token">A token to cancel the initialization.</param>
/// <returns>A task to await initialization.</returns>
public abstract Task InitializeAsync();
public abstract Task InitializeAsync(CancellationToken token);

/// <summary>
/// Loads the bundle into a JavaScript executor.
Expand Down Expand Up @@ -89,13 +91,13 @@ public override string SourceUrl
}

#if WINDOWS_UWP
public override async Task InitializeAsync()
public override async Task InitializeAsync(CancellationToken token)
{
var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(SourceUrl)).AsTask().ConfigureAwait(false);
var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(SourceUrl)).AsTask(token).ConfigureAwait(false);
_script = storageFile.Path;
}
#else
public override Task InitializeAsync()
public override Task InitializeAsync(CancellationToken token)
{
var assembly = Assembly.GetAssembly(typeof(JavaScriptBundleLoader));
var assemblyName = assembly.GetName();
Expand Down Expand Up @@ -134,11 +136,11 @@ public CachedJavaScriptBundleLoader(string sourceUrl, string cachedFileLocation)

public override string SourceUrl { get; }

public override async Task InitializeAsync()
public override async Task InitializeAsync(CancellationToken token)
{
#if WINDOWS_UWP
var localFolder = ApplicationData.Current.LocalFolder;
var storageFile = await localFolder.GetFileAsync(_cachedFileLocation).AsTask().ConfigureAwait(false);
var storageFile = await localFolder.GetFileAsync(_cachedFileLocation).AsTask(token).ConfigureAwait(false);
#else
var localFolder = FileSystem.Current.LocalStorage;
var storageFile = await localFolder.GetFileAsync(_cachedFileLocation).ConfigureAwait(false);
Expand Down Expand Up @@ -170,7 +172,7 @@ public override string SourceUrl
get;
}

public override Task InitializeAsync()
public override Task InitializeAsync(CancellationToken token)
{
return Task.CompletedTask;
}
Expand Down
5 changes: 3 additions & 2 deletions ReactWindows/ReactNative.Shared/Bridge/ReactInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using static System.FormattableString;

Expand Down Expand Up @@ -81,9 +82,9 @@ public void Initialize()
QueueConfiguration.NativeModulesQueue.Dispatch(_registry.NotifyReactInstanceInitialize);
}

public async Task InitializeBridgeAsync()
public async Task InitializeBridgeAsync(CancellationToken token)
{
await _bundleLoader.InitializeAsync().ConfigureAwait(false);
await _bundleLoader.InitializeAsync(token).ConfigureAwait(false);

using (Tracer.Trace(Tracer.TRACE_TAG_REACT_BRIDGE, "initializeBridge").Start())
{
Expand Down
7 changes: 4 additions & 3 deletions ReactWindows/ReactNative.Shared/DevSupport/DevServerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ReactNative.Bridge;
using ReactNative.Bridge;
using System;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -139,13 +139,14 @@ public async Task DownloadBundleFromUrlAsync(string jsModulePath, Stream outputS
/// <summary>
/// Checks if the packager is running.
/// </summary>
/// <param name="token">A token to cancel the request.</param>
/// <returns>A task to await the packager status.</returns>
public async Task<bool> IsPackagerRunningAsync()
public async Task<bool> IsPackagerRunningAsync(CancellationToken token)
{
var statusUrl = CreatePackagerStatusUrl(DebugServerHost);
try
{
using (var response = await _client.GetAsync(statusUrl).ConfigureAwait(false))
using (var response = await _client.GetAsync(statusUrl, token).ConfigureAwait(false))
{
if (!response.IsSuccessStatusCode)
{
Expand Down
Loading

0 comments on commit af1b893

Please sign in to comment.