Skip to content

Commit 56e0e18

Browse files
committed
In JavaScriptEngineSwitcher.Node the default Node JS service is now implemented as a wrapper around the StaticNodeJSService class
1 parent 8804c26 commit 56e0e18

File tree

4 files changed

+121
-12
lines changed

4 files changed

+121
-12
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
using Jering.Javascript.NodeJS;
7+
8+
namespace JavaScriptEngineSwitcher.Node
9+
{
10+
/// <summary>
11+
/// Default Node JS service
12+
/// </summary>
13+
/// <remarks>
14+
/// Wrapper around the <see cref="StaticNodeJSService"/> class.
15+
/// </remarks>
16+
public sealed class DefaultNodeJsService : INodeJSService
17+
{
18+
/// <summary>
19+
/// Instance of default Node JS service
20+
/// </summary>
21+
private static readonly DefaultNodeJsService _instance = new DefaultNodeJsService();
22+
23+
/// <summary>
24+
/// Gets a instance of default Node JS service
25+
/// </summary>
26+
public static INodeJSService Instance
27+
{
28+
get { return _instance; }
29+
}
30+
31+
32+
/// <summary>
33+
/// Private constructor for implementation Singleton pattern
34+
/// </summary>
35+
private DefaultNodeJsService()
36+
{ }
37+
38+
39+
#region INodeJSService implementation
40+
41+
public Task<T> InvokeFromFileAsync<T>(string modulePath, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
42+
{
43+
return StaticNodeJSService.InvokeFromFileAsync<T>(modulePath, exportName, args, cancellationToken);
44+
}
45+
46+
public Task InvokeFromFileAsync(string modulePath, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
47+
{
48+
return StaticNodeJSService.InvokeFromFileAsync(modulePath, exportName, args, cancellationToken);
49+
}
50+
51+
public Task<T> InvokeFromStringAsync<T>(string moduleString, string newCacheIdentifier = null, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
52+
{
53+
return StaticNodeJSService.InvokeFromStringAsync<T>(moduleString, newCacheIdentifier, exportName, args, cancellationToken);
54+
}
55+
56+
public Task InvokeFromStringAsync(string moduleString, string newCacheIdentifier = null, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
57+
{
58+
return StaticNodeJSService.InvokeFromStringAsync(moduleString, newCacheIdentifier, exportName, args, cancellationToken);
59+
}
60+
61+
public Task<T> InvokeFromStringAsync<T>(Func<string> moduleFactory, string cacheIdentifier, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
62+
{
63+
return StaticNodeJSService.InvokeFromStringAsync<T>(moduleFactory, cacheIdentifier, exportName, args, cancellationToken);
64+
}
65+
66+
public Task InvokeFromStringAsync(Func<string> moduleFactory, string cacheIdentifier, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
67+
{
68+
return StaticNodeJSService.InvokeFromStringAsync(moduleFactory, cacheIdentifier, exportName, args, cancellationToken);
69+
}
70+
71+
public Task<T> InvokeFromStreamAsync<T>(Stream moduleStream, string newCacheIdentifier = null, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
72+
{
73+
return StaticNodeJSService.InvokeFromStreamAsync<T>(moduleStream, newCacheIdentifier, exportName, args, cancellationToken);
74+
}
75+
76+
public Task InvokeFromStreamAsync(Stream moduleStream, string newCacheIdentifier = null, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
77+
{
78+
return StaticNodeJSService.InvokeFromStreamAsync(moduleStream, newCacheIdentifier, exportName, args, cancellationToken);
79+
}
80+
81+
public Task<T> InvokeFromStreamAsync<T>(Func<Stream> moduleFactory, string cacheIdentifier, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
82+
{
83+
return StaticNodeJSService.InvokeFromStreamAsync<T>(moduleFactory, cacheIdentifier, exportName, args, cancellationToken);
84+
}
85+
86+
public Task InvokeFromStreamAsync(Func<Stream> moduleFactory, string cacheIdentifier, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
87+
{
88+
return StaticNodeJSService.InvokeFromStreamAsync(moduleFactory, cacheIdentifier, exportName, args, cancellationToken);
89+
}
90+
91+
public Task<(bool, T)> TryInvokeFromCacheAsync<T>(string moduleCacheIdentifier, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
92+
{
93+
return StaticNodeJSService.TryInvokeFromCacheAsync<T>(moduleCacheIdentifier, exportName, args, cancellationToken);
94+
}
95+
96+
public Task<bool> TryInvokeFromCacheAsync(string moduleCacheIdentifier, string exportName = null, object[] args = null, CancellationToken cancellationToken = default)
97+
{
98+
return StaticNodeJSService.TryInvokeFromCacheAsync(moduleCacheIdentifier, exportName, args, cancellationToken);
99+
}
100+
101+
#region IDisposable implementation
102+
103+
public void Dispose()
104+
{
105+
throw new NotSupportedException();
106+
}
107+
108+
#endregion
109+
110+
#endregion
111+
}
112+
}

src/JavaScriptEngineSwitcher.Node/JavaScriptEngineSwitcher.Node.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
This package does not contain the `node.exe`. Therefore, you need to install the Node.js (https://nodejs.org) and add the `node.exe`'s directory to the `Path` environment variable (automatically done by the official installer).</Description>
1616
<PackageIcon>icon.png</PackageIcon>
1717
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;Node.js;Jering.Javascript.NodeJS</PackageTags>
18+
<PackageReleaseNotes>Default Node JS service is now implemented as a wrapper around the `Jering.Javascript.NodeJS.StaticNodeJSService` class.</PackageReleaseNotes>
1819
</PropertyGroup>
1920

2021
<Import Project="../../build/common.props" />

src/JavaScriptEngineSwitcher.Node/NodeJsEngine.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public sealed class NodeJsEngine : JsEngineBase
9595
/// Constructs an instance of adapter for the Node JS engine
9696
/// </summary>
9797
public NodeJsEngine()
98-
: this(GetOrCreateJsService(), new NodeSettings())
98+
: this(DefaultNodeJsService.Instance, new NodeSettings())
9999
{ }
100100

101101
/// <summary>
@@ -111,7 +111,7 @@ public NodeJsEngine(INodeJSService nodeJsService)
111111
/// </summary>
112112
/// <param name="settings">Settings of the Node JS engine</param>
113113
public NodeJsEngine(NodeSettings settings)
114-
: this(GetOrCreateJsService(), settings)
114+
: this(DefaultNodeJsService.Instance, settings)
115115
{ }
116116

117117
/// <summary>
@@ -151,16 +151,6 @@ public NodeJsEngine(INodeJSService service, NodeSettings settings)
151151
}
152152

153153

154-
private static INodeJSService GetOrCreateJsService()
155-
{
156-
Type staticNodeJsServiceType = typeof(StaticNodeJSService);
157-
MethodInfo getOrCreateNodeJsServiceMethodInfo = staticNodeJsServiceType.GetMethod("GetOrCreateNodeJSService",
158-
BindingFlags.NonPublic | BindingFlags.Static);
159-
var nodeJsService = (INodeJSService)getOrCreateNodeJsServiceMethodInfo.Invoke(null, new object[0]);
160-
161-
return nodeJsService;
162-
}
163-
164154
private void InvokeEngineHelper(string exportName = null, object[] args = null)
165155
{
166156
Task<bool> cachedTask = _jsService.TryInvokeFromCacheAsync(ENGINE_HELPERS_RESOURCE_NAME,

src/JavaScriptEngineSwitcher.Node/readme.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
Node.js (https://nodejs.org) and add the `node.exe`'s directory to the `Path`
2020
environment variable (automatically done by the official installer).
2121

22+
=============
23+
RELEASE NOTES
24+
=============
25+
Default Node JS service is now implemented as a wrapper around the
26+
`Jering.Javascript.NodeJS.StaticNodeJSService` class.
27+
2228
=============
2329
DOCUMENTATION
2430
=============

0 commit comments

Comments
 (0)