Pyjo.EventEmitter - Event emitter base class

from Pyjo.EventEmitter import EventEmitter

class Cat(EventEmitter):
    # Emit events
    def poke(self):
        self.emit('roar', 3)

# Subscribe to events
tiger = Cat()

@tiger.on('roar')
def roar(tiger, times):
    for _ in range(times):
        print('RAWR!')

tiger.poke()

# Or import the module, as in Google Python Style Guide

import Pyjo.EventEmitter

class Cat(Pyjo.EventEmitter.object):
    # Emit events
    def poke(self):
        self.emit('roar', 3)

# Subscribe to events
tiger = Cat.new()

@tiger.on('roar')
def roar(tiger, times):
    for _ in range(times):
        print('RAWR!')

tiger.poke()

Pyjo.EventEmitter is a simple base class for event emitting objects.

Events

Pyjo.EventEmitter can emit the following events.

error

e.on('error', lambda e, err: ...)

This is a special event for errors, it will not be emitted directly by this class, but is fatal if unhandled. Subclasses may choose to emit it, but are not required to do so.

e.on('error', lambda e, err: print(f'This looks bad: {err}'))

An unhandled error raises a Pyjo.Exception with the name of the class and the error in its message, and an error that is an exception is its cause.

Debugging

You can set the PYJO_EVENTEMITTER_DEBUG environment variable to get some advanced diagnostics information printed to sys.stderr.

PYJO_EVENTEMITTER_DEBUG=1

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::EventEmitter of Mojolicious 9.49.

class Pyjo.EventEmitter.Pyjo_EventEmitter[source]

Pyjo.EventEmitter inherits all methods from Pyjo.Base and implements the following new ones.

catch(cb: Callback) → Self[source]
e = e.catch(lambda e, err: ...)

Subscribe to the error event.

# Longer version
e.on('error', lambda e, err: ...)
emit(name: str, *args: Any) → Self[source]
e = e.emit('foo')
e = e.emit('foo', 123)

Emit event.

events(name: str) → Pyjo.Iterator.object[tuple[Any, ...]][source]
async for args in e.events('foo'):
    ...

The arguments of the events as a Pyjo.Iterator, as a tuple for each event, which are queued until they are taken, and which ends at the event close or end, and raises the error of the event error.

async for message, in ws.events('message'):
    print(f'Message: {message}')

# The messages in capitals, in a chain
async for message in ws.events('message').map(lambda args: args[0].upper()):
    print(f'Message: {message}')
has_subscribers(name: str) → bool[source]
boolean = e.has_subscribers('foo')

Check if event has subscribers.

on(name: F, cb: None = None) → F[source]
on(name: str, cb: None = None) → Callable[[F], F]
on(name: str, cb: F) → F
cb = e.on('foo', lambda e, *args: ...)

Subscribe to event.

e.on('foo', lambda e, *args: ...)

Without a callback, it is a decorator, with the name of the event or of the function.

@e.on('foo')
def cb(e, *args):
    ...

@e.on
def foo(e, *args):
    ...
once(name: Callback, cb: None = None) → Callback[source]
once(name: str, cb: None = None) → Callable[[Callback], Callback]
once(name: str, cb: Callback) → Callback
cb = e.once('foo', lambda e, *args: ...)

Subscribe to event and unsubscribe again after it has been emitted once.

e.once('foo', lambda e, *args: ...)

Without a callback, it is a decorator, as on() is.

once_p(name: str) → Pyjo_Promise[source]
promise = e.once_p('foo')

Same as once(), but returns a Pyjo.Promise object, which is fulfilled with the arguments of the event, and rejected with the error of the event error, which comes first.

code, signal = await child.once_p('close')
subscribers(name: str) → list[Callback][source]
subscribers = e.subscribers('foo')

All subscribers for event.

# Unsubscribe last subscriber
e.unsubscribe('foo', e.subscribers('foo')[-1])

# Change order of subscribers
e.subscribers('foo').reverse()
unsubscribe(name: str, cb: Callback | None = None) → Self[source]
e = e.unsubscribe('foo')
e = e.unsubscribe('foo', cb)

Unsubscribe from event.