This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkContext;
|
||||
using ShrinkEventBus;
|
||||
|
||||
namespace ShrinkContext.EventBusAdapter
|
||||
{
|
||||
/// <summary>
|
||||
/// 把生成的 ShrinkEventBus 绑定包装为可逆效应。
|
||||
/// </summary>
|
||||
public static class ShrinkEventBusEffects
|
||||
{
|
||||
/// <summary>
|
||||
/// 激活生成的 [ShrinkSubscribe] 绑定,并在 Context 回滚时统一释放。
|
||||
/// </summary>
|
||||
public static ShrinkEffectHandle EffectAttach(this ShrinkCtx ctx, object target,
|
||||
ShrinkBusKey? defaultBus = null)
|
||||
{
|
||||
if (ctx == null)
|
||||
throw new ArgumentNullException(nameof(ctx));
|
||||
if (target == null)
|
||||
throw new ArgumentNullException(nameof(target));
|
||||
|
||||
var binding = EventBus.Attach(target, defaultBus);
|
||||
return ctx.EffectInverse(() =>
|
||||
{
|
||||
binding.Dispose();
|
||||
return UniTask.CompletedTask;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user