2009年12月31日 Tiago Katcipis <tiagokatcipis@gmail.com>: >> If you do implement it, especially >> with your experience in with so many other event systems, I'd be very >> interested in seeing what it >> looks like. > > Well I'm experienced with this other systems but i do not master them :-), i > don't know if i will have a good idea of how to implement it on Lua, that's > exactly why I'm trying to get some help from you guys. I wrote a very simple event system on top of a Win32 API binding. You can find the source online: http://piratery.net/hg/lwin32/file/tip/src/win32/eventloop.lua It's pretty specific to my underlying event system (the Windows one), but the outer API may be a good example of what to provide. Basically the module has four functions : register(object, handler) Associate the 'handler' function with the 'object'. When it's notified the 'handler' function is called. unregister(object) Removes the association of a handler with 'object', if any. run() Enters a loop that read from the system events sent to all registered objects, and call the associated handlers. The function returns after a timeout, which is set by writing the 'timeout' variable in the module (a handle can set it to zero to exit the 'run' loop). flush() Process all pendings events on registered objects, calling handlers, and return. There is no easy way to interrupt running Lua code, and process an event, because the Lua interpreter is single-threaded. However with a coroutine scheduler on top of the event module described above you can get a pretty flexible system. Another example is Copas, which is kindof specialized for network servers, but the principle could be extended. Finally I attached another eventloop.lua file (which is different from the one linked and described above), which is such a generalization of the Copas architecture for mixed client/server application.
Attachment:
eventloop.lua
Description: Binary data