-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMessage.cs
83 lines (70 loc) · 2.22 KB
/
IMessage.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Diagnostics.CodeAnalysis;
namespace Cnoom.UnityTool.MessageQueue
{
/// <summary>
/// 消息接口
/// </summary>
public interface IMessage
{
int priority { get; }
void Handle([NotNull] Action onFinish);
/// <summary>
/// 创建消息
/// </summary>
/// <param name="priority"></param>
/// <param name="onFinish"></param>
/// <returns></returns>
public static IMessage Create(int priority, Action<Action> onFinish)
{
return Message.CreateInstance(priority, onFinish);
}
/// <summary>
/// 创建简单的消息,不会等待
/// </summary>
/// <param name="priority"></param>
/// <param name="action"></param>
/// <returns></returns>
public static IMessage Create(int priority, Action action)
{
return SimpleMessage.CreateInstance(priority,action);
}
private record SimpleMessage : IMessage
{
public int priority { get; }
private readonly Action action;
private SimpleMessage(int i, Action action)
{
this.action = action;
priority = i;
}
public static SimpleMessage CreateInstance(int priority,Action action)
{
return new SimpleMessage(priority,action);
}
public void Handle(Action onFinish)
{
action();
onFinish();
}
}
private record Message : IMessage
{
public int priority { get; }
private readonly Action<Action> action;
private Message(int priority, [NotNull] Action<Action> action)
{
this.priority = priority;
this.action = action;
}
public static Message CreateInstance(int priority, Action<Action> action)
{
return new Message(priority, action);
}
public void Handle(Action onFinish)
{
action(onFinish);
}
}
}
}