Verse Library verse

01 Snippet

Manages and safely cancels spawned asynchronous tasks or coroutines using events and race conditions.

extracted-snippets/community/snippets/4dl/fortnite-cancelable-spawned-coroutines/01-snippet.verse

# Source URL: https://dev.epicgames.com/community/snippets/4dl/fortnite-cancelable-spawned-coroutines
# Local doc:  epic-docs/community/snippets/4dl/fortnite-cancelable-spawned-coroutines.md
  using { /Verse.org/Concurrency }
  SpawnRoutine<public>(Func : type{_()<suspends>:void}):routine =
  Routine := routine{Func := Func}
  Routine.Start()
  return Routine
  routine<public> := class():
  Func<public> : type{_()<suspends>:void}
  Event<protected> : event() = event(){}
  var MaybeTask<protected> : ?task(void) = false
  var _IsRunning<protected> : logic = false
  Start<internal>():void =
  Task := spawn {RunRoutine()}
  set MaybeTask = option {Task}
  RunRoutine<protected>()<suspends>: void =
  set _IsRunning = true
  race:
  Func()
  Event.Await()
  set _IsRunning = false
  Cancel<public>():void = Event.Signal()
  GetTask<public>()<decides><transacts>:task(void) = MaybeTask?
  IsRunning<public>()<decides><transacts>:void = _IsRunning?

  Expand code  Copy full snippet(29 lines long)
* ```
  SpawnRoutine(Func : type{_()<suspends>:void}):routine = 
      Routine := routine{Func := Func}
      Routine.Start()

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