Skip to main content

A minor rewrite of the NodeJS EventEmitter in Python

Project description

node_events.py

A minor rewrite of the NodeJS EventEmitter in Python

PyPI Version License Python Version

Installing

Via PyPI:

pip install node_events

Usage

from node_events import EventEmitter

myEmitter = EventEmitter()


def fn():
    print("An event occurred")


myEmitter.on('event', fn)
myEmitter.emit('event')

# Prints
#   An event occurred

API

Class: EventEmitter

The EventEmitter class is defined and exposed by the module:

from node_events import EventEmitter

EventEmitter.addListener(eventName, listener)

  • eventName: <string>
  • listener: <function>
  • Returns: <EventEmitter>

Alias for self.on(eventName, listener)

EventEmitter.on(eventName, listener)

  • eventName: <string> The name of the event.
  • listener: <function> The callback function.
  • Returns: <EventEmitter>

Appends the listener to the listeners array for the event named eventName. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times. By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

Returns a reference to the EventEmitter, so that calls can be chained.

emitter = EventEmitter()

def appendListener():
  print('a')

def prependListener():
  print('b')

emitter.on('test', appendListener)
emitter.prependListener('test', appendListener)

emitter.emit('test')

# Prints
#   b
#   a

EventEmitter.once(eventName, listener)

  • eventName: <string> The name of the event.
  • listener: <function> The callback function.
  • Returns: <EventEmitter>

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked. By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

Returns a reference to the EventEmitter, so that calls can be chained.

emitter = EventEmitter()

def appendListener():
  print('a')

def prependListener():
  print('b')

emitter.once('test', appendListener)
emitter.prependOnceListener('test', appendListener)

emitter.emit('test')

# Prints
#   b
#   a

EventEmitter.prependListener(eventName, listener)

  • eventName: <string> The name of the event.
  • listener: <function> The callback function.
  • Returns: <EventEmitter>

Adds the listener function to the beginning of the listeners array for the event named eventName. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

emitter = EventEmitter()

def newConnection():
  print('someone connected!')

emitter.prependListener('connection', newConnection)
emitter.emit('connection')

Returns a reference to the EventEmitter, so that calls can be chained.

EventEmitter.prependOnceListener(eventName, listener)

  • eventName: <string> The name of the event.
  • listener: <function> The callback function.
  • Returns: <EventEmitter>

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

emitter = EventEmitter()

def newConnection():
  print('someone connected!')

emitter.prependOnceListener('connection', newConnection)
emitter.emit('connection')

Returns a reference to the EventEmitter, so that calls can be chained.

Development

Building

Feel free to clone, use in adherance to the license and perhaps send pull requests

git clone https://github.com/miraclx/node_events.py.git
cd node_events.py
# hack on code
python3 setup.py bdist_wheel
pip3 install dist/*.whl --user

License

Apache 2.0 © Miraculous Owonubi (@miraclx) <omiraculous@gmail.com>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

node_events-0.5.2.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

node_events-0.5.2-py3-none-any.whl (7.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page