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

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.2.tar.gz (4.7 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.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.2.tar.gz
Algorithm Hash digest
SHA256 91748b36f57bdc2c6df989525d3be32b9f55e60647215a320cd4cebea073e2ed
MD5 2024127bda9dcd80a911682f5cf9cae8
BLAKE2b-256 aceb780731b9ee8f78e95fc0f74cce9655f1a4a721185fd6e26a31fb4def7d35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fcde7698a92a1f5f97ea0377c4307c8afa39ee9a610055f7d0198d8b139ad087
MD5 2431c65c4db7ee6330d8ce538d9c342d
BLAKE2b-256 863f8204a2978823b482fde64a6288ecb513747dac52cc26a64d4755aa3f9b8e

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