Code Monkey home page Code Monkey logo

levent's Introduction

Build Status license

Lua event

Lua event emitter/listener implementation. Implementation uses queueing, so it's always ok to emit messages during processing of previous message. The only pitfall you may encounter is that you can actually create deadlock if you recursively emit messages.

In the library there is defined type EventPool which handles emitting and binding of listeners.

local pool = EventPool.new()
--or
local pool2 = EventPool()

EventPool.new()

Creates new EventPool.

EventPool:bind( source, listener )

Bind listener to messages emitable by source. After source emits any messages pool will lookup callbacks from listener. Returns true on success and false + error on fail. Possible errors are:

  • EventPool.ERROR_NIL_SOURCE, if source is nil
  • EventPool.ERROR_NIL_LISTENER, if listener is nil
  • EventPool.ERROR_LISTENER_ALREADY_BINDED, if listener is already binded to source
local src = {}
local list = {}
pool:bind( src, list ) -- now list listens to src

EventPool:unbind( source, listener )

Unbind listener from source. Returns true on success and false + error on fail. Possible errors are:

  • EventPool.ERROR_NIL_SOURCE, if source is nil
  • EventPool.ERROR_NIL_LISTENER, if listener is nil
  • EventPool.ERROR_LISTENER_NOT_BINDED, if listener is not binded to source

EventPool:emit( source, message, ... )

Emits message from source. All binded listeners with field message will be notified and according method (message) will be called with passed varargs. Function can return:

  • EventPool.STATUS_PROCESSED, if events queue was empty
  • EventPool.STATUS_QUEUED, if events queue wasn't empty and message was queued
local src = { name = 'src', }
local list1 = { name = 'list1', onMessage = function( self, source, msg )
	print( self.name, source.name, msg )
end }
pool:bind( src, list1 )
pool:emit( src, 'onMessage', 42 ) -- will print list1, src, 42

EventPool:notify( source, message, ... )

Lower level of emit (internally emit is implemented using notify) this function directly notifies all listeners without queueing. This is a bit faster, but in many cases it can lead to very hard to catch bugs. It's better just not to use this function.

levent's People

Contributors

iskolbin avatar

Stargazers

Adam Wagner avatar Lynn avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.