2
2
using System . Collections . Concurrent ;
3
3
using System . Collections . Generic ;
4
4
using System . Diagnostics ;
5
+ using System . Linq ;
5
6
using System . Threading ;
6
7
using JavaScriptEngineSwitcher . Core ;
7
- using JavaScriptEngineSwitcher . Jint ;
8
- using JavaScriptEngineSwitcher . Msie ;
9
- using JavaScriptEngineSwitcher . Msie . Configuration ;
10
8
using React . Exceptions ;
11
9
12
10
namespace React
@@ -17,6 +15,11 @@ namespace React
17
15
public class JavaScriptEngineFactory : IDisposable , IJavaScriptEngineFactory
18
16
{
19
17
/// <summary>
18
+ /// List of all available JavaScript engines
19
+ /// </summary>
20
+ private static readonly IList < FactoryWithPriority > _availableFactories
21
+ = new List < FactoryWithPriority > ( ) ;
22
+ /// <summary>
20
23
/// Function used to create new JavaScript engine instances.
21
24
/// </summary>
22
25
private readonly Func < IJsEngine > _factory ;
@@ -34,6 +37,25 @@ public JavaScriptEngineFactory()
34
37
_factory = GetFactory ( ) ;
35
38
}
36
39
40
+ /// <summary>
41
+ /// Adds a supported JavaScript engine. When an instance of
42
+ /// <see cref="JavaScriptEngineFactory" /> is created, the first functioning JavaScript
43
+ /// engine with the lowest priority will be used.
44
+ /// </summary>
45
+ /// <param name="factory">Factory method to create new instance of the engine</param>
46
+ /// <param name="priority">
47
+ /// Any number. All engines will be sorted by priority, so "better" engines should have
48
+ /// a lower priority number.
49
+ /// </param>
50
+ public static void AddFactoryWithPriority ( Func < IJsEngine > factory , int priority )
51
+ {
52
+ _availableFactories . Add ( new FactoryWithPriority
53
+ {
54
+ Factory = factory ,
55
+ Priority = priority
56
+ } ) ;
57
+ }
58
+
37
59
/// <summary>
38
60
/// Gets the JavaScript engine for the current thread
39
61
/// </summary>
@@ -76,12 +98,9 @@ public void DisposeEngineForCurrentThread()
76
98
/// <returns>Function to create JavaScript engine</returns>
77
99
private static Func < IJsEngine > GetFactory ( )
78
100
{
79
- var availableEngineFactories = new List < Func < IJsEngine > >
80
- {
81
- ( ) => new MsieJsEngine ( new MsieConfiguration { EngineMode = JsEngineMode . ChakraActiveScript } ) ,
82
- ( ) => new MsieJsEngine ( new MsieConfiguration { EngineMode = JsEngineMode . Classic } ) ,
83
- ( ) => new JintJsEngine ( )
84
- } ;
101
+ var availableEngineFactories = _availableFactories
102
+ . OrderBy ( x => x . Priority )
103
+ . Select ( x => x . Factory ) ;
85
104
foreach ( var engineFactory in availableEngineFactories )
86
105
{
87
106
IJsEngine engine = null ;
@@ -126,5 +145,11 @@ public void Dispose()
126
145
}
127
146
}
128
147
}
148
+
149
+ private class FactoryWithPriority
150
+ {
151
+ public Func < IJsEngine > Factory { get ; set ; }
152
+ public int Priority { get ; set ; }
153
+ }
129
154
}
130
155
}
0 commit comments