在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):simenkid/lua-events开源软件地址(OpenSource Url):https://github.com/simenkid/lua-events开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):lua-eventsCurrent version: v1.0.0 (stable) Table of Contents1. Overviewlua-events is an EventEmitter class that provides you with APIs in node.js style. I am using this module with my own nodemcu project on ESP8266 WiFi Soc. It helps me write my code in event-driven style. I often grab a emitter from this module and use the emitter as an event hub in my application to control my program flow. Try it on nodemcu, it won't let you down. If you like to code something in an asynchronous manner on nodemcu, might I sugguest nodemcu-timer? They are working well with each other to help arrange your asynchronous flow, e.g. your function can defer its callback and return right away. 2. InstallationClone from github
or use npm install
Just include the file
3. APIs
EventEmitter ClassExposed by local EventEmitter = require 'events' -- or 'events_min' EventEmitter:new([table])Create an instance of event emitter. If a table (or an object) is given, the table will inherit EventEmitter class and itself will be an event emiiter. Arguments:
Returns:
Examples: local EventEmitter = require 'events'
-- Inheritance
local myObject = {
name = 'foo',
greet = function ()
print('Hello!')
end
}
myObject = EventEmitter:new(myObject)
myObject:on('knock', function ()
print('knock, knock!')
end)
myObject:emit('knock')
-- A simple emitter
local hub = EventEmitter:new()
hub:on('sing', function (who)
print(who .. ' is singing.')
end)
hub:emit('sing', 'John')
emitter:emit(event[, ...])Calls each of the listeners in order with the given arguments. Arguments:
Returns:
Examples: local greet = 'hello'
local name = 'John Doe'
emitter:emit('knock', greet, name)
emitter:emit('tick') emitter:on(event, listener)Adds a listener to the listeners table of the specified event. Arguments:
Returns:
Examples: emitter:on('knock', function () {
print('Someone knocks.')
}); emitter:once(event, listener)Attaches a one time listener to the specified event. This listener will be removed after invoked. Arguments:
Returns:
Examples: emitter:once('knock', function () {
print('First visitor comes.')
}); emitter:addListener(event, listener)This is an alias of emitter.on(event, listener). emitter:removeListener(event, listener)Removes a listener from the listener table of the specified event. Arguments:
Returns:
Examples: local callback = function (something)
print('Someone says ' .. greet)
end
emitter:on('greeting', callback)
-- ...
emitter:removeListener('greeting', callback) emitter:removeAllListeners([event])Removes all listeners, or those of the specified event. Arguments:
Returns:
Examples: emitter:removeAllListeners('foo_event') emitter:getMaxListeners()Returns the current max listener value of the emitter. Arguments:
Returns:
emitter:listenerCount(event)Returns the number of listeners listening to the specified event. Arguments:
Returns:
Examples: local numOfListeners = emitter:listenerCount('knock') emitter:listeners(event)Returns a shallow copy of listener table for the specified event. Arguments:
Returns:
Examples: local knockHandlers = emitter:listeners('knock') emitter:setMaxListeners(n)The emitter will print a warning if more than 10 listeners are added to a event. This method let you increase the maximum number of listeners attached to a event. Arguments:
Returns:
******************************************** ## License MIT |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论