22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
44using System . Diagnostics ;
5+ using System . Linq ;
56using System . Threading ;
67using JavaScriptEngineSwitcher . Core ;
7- using JavaScriptEngineSwitcher . Jint ;
8- using JavaScriptEngineSwitcher . Msie ;
9- using JavaScriptEngineSwitcher . Msie . Configuration ;
108using React . Exceptions ;
119
1210namespace React
@@ -17,6 +15,11 @@ namespace React
1715 public class JavaScriptEngineFactory : IDisposable , IJavaScriptEngineFactory
1816 {
1917 /// <summary>
18+ /// List of all available JavaScript engines
19+ /// </summary>
20+ private static readonly IList < FactoryWithPriority > _availableFactories
21+ = new List < FactoryWithPriority > ( ) ;
22+ /// <summary>
2023 /// Function used to create new JavaScript engine instances.
2124 /// </summary>
2225 private readonly Func < IJsEngine > _factory ;
@@ -34,6 +37,25 @@ public JavaScriptEngineFactory()
3437 _factory = GetFactory ( ) ;
3538 }
3639
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+
3759 /// <summary>
3860 /// Gets the JavaScript engine for the current thread
3961 /// </summary>
@@ -76,12 +98,9 @@ public void DisposeEngineForCurrentThread()
7698 /// <returns>Function to create JavaScript engine</returns>
7799 private static Func < IJsEngine > GetFactory ( )
78100 {
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 ) ;
85104 foreach ( var engineFactory in availableEngineFactories )
86105 {
87106 IJsEngine engine = null ;
@@ -126,5 +145,11 @@ public void Dispose()
126145 }
127146 }
128147 }
148+
149+ private class FactoryWithPriority
150+ {
151+ public Func < IJsEngine > Factory { get ; set ; }
152+ public int Priority { get ; set ; }
153+ }
129154 }
130155}
0 commit comments