Til.EventBus
2.0.1
dotnet add package Til.EventBus --version 2.0.1
NuGet\Install-Package Til.EventBus -Version 2.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Til.EventBus" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Til.EventBus" Version="2.0.1" />
<PackageReference Include="Til.EventBus" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Til.EventBus --version 2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Til.EventBus, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Til.EventBus&version=2.0.1
#tool nuget:?package=Til.EventBus&version=2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EventBus
一个高效的事件总线
如何使用
< C#
using Til.EventBus;
namespace Til.EventBusTest;
public class Demo {
[Fact]
public void staticEventHandleTest() {
IEventBus eventBus = new EventBus.EventBus(new EventBus.EventBus.EventBusBuilder());
eventBus.put(typeof(StaticEventHandle));
eventBus.onEvent(new ParentEvent.ChildEvent());
Assert.Equal(1, StaticEventHandle.counter);
}
[Fact]
public void eventHandleTest() {
IEventBus eventBus = new EventBus.EventBus(new EventBus.EventBus.EventBusBuilder());
EventHandle eventHandle = new EventHandle();
eventBus.put(eventHandle);
eventBus.onEvent(new ParentEvent.ChildEvent());
Assert.Equal(1, eventHandle.counter);
}
[Fact]
public async Task asyncEventHandleTest() {
IEventBus eventBus = new EventBus.EventBus(new EventBus.EventBus.EventBusBuilder());
AsyncEventHandle asyncEventHandle = new AsyncEventHandle();
eventBus.put(asyncEventHandle);
CancellationTokenSource source = new CancellationTokenSource();
//所有继承 IAsyncEvent 的事件可以以异步的形式发布
await eventBus.onEventAsync(new AsyncEvent(source.Token));
Assert.Equal(1, asyncEventHandle.counter);
}
[Fact]
public async Task groupingAsyncEventHandleTest() {
IEventBus eventBus = new EventBus.EventBus(new EventBus.EventBus.EventBusBuilder());
GroupingAsyncEventHandle groupingAsyncEventHandle = new GroupingAsyncEventHandle();
eventBus.put(groupingAsyncEventHandle);
CancellationTokenSource source = new CancellationTokenSource();
await eventBus.onEventAsync(new AsyncEvent.SonAsyncEvent(source.Token));
await eventBus.onEventAsync(new EmptyGroupSynchronizationEvent(source.Token));
}
}
public class StaticEventHandle {
public static int counter;
/// <summary>
/// 静态方法
/// 使用 eventBus.put(typeof(StaticEventHandle)); 注册
/// </summary>
[Event]
public static void onEvent(ParentEvent parentEvent) {
counter++;
}
}
public class EventHandle {
public int counter;
/// <summary>
/// 实例方法
/// 使用 eventBus.put(new EventHandle()); 注册
/// </summary>
[Event]
public void onEvent(ParentEvent parentEvent) {
counter++;
}
}
public class AsyncEventHandle {
public int counter;
[Event]
public async Task onEvent(AsyncEvent async) {
await Task.Delay(100);
counter++;
}
}
public class GroupingAsyncEventHandle {
public int counterA;
/// <summary>
/// 同组的将按照优先级从上到下调用
/// 空组默认并发处理
/// 其中异常不会传播
/// </summary>
[Event(concurrencyGroup = "A", priority = 1)]
public async Task onEvent_A1(AsyncEvent @event) {
await Task.Delay(100);
counterA++;
}
[Event(concurrencyGroup = "A", priority = 0)]
public async Task onEvent_A0(AsyncEvent @event) {
await Task.Delay(100);
Assert.Equal(1, counterA);
}
/// <summary>
/// EmptyGroupSynchronizationEvent 指定空组同步
/// 不需要指定 concurrencyGroup 也能按照优先级调用
/// </summary>
/// <param name="event"></param>
[Event(priority = 1)]
public async Task onEvent_B1(EmptyGroupSynchronizationEvent @event) {
await Task.Delay(100);
counterA++;
}
[Event(priority = 0)]
public async Task onEvent_B0(EmptyGroupSynchronizationEvent @event) {
await Task.Delay(100);
Assert.Equal(1, counterA);
}
}
public class ParentEvent : Event {
public class ChildEvent : ParentEvent {
}
}
public class AsyncEvent : Event, IAsyncEvent {
public CancellationToken token { get; }
public AsyncEvent(CancellationToken token) {
this.token = token;
}
public class SonAsyncEvent(CancellationToken token) : AsyncEvent(token);
}
public class EmptyGroupSynchronizationEvent : Event, IAsyncEvent {
public CancellationToken token { get; }
public EmptyGroupSynchronizationEvent(CancellationToken token) {
this.token = token;
}
public bool isEmptyGroupSynchronization => true;
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.