npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lua-eventemitter

v1.0.3

Published

lua-eventemitter is an EventEmitter class that provides you with APIs in node.js style.

Downloads

13

Readme

lua-events

Current version: v1.0.0 (stable)
Compatible Lua version: 5.1.x

Table of Contents

  1. Overiew
  2. Installation
  3. APIs

1. Overview

lua-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. Installation

Clone from github

$ git clone https://github.com/simenkid/lua-events.git

or use npm install

$ npm install lua-eventemitter

Just include the file events.lua or use the minified one events_min.lua in your project.
If you are with the nodemcu on ESP8266, it would be good for you to compile *.lua text file into *.lc bytecode to further reduce memory usage.

  • FS/Memory footprint

    • events.lua: ~5.1 KB(file size) / ~17 KB (heap available after loaded)
    • events_min.lua: ~2.9 KB(size) / ~17 KB (heap)
    • events.lc: ~4.0 KB(size) / ~24 KB (heap)
    • timer.lc + events.lc: ~17.7 KB (heap)

3. APIs


EventEmitter Class

Exposed by require 'events'

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:

  1. table (table): If given, the table (or an object) itself will be returned as an event emitter. If not given, a new emitter will be returned.

Returns:

  • (object) emitter

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:

  1. event (string): Event name.
  2. ... (variadic arguments): The parameters pass along with the event.

Returns:

  • (boolean) Returns true if listeners exist, false otherwise.

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:

  1. event (string): Event name.
  2. listener (listener): Event handler.

Returns:

  • (object) emitter

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:

  1. event (string): Event name.
  2. listener (listener): Event handler.

Returns:

  • (object) emitter

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:

  1. event (string): Event name.
  2. listener (function): Event handler

Returns:

  • (object) emitter

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:

  1. event (string): This is optional. If given, all listeners attached to the specified event will be removed. If not given, all listeners of the emiiter will be removed.

Returns:

  • (object) emitter

Examples:

emitter:removeAllListeners('foo_event')

emitter:getMaxListeners()

Returns the current max listener value of the emitter.

Arguments:

  1. none

Returns:

  • (number) Number of current max listeners.


emitter:listenerCount(event)

Returns the number of listeners listening to the specified event.

Arguments:

  1. event (string): Event name.

Returns:

  • (number) Total number of the listeners.

Examples:

local numOfListeners = emitter:listenerCount('knock')

emitter:listeners(event)

Returns a shallow copy of listener table for the specified event.

Arguments:

  1. event (string): Event name.

Returns:

  • (table) table of listeners

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:

  1. n (number): Maximum number of listeners attached to a event.

Returns:

  • (object) emitter