Client-side toolkit for abstract events.
Project description
Simple example
# main.py
from collections.abc import Sequence
from typing import NamedTuple
import pydepot
from eventoolkit import Event, EventPublisher, StoreEventHandler
class Chatroom(NamedTuple):
users: tuple[str, ...]
class State(NamedTuple):
chatroom: Chatroom
class StateSubscriber:
def on_state(self, state: State) -> None:
print(f"[StoreSubscriber] on_state called with {state}")
class JoinUserAction(pydepot.Action):
def __init__(self, user: str):
self.user: str = user
class JoinUserReducer(pydepot.Reducer[JoinUserAction, State]):
@property
def action_type(self) -> type[JoinUserAction]:
return JoinUserAction
def apply(self, action: JoinUserAction, state: State) -> State:
return State(chatroom=Chatroom(users=(*state.chatroom.users, action.user)))
class UserJoinedEvent(Event):
@property
def name(self) -> str:
return "user_joined"
def __init__(self, user: str):
self.user: str = user
class UserJoinedEventHandler(StoreEventHandler[UserJoinedEvent, State]):
@property
def event_type(self) -> type[UserJoinedEvent]:
return UserJoinedEvent
def actions(self, event: UserJoinedEvent) -> Sequence[pydepot.Action]:
return [JoinUserAction(user=event.user)]
def main() -> None:
store = pydepot.Store(State(chatroom=Chatroom(users=())))
store.register(JoinUserReducer())
subscriber = StateSubscriber()
store.subscribe(subscriber)
handler = UserJoinedEventHandler(store=store)
publisher = EventPublisher()
publisher.subscribe(handler)
publisher.publish(UserJoinedEvent(user="Alice"))
publisher.publish(UserJoinedEvent(user="Bob"))
if __name__ == "__main__":
main()
$ python3 main.py
[StoreSubscriber] on_state called with
State(chatroom=Chatroom(users=('Alice',)))
[StoreSubscriber] on_state called with
State(chatroom=Chatroom(users=('Alice', 'Bob')))
Installation
Eventoolkit is available as eventoolkit
on PyPI:
pip install eventoolkit
Usage
For detailed quickstart and API reference, visit the Documentation.
License
Copyright (C) 2023 tombartk
This program is free software: you can redistribute it and/or modify it under the terms
of the GNU Affero General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program.
If not, see https://www.gnu.org/licenses/.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
eventoolkit-0.1.0.tar.gz
(24.3 kB
view details)
Built Distribution
File details
Details for the file eventoolkit-0.1.0.tar.gz
.
File metadata
- Download URL: eventoolkit-0.1.0.tar.gz
- Upload date:
- Size: 24.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.24.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b6ba891f2449b50562e548ed14b5900362d0369c5b21f7b6e389bbc3f6ccd99 |
|
MD5 | e1efb501d6542547d05e7b0ea91b8783 |
|
BLAKE2b-256 | e4e6e079a21628f5d2c5c46e5d3f9cabb227a1e6f30c4302d9e8e9b14b38d7bb |
File details
Details for the file eventoolkit-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: eventoolkit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.24.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01330dedd2f8c36f4bc144c91ceedca3ef54b3d004e6eb0ddb581e3abe7b0869 |
|
MD5 | 6c88a10bcae4fde198e9274c1db3386a |
|
BLAKE2b-256 | a009b91cf378bfc47a09bef80a49efd79960ff40162a58497a645239ea8aeaf8 |