Skip to main content

Type-safe event emitter with sync and async listener support

Project description

philiprehberger-event-emitter

Tests PyPI version License

Type-safe event emitter with sync and async listener support.

Installation

pip install philiprehberger-event-emitter

Usage

Basic Events

from philiprehberger_event_emitter import EventEmitter

emitter = EventEmitter()

def on_user_created(user):
    print(f"User created: {user['name']}")

emitter.on("user:created", on_user_created)
emitter.emit("user:created", {"name": "Alice"})

Unsubscribe

# Using the returned unsubscribe function
unsubscribe = emitter.on("event", handler)
unsubscribe()

# Or manually
emitter.off("event", handler)

One-Time Listeners

emitter.once("init", lambda: print("Only fires once"))
emitter.emit("init")  # prints
emitter.emit("init")  # nothing

Async Listeners

async def async_handler(data):
    await save_to_db(data)

emitter.on("data:received", async_handler)

# Use async_emit to await async listeners
await emitter.async_emit("data:received", {"key": "value"})

Max Listeners

# Warn when too many listeners are added (helps detect memory leaks)
emitter = EventEmitter(max_listeners=10)

Management

emitter.listener_count("event")      # number of listeners
emitter.event_names()                 # list of events with listeners
emitter.remove_all_listeners("event") # remove all for one event
emitter.remove_all_listeners()        # remove all listeners

API

Method Description
on(event, listener) Register listener, returns unsubscribe function
once(event, listener) Register one-time listener
off(event, listener) Remove a listener
emit(event, *args, **kwargs) Emit event synchronously
async_emit(event, *args, **kwargs) Emit event, awaiting async listeners
listener_count(event) Count listeners for an event
event_names() List events with listeners
remove_all_listeners(event?) Remove all or event-specific listeners

License

MIT

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

philiprehberger_event_emitter-0.2.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_event_emitter-0.2.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_event_emitter-0.2.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.1.tar.gz
Algorithm Hash digest
SHA256 335a2b65f51d09882e0be74c13ca848600a792eaeae46c27e23c2c4c2b22c4be
MD5 a05aba5424cd0991bdbaed626ef79be2
BLAKE2b-256 a4089346601989d4fbec92ce146f71843171f21ab545410c7d20299e26cf6ccc

See more details on using hashes here.

File details

Details for the file philiprehberger_event_emitter-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b974a2527c6b6c919e71cd3a7251ebbec25367c40abac5007899fa1d07914e4d
MD5 d00822a9419a4a4bf3237d68cd047f95
BLAKE2b-256 c74eea423838921e6beac357a709bc9125cb7450d5c227db91e8a614fe7d495d

See more details on using hashes here.

Supported by

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