1
|
|
#nullable enable
|
2
|
|
using System;
|
3
|
|
using Libplanet.Action;
|
4
|
|
using Libplanet.Blocks;
|
5
|
|
|
6
|
|
namespace Libplanet.Blockchain.Renderers
|
7
|
|
{
|
8
|
|
/// <summary>
|
9
|
|
/// A renderer that invokes its callbacks.
|
10
|
|
/// <para>This class is useful when you want an one-use ad-hoc implementation (i.e., Java-style
|
11
|
|
/// anonymous class) of <see cref="IRenderer{T}"/> interface.</para>
|
12
|
|
/// </summary>
|
13
|
|
/// <example>
|
14
|
|
/// With object initializers, you can easily make an one-use renderer:
|
15
|
|
/// <code>
|
16
|
|
/// var renderer = new AnonymousRenderer<ExampleAction>
|
17
|
|
/// {
|
18
|
|
/// BlockRenderer = (oldTip, newTip) =>
|
19
|
|
/// {
|
20
|
|
/// // Implement RenderBlock() here.
|
21
|
|
/// };
|
22
|
|
/// };
|
23
|
|
/// </code>
|
24
|
|
/// </example>
|
25
|
|
/// <typeparam name="T">An <see cref="IAction"/> type. It should match to
|
26
|
|
/// <see cref="BlockChain{T}"/>'s type parameter.</typeparam>
|
27
|
|
public class AnonymousRenderer<T> : IRenderer<T>
|
28
|
|
where T : IAction, new()
|
29
|
|
{
|
30
|
|
/// <summary>
|
31
|
|
/// A callback function to be invoked together with
|
32
|
|
/// <see cref="RenderBlock(Block{T}, Block{T})"/>.
|
33
|
|
/// </summary>
|
34
|
1
|
public Action<Block<T>, Block<T>>? BlockRenderer { get; set; }
|
35
|
|
|
36
|
|
/// <summary>
|
37
|
|
/// A callback function to be invoked together with
|
38
|
|
/// <see cref="RenderReorg(Block{T}, Block{T}, Block{T})"/>.
|
39
|
|
/// </summary>
|
40
|
1
|
public Action<Block<T>, Block<T>, Block<T>>? ReorgRenderer { get; set; }
|
41
|
|
|
42
|
|
/// <summary>
|
43
|
|
/// A callback function to be invoked together with
|
44
|
|
/// <see cref="RenderReorgEnd(Block{T}, Block{T}, Block{T})"/>.
|
45
|
|
/// </summary>
|
46
|
1
|
public Action<Block<T>, Block<T>, Block<T>>? ReorgEndRenderer { get; set; }
|
47
|
|
|
48
|
|
/// <inheritdoc cref="IRenderer{T}.RenderBlock(Block{T}, Block{T})"/>
|
49
|
|
public void RenderBlock(Block<T> oldTip, Block<T> newTip) =>
|
50
|
1
|
BlockRenderer?.Invoke(oldTip, newTip);
|
51
|
|
|
52
|
|
/// <inheritdoc cref="IRenderer{T}.RenderReorg(Block{T}, Block{T}, Block{T})"/>
|
53
|
|
public void RenderReorg(Block<T> oldTip, Block<T> newTip, Block<T> branchpoint) =>
|
54
|
1
|
ReorgRenderer?.Invoke(oldTip, newTip, branchpoint);
|
55
|
|
|
56
|
|
/// <inheritdoc cref="IRenderer{T}.RenderReorgEnd(Block{T}, Block{T}, Block{T})"/>
|
57
|
|
public void RenderReorgEnd(Block<T> oldTip, Block<T> newTip, Block<T> branchpoint) =>
|
58
|
1
|
ReorgEndRenderer?.Invoke(oldTip, newTip, branchpoint);
|
59
|
|
}
|
60
|
|
}
|