Last active
October 13, 2024 18:45
-
-
Save MattRix/5de865fa3e87cdc91673cf4a2a7ac0ac to your computer and use it in GitHub Desktop.
Send additional data to Subscribe() listeners in Verse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#usage example (when subscribing to an event that returns an agent) | |
ButtonDevice.InteractedWithEvent.SubscribeAgent(OnButtonInteract, "Hello!") | |
OnButtonInteract(Agent : agent, Text : string) : void = | |
Print("Button interacted with {Text}!") | |
#usage example (when subscribing to an event that returns tuple(), aka an empty tuple) | |
CampfireDevice.CampfirePulseEvent.SubscribeEmpty(OnCampfirePulse, 90210) | |
OnCampfirePulse(Number : int) : void = | |
Print("Campfire pulse had {Number}!") | |
#put this stuff somewhere in one of your verse files, perhaps utils.verse or something! | |
(Listenable : listenable(agent)).SubscribeAgent(OutputFunc : tuple(agent, t)->void, ExtraData : t where t:type) : cancelable = | |
Wrapper := wrapper_agent(t){ExtraData := ExtraData, OutputFunc := OutputFunc} | |
Listenable.Subscribe(Wrapper.InputFunc) | |
wrapper_agent(t : type) := class(): | |
ExtraData : t; | |
OutputFunc : tuple(agent, t) -> void | |
InputFunc(Agent : agent):void = OutputFunc(Agent, ExtraData) | |
(Listenable : listenable(tuple())).SubscribeEmpty(OutputFunc : t -> void, ExtraData : t where t:type) : cancelable = | |
Wrapper := wrapper_empty(t) {ExtraData := ExtraData, OutputFunc := OutputFunc} | |
Listenable.Subscribe(Wrapper.InputFunc) | |
wrapper_empty(t : type) := class(): | |
ExtraData : t; | |
OutputFunc : t -> void | |
InputFunc():void = OutputFunc(ExtraData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment