Can I de-register an event?

Post Reply
EmanueleCiriachi
Posts: 9
Joined: Tue Jul 18, 2017 8:03 pm

Can I de-register an event?

Post by EmanueleCiriachi » Sat Jul 22, 2017 8:59 pm

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()?

User avatar
keneanung
Site Admin
Posts: 85
Joined: Mon Mar 21, 2011 9:36 am

Re: Can I de-register an event?

Post by keneanung » Sat Jul 22, 2017 9:56 pm

There is no way to deregister one right now...

EmanueleCiriachi
Posts: 9
Joined: Tue Jul 18, 2017 8:03 pm

Re: Can I de-register an event?

Post by EmanueleCiriachi » Sat Jul 22, 2017 11:14 pm

Thank you, I'll stick to my home-made, boolean variable-dependent, on-tick events then :)

User avatar
SlySven
Posts: 778
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK

Re: Can I de-register an event?

Post by SlySven » Tue Jul 25, 2017 5:01 am

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:

Skylark
Posts: 46
Joined: Mon Feb 22, 2010 12:38 am

Re: Can I de-register an event?

Post by Skylark » Thu Sep 07, 2017 2:10 am

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!

User avatar
Vadi
Posts: 4409
Joined: Sat Mar 14, 2009 3:13 pm

Re: Can I de-register an event?

Post by Vadi » Thu Sep 07, 2017 3:36 am

That's a good case for it! Could you add it to https://github.com/Mudlet/Mudlet/issues/new ?

Skylark
Posts: 46
Joined: Mon Feb 22, 2010 12:38 am

Re: Can I de-register an event?

Post by Skylark » Thu Sep 07, 2017 2:42 pm

Done!

User avatar
keneanung
Site Admin
Posts: 85
Joined: Mon Mar 21, 2011 9:36 am

Re: Can I de-register an event?

Post by keneanung » Tue Sep 26, 2017 6:50 pm

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.

Post Reply

Who is online

Users browsing this forum: Baidu [Spider] and 7 guests