A minor rewrite of the NodeJS EventEmitter in Python
Project description
node_events.py
A minor rewrite of the NodeJS EventEmitter in Python
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
Built Distribution
Hashes for node_events-0.5.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee5ee02dbe98c584bff513249e29fb2d26d2254be13c5aca413ec24192fc3cda |
|
MD5 | ddc3426bc6f187a96d458ef3b7382588 |
|
BLAKE2b-256 | a0e647d1ea703fd237918d6cefdc4fa8475f6ba670fb83f22d4f7c169674ca3d |