Verse Library verse

01 Snippet

Provides extensible subscribable and listenable event classes with built-in cancellation support.

extracted-snippets/community/snippets/Ej2p/fortnite-subscribable-event/01-snippet.verse

# Source URL: https://dev.epicgames.com/community/snippets/Ej2p/fortnite-subscribable-event
# Local doc:  epic-docs/community/snippets/Ej2p/fortnite-subscribable-event.md
  using { /Verse.org/Concurrency }
  subscribable_event<public>(t:type) := class(subscribable(t), signalable(t)):
  var Callbacks<private>:[]tuple(type{_(:t):void}, int) = array{}
  var NextID<private>:int = 0
  Signal<override>(Payload:t):void =
  for (Callback : Callbacks) do Callback(0)(Payload)
  Subscribe<override>(Callback:type{_(:t):void})<transacts>:cancelable =
  ID := NextID
  set NextID += 1
  set Callbacks += array{(Callback, ID)}
  return basic_cancelable:
  ID := ID
  CancelCallback := CancelSubscription
  CancelAllSubscriptions<public>()<transacts>:void =
  set Callbacks = array{}
  CancelSubscription<private>(ID:int)<transacts>:void =
  set Callbacks = for (Callback : Callbacks, Callback(1) <> ID) do Callback
  listenable_event<public>(t:type) := class(subscribable_event(t), listenable(t)):
  Event<private>:event(t) = event(t){}
  Await<override>()<suspends>:t = Event.Await()
  Signal<override>(Payload:t):void =
  (super:)Signal(Payload)
  Event.Signal(Payload)
  basic_cancelable<internal> := class(cancelable):
  ID<internal>:int
  CancelCallback<internal>:type{_(:int)<transacts>:void}
  Cancel<override>()<transacts>:void =
  CancelCallback(ID)

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in