Page 1 of 1

Can I de-register an event?

Posted: Sat Jul 22, 2017 8:59 pm
by EmanueleCiriachi
As per title - I want to attach a handler to my global tick event, but only while I need it. Is there a function that does the opposite of registerAnonymousEventHandler()?

Re: Can I de-register an event?

Posted: Sat Jul 22, 2017 9:56 pm
by keneanung
There is no way to deregister one right now...

Re: Can I de-register an event?

Posted: Sat Jul 22, 2017 11:14 pm
by EmanueleCiriachi
Thank you, I'll stick to my home-made, boolean variable-dependent, on-tick events then :)

Re: Can I de-register an event?

Posted: Tue Jul 25, 2017 5:01 am
by SlySven
You can have a permanent handler set up via the GUI editor as a function in a script item (the handler function must have the same name as the script) and then you could control how that behaves with a variable it inspect when it runs... :ugeek:

Re: Can I de-register an event?

Posted: Thu Sep 07, 2017 2:10 am
by Skylark
Feature request! Being unable to de-register events makes my code error-prone.

Consider a module (in the sense that it's a modular bunch of code, not a Mudlet or Lua module) built around Mudlet's event engine; ie it listens and responds to a dozen+ different events. This module can be switched on and off. When it's off it shouldn't be responding to events. My options for registering events are either through registerAnonymousEventHandler() or the GUI editor. In the first case it's impossible to deregister listeners. In the second disabling the script in the GUI prevents the listener from firing, but there's no programmatic way to do this. disableScript() and enableScript() would do the trick if they existed, but they don't.

As such my only option is to insert an early return into every single one of my listeners. That's workable but as a human I'm prone to forget these things, and the only way to prove that all of the module's events are being properly skipped when it's disabled is exhaustively.

If I could disable scripts I'd be able to handle it by disabling the top-level script that contains the module's GUI-registered event listeners. If I could deregister listeners through an id returned by registerAnonymousEventHandler() or something:

Code: Select all

-- Register an event & add it to table of listener ids for this module
 table.insert(module.listeners, registerAnonymousEventHandler("onPrompt", callback))
 
 -- Deregister all events
 for i,v in ipairs(module.listeners) do
 	deRegisterAnonymousEventHandler(v)
 end
 module.listeners = {}
 
...then I'd be able to register or deregister the module's listeners when enabling / disabling the module.

If there's a better way to do this I'd appreciate the advice. If not, feature request!

Re: Can I de-register an event?

Posted: Thu Sep 07, 2017 3:36 am
by Vadi
That's a good case for it! Could you add it to https://github.com/Mudlet/Mudlet/issues/new ?

Re: Can I de-register an event?

Posted: Thu Sep 07, 2017 2:42 pm
by Skylark
Done!

Re: Can I de-register an event?

Posted: Tue Sep 26, 2017 6:50 pm
by keneanung
The next version will fix this. the registerAnonymousEventHandler() function returns an ID (similar to the tempTimer and others), which then can be used in the killAnonymousEventHandler() function.