public static void InvokeTaskCallback(string id, string result) { var reference = TrackedReference.Get(id); var function = reference.TrackedInstance as Action <string>; function(result); }
public static void Track(string id, object reference) { var trackedRef = new TrackedReference(id, reference); if (References.ContainsKey(id)) { throw new InvalidOperationException($"An element with id '{id}' is already being tracked."); } References.Add(id, trackedRef); }
public static Task <TResult> InvokeJavaScriptFunctionAsync <TResult>(string identifier, params object[] args) { var tcs = new TaskCompletionSource <TResult>(); var argsJson = args.Select(JsonUtil.Serialize); var successId = Guid.NewGuid().ToString(); var failureId = Guid.NewGuid().ToString(); var asyncProtocol = JsonUtil.Serialize(new { Success = successId, Failure = failureId, Function = new MethodOptions { Type = new TypeInstance { Assembly = typeof(JavaScriptInvoke).Assembly.FullName, TypeName = typeof(JavaScriptInvoke).FullName }, Method = new MethodInstance { Name = nameof(JavaScriptInvoke.InvokeTaskCallback), ParameterTypes = new[] { new TypeInstance { Assembly = typeof(string).Assembly.FullName, TypeName = typeof(string).FullName }, new TypeInstance { Assembly = typeof(string).Assembly.FullName, TypeName = typeof(string).FullName } } } } }); TrackedReference.Track(successId, new Action <string>(r => { var res = JsonUtil.Deserialize <TResult>(r); tcs.SetResult(res); })); TrackedReference.Track(failureId, (new Action <string>(r => { tcs.SetException(new InvalidOperationException(r)); }))); var resultJson = RegisteredFunction.InvokeUnmarshalled <string>( "invokeWithJsonMarshallingAsync", argsJson.Prepend(asyncProtocol).Prepend(identifier).ToArray()); return(tcs.Task); }