#nullable enable
using System;
using Cysharp.Threading.Tasks;
using ShrinkContext;
using ShrinkEventBus;
namespace ShrinkContext.EventBusAdapter
{
///
/// 把生成的 ShrinkEventBus 绑定包装为可逆效应。
///
public static class ShrinkEventBusEffects
{
///
/// 激活生成的 [ShrinkSubscribe] 绑定,并在 Context 回滚时统一释放。
///
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;
});
}
}
}