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.

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.

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.