-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFrameworkLifecycleContracts.cs
More file actions
155 lines (143 loc) · 5.38 KB
/
FrameworkLifecycleContracts.cs
File metadata and controls
155 lines (143 loc) · 5.38 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
namespace STS2RitsuLib
{
/// <summary>
/// Base type for framework lifecycle notifications published through
/// <see cref="RitsuLibFramework.SubscribeLifecycle" />.
/// 通过 <see cref="RitsuLibFramework.SubscribeLifecycle" /> 发布的框架生命周期通知的
/// 基类型。
/// </summary>
public interface IFrameworkLifecycleEvent
{
/// <summary>
/// UTC timestamp when the event was raised.
/// 事件引发时的 UTC 时间戳。
/// </summary>
DateTimeOffset OccurredAtUtc { get; }
}
/// <summary>
/// Marker for events that are replayed to new subscribers when <c>replayCurrentState</c> is true.
/// 当 <c>replayCurrentState</c> 为 true 时,会向新订阅者重放的事件标记。
/// </summary>
public interface IReplayableFrameworkLifecycleEvent : IFrameworkLifecycleEvent;
/// <summary>
/// Fired while the RitsuLib framework is initializing (before mods complete setup).
/// RitsuLib 框架初始化期间触发(在 mod 完成设置之前)。
/// </summary>
/// <param name="FrameworkModId">
/// Manifest id of the framework mod.
/// 框架 mod 的清单 id。
/// </param>
/// <param name="FrameworkVersion">
/// Framework assembly or package version string.
/// 框架程序集或包版本字符串。
/// </param>
/// <param name="OccurredAtUtc">
/// When the event was raised.
/// 事件引发的时间。
/// </param>
public readonly record struct FrameworkInitializingEvent(
string FrameworkModId,
string FrameworkVersion,
DateTimeOffset OccurredAtUtc
) : IFrameworkLifecycleEvent;
/// <summary>
/// Fired after framework initialization finished.
/// 框架初始化完成后触发。
/// </summary>
/// <param name="FrameworkModId">
/// Manifest id of the framework mod.
/// 框架 mod 的清单 id。
/// </param>
/// <param name="IsActive">
/// Whether the framework considers itself active for this session.
/// 框架是否认为自己在本会话中处于活动状态。
/// </param>
/// <param name="OccurredAtUtc">
/// When the event was raised.
/// 事件引发的时间。
/// </param>
public readonly record struct FrameworkInitializedEvent(
string FrameworkModId,
bool IsActive,
DateTimeOffset OccurredAtUtc
) : IReplayableFrameworkLifecycleEvent;
/// <summary>
/// Fired before profile-scoped services are initialized.
/// 在档案作用域服务初始化之前触发。
/// </summary>
/// <param name="OccurredAtUtc">
/// When the event was raised.
/// 事件引发的时间。
/// </param>
public readonly record struct ProfileServicesInitializingEvent(
DateTimeOffset OccurredAtUtc
) : IFrameworkLifecycleEvent;
/// <summary>
/// Fired after profile-scoped services are ready.
/// 在档案作用域服务就绪后触发。
/// </summary>
/// <param name="ProfileId">
/// Active profile identifier.
/// 活动档案标识符。
/// </param>
/// <param name="OccurredAtUtc">
/// When the event was raised.
/// 事件引发的时间。
/// </param>
public readonly record struct ProfileServicesInitializedEvent(
int ProfileId,
DateTimeOffset OccurredAtUtc
) : IReplayableFrameworkLifecycleEvent;
/// <summary>
/// Receives strongly typed lifecycle events from <see cref="RitsuLibFramework.SubscribeLifecycle" />.
/// 接收来自 <see cref="RitsuLibFramework.SubscribeLifecycle" /> 的强类型生命周期事件。
/// </summary>
public interface ILifecycleObserver
{
/// <summary>
/// Called for each lifecycle event; implementors typically switch on concrete event types.
/// 针对每个生命周期事件调用;实现通常会按具体事件类型分支。
/// </summary>
/// <param name="evt">
/// The event instance.
/// 事件实例。
/// </param>
void OnEvent(IFrameworkLifecycleEvent evt);
}
internal sealed class DelegateLifecycleObserver<TEvent>(Action<TEvent> handler) : ILifecycleObserver
where TEvent : IFrameworkLifecycleEvent
{
public void OnEvent(IFrameworkLifecycleEvent evt)
{
if (evt is TEvent typedEvent)
handler(typedEvent);
}
}
internal sealed class LifecycleSubscriptionHolder
{
public IDisposable Subscription { get; set; } = null!;
}
internal sealed class DelegateLifecycleObserverWithSubscription<TEvent>(
Action<TEvent, IDisposable> handler,
LifecycleSubscriptionHolder holder
) : ILifecycleObserver
where TEvent : IFrameworkLifecycleEvent
{
public void OnEvent(IFrameworkLifecycleEvent evt)
{
if (evt is TEvent typedEvent)
handler(typedEvent, holder.Subscription);
}
}
internal sealed class FrameworkLifecycleSubscription(Action unsubscribe) : IDisposable
{
private bool _disposed;
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
unsubscribe();
}
}
}