Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created August 29, 2016 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/40b5ff89e966ad7c6118d3657f5b71f8 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/40b5ff89e966ad7c6118d3657f5b71f8 to your computer and use it in GitHub Desktop.
private void RunSerializedScript(string script, string sourceUrl)
{
var localFolder = ApplicationData.Current.LocalFolder;
var binFile = "ReactNativeSerializedBundle.bin";
var binPath = Path.Combine(localFolder.Path, binFile);
try
{
if(!EnsureSerializedScriptAsync(script, binFile).Result)
{
Native.ThrowIfError((JavaScriptErrorCode)_executor.SerializeScriptFromFile(script, binPath));
}
var result = _executor.RunSerializedScriptFromFile(binPath, script, sourceUrl);
Native.ThrowIfError((JavaScriptErrorCode)result.ErrorCode);
}
catch (JavaScriptScriptException ex)
{
var jsonError = JavaScriptValueToJTokenConverter.Convert(ex.Error);
var message = jsonError.Value<string>("message");
var stackTrace = jsonError.Value<string>("stack");
throw new Modules.Core.JavaScriptException(message ?? ex.Message, stackTrace, ex);
}
}
private static async Task<bool> EnsureSerializedScriptAsync(string scriptFile, string binFile)
{
var localFolder = ApplicationData.Current.LocalFolder;
var scriptItem = await StorageFile.GetFileFromPathAsync(scriptFile);
var scriptItemProps = await scriptItem.GetBasicPropertiesAsync();
var item = await localFolder.TryGetItemAsync(binFile);
if (item != null)
{
var props = await item.GetBasicPropertiesAsync();
return props.DateModified > scriptItemProps.DateModified;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment