[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Removing event handlers
- From: Coda Highland <chighland@...>
- Date: Tue, 4 Sep 2012 13:41:39 -0700
On Tue, Sep 4, 2012 at 1:37 PM, James Norton <jamesnorton@gmail.com> wrote:
> I'm sure this must be a pretty common situation, but I can't seem to find a
> solution anywhere. Pointers to an existing solution would be greatly
> appreciated.
>
> I have implemented a callback system that lets users register Lua functions
> as handlers/listeners for events received by objects like this:
>
> local myListener = function(event)
>
> -- do something here
>
> end
>
>
> someObject:addEventListener("myEvent", myListener)
>
>
>
> Internally, my C code calls luaL_ref to get a reference to the callback
> function and then stores that reference in an array of callbacks associated
> with the event, in this case "myEvent". These references are then used to
> call the handlers when the event occurs.
>
> This works fine, but I want to be able to remove the listener using the same
> syntax:
>
> someObject:removeEventListener("myEvent", myListener)
>
> The problem with this is that I can't think of a way to associate the
> function on the stack at this point (myListener), with the reference I
> stored earlier. Calling luaL_ref again with the same function simply
> generates a new reference. So apparently I need to store something else to
> identify the listener in addition to the reference, but I'm not sure what.
>
> Any suggestions would be greatly appreciated.
>
> Thanks,
>
> James
> --
> James Norton
> Sent with Sparrow
>
I would create a table and store it in the registry, with the keys
being the callback functions and the values being the registry index
you got earlier.
/s/ Adam