Ejemplo n.º 1
0
        internal void EndInvokeJS(long asyncHandle, bool succeeded, JSAsyncCallResult asyncCallResult)
        {
            using (asyncCallResult?.JsonDocument)
            {
                if (!_pendingTasks.TryRemove(asyncHandle, out var tcs))
                {
                    throw new ArgumentException($"There is no pending task with handle '{asyncHandle}'.");
                }

                if (succeeded)
                {
                    var resultType = TaskGenericsUtil.GetTaskCompletionSourceResultType(tcs);
                    try
                    {
                        var result = asyncCallResult != null?
                                     JsonSerializer.Deserialize(asyncCallResult.JsonElement.GetRawText(), resultType, JsonSerializerOptionsProvider.Options) :
                                         null;

                        TaskGenericsUtil.SetTaskCompletionSourceResult(tcs, result);
                    }
                    catch (Exception exception)
                    {
                        var message = $"An exception occurred executing JS interop: {exception.Message}. See InnerException for more details.";
                        TaskGenericsUtil.SetTaskCompletionSourceException(tcs, new JSException(message, exception));
                    }
                }
                else
                {
                    var exceptionText = asyncCallResult?.JsonElement.ToString() ?? string.Empty;
                    TaskGenericsUtil.SetTaskCompletionSourceException(tcs, new JSException(exceptionText));
                }
            }
        }
Ejemplo n.º 2
0
        internal void EndInvokeJS(long asyncHandle, bool succeeded, JSAsyncCallResult asyncCallResult)
        {
            using (asyncCallResult?.JsonDocument)
            {
                if (!_pendingTasks.TryRemove(asyncHandle, out var tcs))
                {
                    throw new ArgumentException($"There is no pending task with handle '{asyncHandle}'.");
                }

                if (succeeded)
                {
                    var resultType = TaskGenericsUtil.GetTaskCompletionSourceResultType(tcs);
                    var result     = asyncCallResult != null?
                                     JsonSerializer.Parse(asyncCallResult.JsonElement.GetRawText(), resultType, JsonSerializerOptionsProvider.Options) :
                                         null;

                    TaskGenericsUtil.SetTaskCompletionSourceResult(tcs, result);
                }
                else
                {
                    var exceptionText = asyncCallResult?.JsonElement.ToString() ?? string.Empty;
                    TaskGenericsUtil.SetTaskCompletionSourceException(tcs, new JSException(exceptionText));
                }
            }
        }
Ejemplo n.º 3
0
        internal void EndInvokeJS(long taskId, bool succeeded, JSAsyncCallResult asyncCallResult)
        {
            using (asyncCallResult?.JsonDocument)
            {
                if (!_pendingTasks.TryRemove(taskId, out var tcs))
                {
                    // We should simply return if we can't find an id for the invocation.
                    // This likely means that the method that initiated the call defined a timeout and stopped waiting.
                    return;
                }

                CleanupTasksAndRegistrations(taskId);

                if (succeeded)
                {
                    var resultType = TaskGenericsUtil.GetTaskCompletionSourceResultType(tcs);
                    try
                    {
                        var result = asyncCallResult != null?
                                     JsonSerializer.Deserialize(asyncCallResult.JsonElement.GetRawText(), resultType, JsonSerializerOptionsProvider.Options) :
                                         null;

                        TaskGenericsUtil.SetTaskCompletionSourceResult(tcs, result);
                    }
                    catch (Exception exception)
                    {
                        var message = $"An exception occurred executing JS interop: {exception.Message}. See InnerException for more details.";
                        TaskGenericsUtil.SetTaskCompletionSourceException(tcs, new JSException(message, exception));
                    }
                }
                else
                {
                    var exceptionText = asyncCallResult?.JsonElement.ToString() ?? string.Empty;
                    TaskGenericsUtil.SetTaskCompletionSourceException(tcs, new JSException(exceptionText));
                }
            }
        }