from the event UMG update. TODO: test if you can use this for events outside of UMG by just signaling the event or if you need extra sauce
# Custom event class to hold an Event, Cancel, and Await function
event_subscription := class:
CancelEvent:event() = event(){}
Cancel():void=
CancelEvent.Signal()
# Async function that holds until the event is signaled or canceled
AwaitForEvent(Event:event(t), Callback(:t):void, Subscription:event_subscription where t : type)<suspends>:void=
race:
loop:
# Await till event is signaled
Payload := Event.Await()
Callback(Payload)
# Await till event is cancelled
Subscription.CancelEvent.Await()
# Call this function to subscribe to an event which spawns the AwaitForEvent async function to fire when the event is signaled
(Event:event(t) where t : type).Subscribe(Callback(:t):void):event_subscription=
Subscription := event_subscription{}
spawn{ AwaitForEvent(Event, Callback, Subscription) }
return Subscription