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

Development

pip install -e .
python -m pytest tests/ -v

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.5.tar.gz (4.9 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.5-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.5.tar.gz
Algorithm Hash digest
SHA256 505ff3dd93f72b85ba5ad813c5d29496b6049ce796095dc52390d94f284b6184
MD5 08e7300db66edcc32f09a2e7487816ee
BLAKE2b-256 30172015ac2c7a9e88eca25810e64c9a57d7f2237ba03b18fe0d874c796cf18e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 463b5a7f4576fddec9e2d4cd377a08b0e99be58d31d1f10e32831c3fbac98aeb
MD5 f0aceefbe3937d1823a4179adccbba3b
BLAKE2b-256 98012d00bc565fa18281f06f660b9d6a2b5dce3f26932fd0211833ded81c3691

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