This commit is contained in:
CortexCore
2024-11-08 17:28:07 +08:00
parent 1650126d55
commit faf72b05e9
14 changed files with 196 additions and 379 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit
{
@@ -22,6 +23,23 @@ namespace BITKit
[CustomType(typeof(IValidHandle))]
public sealed class ValidHandle: IValidHandle
{
public class MyHandle:IDisposable
{
private readonly ValidHandle _validHandle;
public MyHandle(ValidHandle validHandle)
{
_validHandle = validHandle;
_validHandle.AddElement(this);
}
public void Dispose()
{
_validHandle.RemoveElement(this);
}
}
public MyHandle GetHandle() => new MyHandle(this);
public override string ToString()
{
return $"Allow:{enableHandle}\nElements:{string.Join("\n",objs)}\nDisableElements:{string.Join("\n",disableObjs)}";
@@ -51,6 +69,7 @@ namespace BITKit
public readonly List<object> disableObjs = new List<object>();
private bool tempEnable;
private Action<bool> EventOnEnableChanged;
private readonly List<UniTaskCompletionSource> _completionSources = new();
public void AddElement(object obj)
@@ -73,7 +92,14 @@ namespace BITKit
{
enableHandle = tempEnable;
if (EventOnEnableChanged is not null)
{
EventOnEnableChanged.Invoke(enableHandle);
}
if (tempEnable) return;
foreach (var cs in _completionSources)
{
cs.TrySetResult();
}
}
}
public void RemoveElement(object obj)
@@ -164,7 +190,16 @@ namespace BITKit
EventOnEnableChanged -= action;
}
}
public UniTask.Awaiter GetAwaiter()
{
if (Allow is false)
{
return UniTask.CompletedTask.GetAwaiter();
}
var cs = new UniTaskCompletionSource();
_completionSources.Add(cs);
return cs.Task.GetAwaiter();
}
public void Clear()
{
objs.Clear();