Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/Statiq.Common/Execution/EmptyExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public Task<ImmutableArray<IDocument>> ExecuteModulesAsync(IEnumerable<IModule>

public Stream GetContentStream(string content = null) => ExecutionState.GetContentStream(content);

public IJavaScriptEnginePool GetJavaScriptEnginePool(Action<IJavaScriptEngine> initializer = null, int startEngines = 10, int maxEngines = 25, int maxUsagesPerEngine = 100, TimeSpan? engineTimeout = null) =>
ExecutionState.GetJavaScriptEnginePool(initializer, startEngines, maxEngines, maxUsagesPerEngine, engineTimeout);
public IJavaScriptEngine GetJavaScriptEngine(Action<IJavaScriptEngine> configureEngine = null) =>
ExecutionState.GetJavaScriptEngine(configureEngine);

public Task<HttpResponseMessage> SendHttpRequestWithRetryAsync(Func<HttpRequestMessage> requestFactory) =>
ExecutionState.SendHttpRequestWithRetryAsync(requestFactory);
Expand Down
26 changes: 5 additions & 21 deletions src/core/Statiq.Common/Execution/IExecutionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,10 @@ internal set
Task<HttpResponseMessage> SendHttpRequestWithRetryAsync(Func<HttpRequestMessage> requestFactory, int retryCount);

/// <summary>
/// Gets a new <see cref="IJavaScriptEnginePool"/>. The returned engine pool should be disposed
/// when no longer needed.
/// </summary>
/// <param name="initializer">
/// The code to run when a new engine is created. This should configure
/// the environment and set up any required JavaScript libraries.
/// </param>
/// <param name="startEngines">The number of engines to initially start when a pool is created.</param>
/// <param name="maxEngines">The maximum number of engines that will be created in the pool.</param>
/// <param name="maxUsagesPerEngine">The maximum number of times an engine can be reused before it is disposed.</param>
/// <param name="engineTimeout">
/// The default timeout to use when acquiring an engine from the pool (defaults to 5 seconds).
/// If an engine can not be acquired in this time frame, an exception will be thrown.
/// </param>
/// <returns>A new JavaScript engine pool.</returns>
IJavaScriptEnginePool GetJavaScriptEnginePool(
Action<IJavaScriptEngine> initializer = null,
int startEngines = 10,
int maxEngines = 25,
int maxUsagesPerEngine = 100,
TimeSpan? engineTimeout = null);
/// Gets a new <see cref="IJavaScriptEngine"/>.
/// </summary>
/// <param name="configureEngine">Optional delegate to configure the engine.</param>
/// <returns>A new JavaScript engine.</returns>
IJavaScriptEngine GetJavaScriptEngine(Action<IJavaScriptEngine> configureEngine = null);
}
}
5 changes: 1 addition & 4 deletions src/core/Statiq.Common/JavaScript/IJavaScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
namespace Statiq.Common
{
/// <summary>
/// A common interface to a JavaScript engine. Every JavaScript engine is
/// obtained from a <see cref="IJavaScriptEnginePool"/> and will be returned to the
/// pool when it is disposed. Therefore, you must dispose the engine when
/// you are done with it.
/// A common interface to a JavaScript engine.
/// </summary>
public interface IJavaScriptEngine : IDisposable
{
Expand Down
36 changes: 0 additions & 36 deletions src/core/Statiq.Common/JavaScript/IJavaScriptEnginePool.cs

This file was deleted.

35 changes: 23 additions & 12 deletions src/core/Statiq.Core/Execution/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Jint;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -36,6 +38,8 @@ public class Engine : IEngine, IDisposable
// Gets initialized on first execute and reset when the pipeline collection changes
private PipelinePhase[] _phases;

private static readonly object JsEngineSwitcherLock = new object();

private bool _disposed;

/// <summary>
Expand Down Expand Up @@ -1057,18 +1061,25 @@ public async Task<HttpResponseMessage> SendHttpRequestWithRetryAsync(Func<HttpRe
}

/// <inheritdoc/>
public IJavaScriptEnginePool GetJavaScriptEnginePool(
Action<IJavaScriptEngine> initializer = null,
int startEngines = 10,
int maxEngines = 25,
int maxUsagesPerEngine = 100,
TimeSpan? engineTimeout = null) =>
new JavaScriptEnginePool(
initializer,
startEngines,
maxEngines,
maxUsagesPerEngine,
engineTimeout ?? TimeSpan.FromSeconds(5));
public IJavaScriptEngine GetJavaScriptEngine(Action<IJavaScriptEngine> configureEngine = null)
{
// First we need to check if the JsEngineSwitcher has been configured. We'll do this
// by checking the DefaultEngineName being set. If that's there we can safely assume
// its been configured somehow (maybe via a configuration file). If not we'll wire up
// Jint as the default engine.
lock (JsEngineSwitcherLock)
{
if (string.IsNullOrWhiteSpace(JsEngineSwitcher.Current.DefaultEngineName))
{
JsEngineSwitcher.Current.EngineFactories.Add(new JintJsEngineFactory());
JsEngineSwitcher.Current.DefaultEngineName = JintJsEngine.EngineName;
}
}

IJavaScriptEngine engine = new JavaScriptEngine(JsEngineSwitcher.Current.CreateDefaultEngine());
configureEngine?.Invoke(engine);
return engine;
}

/// <summary>
/// Applies settings for analyzers and log levels as "[analyzer]=[log level]" (log level is optional, "All" to set all analyzers).
Expand Down
9 changes: 2 additions & 7 deletions src/core/Statiq.Core/Execution/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,8 @@ public async Task<ImmutableArray<IDocument>> ExecuteModulesAsync(IEnumerable<IMo
public Stream GetContentStream(string content = null) => _contextData.Engine.GetContentStream(content);

/// <inheritdoc/>
public IJavaScriptEnginePool GetJavaScriptEnginePool(
Action<IJavaScriptEngine> initializer = null,
int startEngines = 10,
int maxEngines = 25,
int maxUsagesPerEngine = 100,
TimeSpan? engineTimeout = null) =>
_contextData.Engine.GetJavaScriptEnginePool(initializer, startEngines, maxEngines, maxUsagesPerEngine, engineTimeout);
public IJavaScriptEngine GetJavaScriptEngine(Action<IJavaScriptEngine> configureEngine = null) =>
_contextData.Engine.GetJavaScriptEngine(configureEngine);

// IDocumentFactory

Expand Down
81 changes: 0 additions & 81 deletions src/core/Statiq.Core/JavaScript/JavaScriptEnginePool.cs

This file was deleted.

Loading
Loading