forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptExtensions.cs
More file actions
102 lines (94 loc) · 4.41 KB
/
JavaScriptExtensions.cs
File metadata and controls
102 lines (94 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Threading.Tasks;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript.JavaScript
{
/// <summary>
/// Defines extension methods for use with JavaScript engines.
/// </summary>
public static class JavaScriptExtensions
{
private delegate void Executor(dynamic resolve, dynamic reject);
/// <summary>
/// Converts a <see cref="Task{TResult}"/> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code currently running on the calling thread.
/// </summary>
/// <typeparam name="TResult">The task's result type.</typeparam>
/// <param name="task">The task to convert to a promise.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise<TResult>(this Task<TResult> task)
{
return task.ToPromise(ScriptEngine.Current);
}
/// <summary>
/// Converts a <see cref="Task{TResult}"/> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code running in the specified script engine.
/// </summary>
/// <typeparam name="TResult">The task's result type.</typeparam>
/// <param name="task">The task to convert to a promise.</param>
/// <param name="engine">The script engine in which the promise will be used.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise<TResult>(this Task<TResult> task, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(task, "task");
MiscHelpers.VerifyNonNullArgument(engine, "engine");
var ctor = (ScriptObject)engine.Script.Promise;
return ctor.Invoke(true, new Executor((resolve, reject) =>
{
task.ContinueWith(thisTask =>
{
if (thisTask.IsCompleted)
{
resolve(thisTask.Result);
}
else
{
reject(thisTask.Exception);
}
}, TaskContinuationOptions.ExecuteSynchronously);
}));
}
/// <summary>
/// Converts a <see cref="Task"/> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code currently running on the calling thread.
/// </summary>
/// <param name="task">The task to convert to a promise.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise(this Task task)
{
return task.ToPromise(ScriptEngine.Current);
}
/// <summary>
/// Converts a <see cref="Task"/> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code running in the specified script engine.
/// </summary>
/// <param name="task">The task to convert to a promise.</param>
/// <param name="engine">The script engine in which the promise will be used.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise(this Task task, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(task, "task");
MiscHelpers.VerifyNonNullArgument(engine, "engine");
var ctor = (ScriptObject)engine.Script.Promise;
return ctor.Invoke(true, new Executor((resolve, reject) =>
{
task.ContinueWith(thisTask =>
{
if (thisTask.IsCompleted)
{
resolve();
}
else
{
reject(thisTask.Exception);
}
}, TaskContinuationOptions.ExecuteSynchronously);
}));
}
}
}