forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeJsEngineFactory.cs
More file actions
130 lines (109 loc) · 3.21 KB
/
NodeJsEngineFactory.cs
File metadata and controls
130 lines (109 loc) · 3.21 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#if NET10_0_OR_GREATER
using Lock = System.Threading.Lock;
#else
using Lock = System.Object;
#endif
using Jering.Javascript.NodeJS;
using Microsoft.Extensions.DependencyInjection;
using JavaScriptEngineSwitcher.Core;
namespace JavaScriptEngineSwitcher.Node
{
/// <summary>
/// Node JS engine factory
/// </summary>
public sealed class NodeJsEngineFactory : IJsEngineFactory
{
/// <summary>
/// The services available in the application
/// </summary>
private IServiceCollection _services;
/// <summary>
/// Node JS service
/// </summary>
private INodeJSService _jsService;
/// <summary>
/// Settings of the Node JS engine
/// </summary>
private readonly NodeSettings _settings;
/// <summary>
/// Synchronizer of Node JS service creation
/// </summary>
private readonly Lock _creationSynchronizer = new Lock();
/// <summary>
/// Constructs an instance of the Node JS engine factory
/// </summary>
public NodeJsEngineFactory()
: this(new NodeSettings())
{ }
/// <summary>
/// Constructs an instance of the Node JS engine factory
/// </summary>
/// <param name="service">Node JS service</param>
public NodeJsEngineFactory(INodeJSService service)
: this(service, new NodeSettings())
{ }
/// <summary>
/// Constructs an instance of the Node JS engine factory
/// </summary>
/// <param name="services">The services available in the application</param>
public NodeJsEngineFactory(IServiceCollection services)
: this(services, new NodeSettings())
{ }
/// <summary>
/// Constructs an instance of the Node JS engine factory
/// </summary>
/// <param name="settings">Settings of the Node JS engine</param>
public NodeJsEngineFactory(NodeSettings settings)
{
_settings = settings;
}
/// <summary>
/// Constructs an instance of the Node JS engine factory
/// </summary>
/// <param name="service">Node JS service</param>
/// <param name="settings">Settings of the Node JS engine</param>
public NodeJsEngineFactory(INodeJSService service, NodeSettings settings)
{
_jsService = service;
_settings = settings;
}
/// <summary>
/// Constructs an instance of the Node JS engine factory
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="settings">Settings of the Node JS engine</param>
public NodeJsEngineFactory(IServiceCollection services, NodeSettings settings)
{
_services = services;
_settings = settings;
}
#region IJsEngineFactory implementation
/// <inheritdoc/>
public string EngineName
{
get { return NodeJsEngine.EngineName; }
}
/// <summary>
/// Creates a instance of the Node JS engine
/// </summary>
/// <returns>Instance of the Node JS engine</returns>
public IJsEngine CreateEngine()
{
if (_services is not null && _jsService is null)
{
lock (_creationSynchronizer)
{
if (_jsService is null)
{
ServiceProvider serviceProvider = _services.BuildServiceProvider();
_jsService = serviceProvider.GetRequiredService<INodeJSService>();
}
}
}
IJsEngine engine = _jsService is not null ?
new NodeJsEngine(_jsService, _settings) : new NodeJsEngine(_settings);
return engine;
}
#endregion
}
}