Skip to content

Instantly share code, notes, and snippets.

@MattRix
Last active May 4, 2023 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/5de865fa3e87cdc91673cf4a2a7ac0ac to your computer and use it in GitHub Desktop.
Save MattRix/5de865fa3e87cdc91673cf4a2a7ac0ac to your computer and use it in GitHub Desktop.
Send additional data to Subscribe() listeners in Verse
#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