Warning
Currently very experimental.
Disposify is a source generator to subscribe / unsubscribe C# events with using-dispose pattern.
using System;
using Disposify;
var c = new C();
using (c.Disposify().SomeEvent(v => ++v)) // subscribe with lambda
{
c.Invoke(100);
} // unsubscribe on Dispose()
public class C
{
public event Func<int, int>? SomeEvent;
public int Invoke(int a) => SomeEvent.Invoke(a);
}
It is also useful to convert callbacks into await
pattern.
using System;
using System.Threading.Tasks;
using Disposify;
var c = new C();
var tcs = new TaskCompletionSource<int>();
using (c.Disposify().SomethingCompleted(result => tcs.TrySetResult(result)))
{
var result = await tcs.Task;
}
public class C
{
public event Action<int>? SomethingCompleted;
}
Install from https://www.nuget.org/packages/Disposify/ .
Unity 2022.3.12f1 and later is supported.
Add a git dependency to package manager:
https://github.com/ruccho/Disposify.git?path=/Disposify.Unity/Packages/com.ruccho.disposify
- Disposifying events declared in non-generic classes
- Disposifying events declared in generic types and value types
- Returned
IDisposable
s are automatically pooled after the disposal. Disposify()
seems to returndynamic
but is replaced by performant overload (no dynamic, no boxed) by the source generator.
For instance types, use ((T)null).Disposify()
.
using System;
using Disposify;
using (((C)null).Disposify().SomeEvent(v => ++v))
{
C.Invoke(100);
}
public class C
{
public static event Func<int, int>? SomeEvent;
public static int Invoke(int a) => SomeEvent.Invoke(a);
}
For static types, use [GenerateDisposifier(typeof(T))]
.
using System;
using Disposify;
using (C_Disposifier.SomeEvent(v => ++v))
{
C.Invoke(100);
}
public static class C
{
public static event Func<int, int>? SomeEvent;
public static int Invoke(int a) => SomeEvent.Invoke(a);
}
[GenerateDisposifier(typeof(C))]
static partial class C_Disposifier
{
// members will be generated
}