public static async Task Initialize(ChakraJavaScriptExecutor executor, IMessageQueueThread jsQueueThread) { var scriptUris = new[] { @"ms-appx:///Resources/test.js", }; var scripts = new KeyValuePair <string, string> [scriptUris.Length]; for (var i = 0; i < scriptUris.Length; ++i) { var uri = scriptUris[i]; var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri)); scripts[i] = new KeyValuePair <string, string>(uri, storageFile.Path); } await jsQueueThread.CallOnQueue(() => { foreach (var script in scripts) { executor.RunScript(script.Value, script.Key); } return(true); }); }
/// <summary> /// Asserts <see cref="IMessageQueueThread.IsOnThread"/>, throwing if the <b>false</b>. /// </summary> /// <param name="actionQueue">The message queue thread.</param> /// <exception cref="InvalidOperationException"> /// Thrown if the assertion fails. /// </exception> public static void AssertOnThread(this IMessageQueueThread actionQueue) { if (!actionQueue.IsOnThread()) { throw new InvalidOperationException("Thread access assertion failed."); } }
public static async Task Initialize(ChakraJavaScriptExecutor executor, IMessageQueueThread jsQueueThread) { var scriptUris = new[] { new Uri(@"ms-appx:///Resources/test.js"), }; var scripts = new string[scriptUris.Length]; for (var i = 0; i < scriptUris.Length; ++i) { var uri = scriptUris[i]; var storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri); using (var stream = await storageFile.OpenStreamForReadAsync()) using (var reader = new StreamReader(stream)) { scripts[i] = reader.ReadToEnd(); } } await jsQueueThread.CallOnQueue(() => { foreach (var script in scripts) { executor.RunScript(script); } return(true); }); }
/// <summary> /// Calls a function on a message queue and returns a task to await the response. /// </summary> /// <typeparam name="T">Type of response.</typeparam> /// <param name="actionQueue">The message queue thread.</param> /// <param name="func">The function.</param> /// <returns>A task to await the result.</returns> public static Task <T> CallOnQueue <T>(this IMessageQueueThread actionQueue, Func <T> func) { var taskCompletionSource = new TaskCompletionSource <T>(); actionQueue.RunOnQueue(() => { var result = func(); // TaskCompletionSource<T>.SetResult can call continuations // on the awaiter of the task completion source. Task.Run(() => taskCompletionSource.SetResult(result)); }); return(taskCompletionSource.Task); }
/// <summary> /// Instantiates the <see cref="IReactBridge"/>. /// </summary> /// <param name="executor">The JavaScript executor.</param> /// <param name="reactCallback">The native callback handler.</param> /// <param name="nativeModulesQueueThread"> /// The native modules queue thread. /// </param> public ReactBridge( IJavaScriptExecutor executor, IReactCallback reactCallback, IMessageQueueThread nativeModulesQueueThread) { if (executor == null) throw new ArgumentNullException(nameof(executor)); if (reactCallback == null) throw new ArgumentNullException(nameof(reactCallback)); if (nativeModulesQueueThread == null) throw new ArgumentNullException(nameof(nativeModulesQueueThread)); _jsExecutor = executor; _reactCallback = reactCallback; _nativeModulesQueueThread = nativeModulesQueueThread; }
public static async Task Initialize(ChakraJavaScriptExecutor executor, IMessageQueueThread jsQueueThread) { var scriptUris = new[] { @"Resources/test.js", }; var scripts = new KeyValuePair <string, string> [scriptUris.Length]; for (var i = 0; i < scriptUris.Length; ++i) { var uri = scriptUris[i]; #if WINDOWS_UWP var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx://" + "/" + uri)); var filePath = storageFile.Path; #else var assembly = Assembly.GetAssembly(typeof(JavaScriptHelpers)); var assemblyName = assembly.GetName(); var pathToAssembly = Path.GetDirectoryName(assemblyName.CodeBase); if (pathToAssembly == null) { throw new FileNotFoundException($"Could not get directory name for code base of '{assemblyName}'."); } var pathToAssemblyResource = Path.Combine(pathToAssembly, uri); var u = new Uri(pathToAssemblyResource); var filePath = u.LocalPath; #endif scripts[i] = new KeyValuePair <string, string>(uri, filePath); } await jsQueueThread.CallOnQueue(() => { foreach (var script in scripts) { executor.RunScript(script.Value, script.Key); } return(true); }); }
/// <summary> /// Instantiates the <see cref="IReactBridge"/>. /// </summary> /// <param name="executor">The JavaScript executor.</param> /// <param name="reactCallback">The native callback handler.</param> /// <param name="nativeModulesQueueThread"> /// The native modules queue thread. /// </param> public ReactBridge( IJavaScriptExecutor executor, IReactCallback reactCallback, IMessageQueueThread nativeModulesQueueThread) { if (executor == null) { throw new ArgumentNullException(nameof(executor)); } if (reactCallback == null) { throw new ArgumentNullException(nameof(reactCallback)); } if (nativeModulesQueueThread == null) { throw new ArgumentNullException(nameof(nativeModulesQueueThread)); } _jsExecutor = executor; _reactCallback = reactCallback; _nativeModulesQueueThread = nativeModulesQueueThread; }
public static async Task Initialize(JSCoreJavaScriptExecutor executor, IMessageQueueThread jsQueueThread) { var scriptUris = new[] { @"Resources/test.js", }; var scripts = new KeyValuePair <string, string> [scriptUris.Length]; for (var i = 0; i < scriptUris.Length; ++i) { var uri = scriptUris[i]; #if WINDOWS_UWP var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx://" + "/" + uri)); var filePath = storageFile.Path; #else var pathToAssemblyResource = Application.Current.DirectoryInfo.Resource; var u = new Uri(pathToAssemblyResource); var filePath = u.LocalPath; #endif scripts[i] = new KeyValuePair <string, string>(uri, filePath); } await jsQueueThread.CallOnQueue(() => { foreach (var script in scripts) { executor.RunScript(script.Value, script.Key); } return(true); }); }