-
Notifications
You must be signed in to change notification settings - Fork 138
/
AnonymousRenderer.cs
60 lines (54 loc) · 2.37 KB
/
AnonymousRenderer.cs
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
#nullable enable
using System;
using Libplanet.Action;
using Libplanet.Blocks;
namespace Libplanet.Blockchain.Renderers
{
/// <summary>
/// A renderer that invokes its callbacks.
/// <para>This class is useful when you want an one-use ad-hoc implementation (i.e., Java-style
/// anonymous class) of <see cref="IRenderer{T}"/> interface.</para>
/// </summary>
/// <example>
/// With object initializers, you can easily make an one-use renderer:
/// <code>
/// var renderer = new AnonymousRenderer<ExampleAction>
/// {
/// BlockRenderer = (oldTip, newTip) =>
/// {
/// // Implement RenderBlock() here.
/// };
/// };
/// </code>
/// </example>
/// <typeparam name="T">An <see cref="IAction"/> type. It should match to
/// <see cref="BlockChain{T}"/>'s type parameter.</typeparam>
public class AnonymousRenderer<T> : IRenderer<T>
where T : IAction, new()
{
/// <summary>
/// A callback function to be invoked together with
/// <see cref="RenderBlock(Block{T}, Block{T})"/>.
/// </summary>
public Action<Block<T>, Block<T>>? BlockRenderer { get; set; }
/// <summary>
/// A callback function to be invoked together with
/// <see cref="RenderReorg(Block{T}, Block{T}, Block{T})"/>.
/// </summary>
public Action<Block<T>, Block<T>, Block<T>>? ReorgRenderer { get; set; }
/// <summary>
/// A callback function to be invoked together with
/// <see cref="RenderReorgEnd(Block{T}, Block{T}, Block{T})"/>.
/// </summary>
public Action<Block<T>, Block<T>, Block<T>>? ReorgEndRenderer { get; set; }
/// <inheritdoc cref="IRenderer{T}.RenderBlock(Block{T}, Block{T})"/>
public void RenderBlock(Block<T> oldTip, Block<T> newTip) =>
BlockRenderer?.Invoke(oldTip, newTip);
/// <inheritdoc cref="IRenderer{T}.RenderReorg(Block{T}, Block{T}, Block{T})"/>
public void RenderReorg(Block<T> oldTip, Block<T> newTip, Block<T> branchpoint) =>
ReorgRenderer?.Invoke(oldTip, newTip, branchpoint);
/// <inheritdoc cref="IRenderer{T}.RenderReorgEnd(Block{T}, Block{T}, Block{T})"/>
public void RenderReorgEnd(Block<T> oldTip, Block<T> newTip, Block<T> branchpoint) =>
ReorgEndRenderer?.Invoke(oldTip, newTip, branchpoint);
}
}