# How do you implement your own listenable in Verse?

**URL:** <https://forums.unrealengine.com/t/how-do-you-implement-your-own-listenable-in-verse/832267>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, fortnite\
**Created:** [March 29, 2023, 3:13pm UTC](https://forums.unrealengine.com/t/how-do-you-implement-your-own-listenable-in-verse/832267 "2023-03-29T15:13:18Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [September 6, 2023, 8:58pm UTC](https://forums.unrealengine.com/t/how-do-you-implement-your-own-listenable-in-verse/832267/8 "2023-09-06T20:58:05Z")

</div>

I might have found a better way

```auto
custom_cancelable<public> := class:
    var Canceled : logic = false

    CancelEvent : event() = event(){}

    Cancel():void=
        set Canceled = true
        CancelEvent.Signal()

custom_subscribable<public>(t:type)<computes> := class<public>(awaitable(t)):

    InnerEvent : event(t) = event(t){}

    Subscribe(Callback:type {__(:t):void}):custom_cancelable=
        Cancelable := custom_cancelable{}
        spawn{_WaitForEvent(Callback, Cancelable)}

        Cancelable

    _WaitForEvent(Callback:type {__(:t):void}, Cancelable: custom_cancelable)<suspends>:void=
        race:
            # Wait for payload to be sent
            loop:
                Payload := InnerEvent.Await()

                # Just in case
                if(not Cancelable.Canceled?):
                    Callback(Payload)
                else:
                    break # Just in case

            # Cancelable got canceled
            Cancelable.CancelEvent.Await()

    
    Signal<public>(Payload:t):void =
        InnerEvent.Signal(Payload)

    Await<override>()<suspends>:t=
        InnerEvent.Await()

```

These might not be stored along in an array of `cancelable`, but it seem to work good (forgetting that it uses `spawns`)

EDIT: For anyone wanting to use this, just need to warn you, the garbage collector will not clean up the `suspending` calls  
So each `.Subscribe()` call will stay alive untill you manually call `.Cancel()` or `.CancelAll()`  
So to say, try to not dynamically generate `.Subscribe()` calls and you should be fine 👍

---

_[View the full topic](https://forums.unrealengine.com/t/how-do-you-implement-your-own-listenable-in-verse/832267)._
