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"})

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

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.1.1.tar.gz (3.1 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.1.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 465b91ea267937bca74e205c20d98f617c35e862ea3ed7f86ccba3a17009e596
MD5 d39f129d39ada3c0efbf894ad12fd86a
BLAKE2b-256 29bd45e3f9f67ff47bddf37fcd3336766115569732622caaf6e71edd6b8f3d8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_event_emitter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 549841468e64f1db4e72057bf9841d02de443657991e804248b1c4c5debc6b26
MD5 0a46a9bd8e2d944b338c0040beb8cef5
BLAKE2b-256 aaa6d655a43751fa11392bf20a3031dd6153dfbe0acb0e7baa7686b7009c8d48

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