Skip to main content

Type-safe event emitter with sync and async listener support

Project description

philiprehberger-event-emitter

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.4.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.4-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.4.tar.gz
Algorithm Hash digest
SHA256 4d429ce8b1fd5d81756825458b09c09cb0d26b66a8b71bac8bc63b0a7e0b19c1
MD5 63b5e4dcd976a22b69f63e5591d59d8e
BLAKE2b-256 2704d2629b5bcc278d648c93a599389e4c107ad854d068d562bddcc8c563f0e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6602035ff559b3ab771b43cccaf76c5cb7ce2932fdf642a60f04997eb7fdb47c
MD5 f860599a8cd77d94c651dd2ee7b2a1d8
BLAKE2b-256 14b64852daa314ff794b55f1f163db8b5a620b4823f4df7e2bf89786fbcd698c

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