using System.Threading;
using R3;
using UnityEngine.UIElements;

namespace KitsuneCafe.Extension
{
    public static class VisualElementExtensions
    {
        public static Observable<T> ObserveEvent<T>(this VisualElement target, CancellationToken token = default) where T : EventBase<T>, new()
        {
            return Observable.FromEvent<EventCallback<T>, T>(
                h => e => h(e),
                e => target.RegisterCallback(e),
                e => target.UnregisterCallback(e),
                token
            );
        }
    }
}
