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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.3.tar.gz
Algorithm Hash digest
SHA256 fcb12af1deb021c0115decb2fb348558b1cf5f8d97c03fc9670966b4f0b2164b
MD5 b65aa9061bd0b259def3c388c268b76f
BLAKE2b-256 e4b2552308a0029acbe617ad237d45a14a46a0cdce1b45dceb7741624f22192d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 83fc1557182edec64745c42b1f8f4708b3230d166058e525079c51e9bc39ca90
MD5 7aee87c962a0f273717c23b689beaaa8
BLAKE2b-256 a9fb28f09d17717e1c512861baaa74ed72d7c95be57d2fa7914dcb1698f740b8

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