Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to dynamically add/remove listeners on bots #463

Open
KallynGowdy opened this issue May 13, 2024 · 0 comments
Open

Add the ability to dynamically add/remove listeners on bots #463

KallynGowdy opened this issue May 13, 2024 · 0 comments
Labels
area:runtime Is related to the CasualOS runtime. (Everything in aux-runtime, aux-common, aux-vm, etc.) enhancement New feature or request

Comments

@KallynGowdy
Copy link
Member

KallynGowdy commented May 13, 2024

Currently, the only way to place a listener on a bot is to set a tag with the @ symbol. This means that it is currently only possible to have one listener on a bot for a particular event, and also that every listener must be a script that is compiled by itself.

This has a couple downsides:

  • There can only be one listener for an event per bot. (only one @onClick for example)
  • Every listener needs to be compiled separately per bot. (Dynamically adding/removing a listener like @onClick will trigger script compilation)
  • It is not possible to reuse listeners between bots.
  • Dynamically adding/removing listeners becomes a lot more difficult to manage.

Ideally, it would be possible for a script to register a listener for a bot using an API function call. This would allow bots to have multiple listeners for a single event (two @onClick handlers). It would make dynamically adding/removing listeners much easier. It would increase performance by allowing the user to skip the script compilation step and reuse listeners.

Here's the API:

os.addBotListener(bot, tagName, listenerFunction) - Registers a function to be called whenever an event would be sent for the given tag name to the given bot.
os.removeBotListener(bot, tagName, listenerFunction) - Unregisters a function to be called whenever an event would be sent for the given tag name to the given bot.

Additionally, functions should be able to be used directly in create() function calls:

create({
  // Registers a onClick listener on the bot during creation
  onClick: () => os.toast("You clicked the bot!"),
  
  // The registration happens before the @onCreate whisper,
  // so it is possible to do this
  onCreate: () => os.toast("You created the bot!"),
});

Listener functions should have the following structure:

type ListenerFunc = (that: any, bot: Bot, tagName: string) => any
@KallynGowdy KallynGowdy converted this from a draft issue May 13, 2024
@KallynGowdy KallynGowdy added enhancement New feature or request area:runtime Is related to the CasualOS runtime. (Everything in aux-runtime, aux-common, aux-vm, etc.) labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:runtime Is related to the CasualOS runtime. (Everything in aux-runtime, aux-common, aux-vm, etc.) enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

1 participant