forked from pauldotknopf/JavaScriptViewEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsEngineFactory.cs
More file actions
62 lines (56 loc) · 1.73 KB
/
JsEngineFactory.cs
File metadata and controls
62 lines (56 loc) · 1.73 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
using JavaScriptViewEngine.Pool;
using System;
namespace JavaScriptViewEngine
{
/// <summary>
/// Handles creation of <see cref="IRenderEngine"/> instances. All methods are thread-safe.
/// </summary>
public class RenderEngineFactory : IRenderEngineFactory
{
IRenderEnginePool _pool;
bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="RenderEngineFactory"/> class.
/// </summary>
public RenderEngineFactory(IRenderEnginePool pool)
{
_pool = pool;
}
/// <summary>
/// Gets a JavaScript engine from the pool.
/// </summary>
/// <returns>The JavaScript engine</returns>
public virtual IRenderEngine GetEngine()
{
EnsureValidState();
return _pool.GetEngine();
}
/// <summary>
/// Returns an engine to the pool so it can be reused
/// </summary>
/// <param name="engine">Engine to return</param>
public virtual void ReturnEngineToPool(IRenderEngine engine)
{
if (!_disposed)
_pool.ReturnEngineToPool(engine);
}
/// <summary>
/// Clean up all engines
/// </summary>
public virtual void Dispose()
{
_disposed = true;
_pool?.Dispose();
_pool = null;
}
/// <summary>
/// Ensures that this object has not been disposed, and that no error was thrown while
/// loading the scripts.
/// </summary>
public void EnsureValidState()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
}
}
}