Writes events from the server itself.
Functions#
__init__#
__init__(storage, handler) -> None| Parameter | Type | Description |
|---|---|---|
storage | StorageService | |
handler | _ServerHandler |
def __init__(self, storage: StorageService, handler: _ServerHandler) -> None:
self._storage = storage
self._handler = handlermake_write#
make_write(schema) -> ServerWriteThe server's own write(store_id=, event=, payload=) for the
events of schema: it runs the event's handler as the server's
identity for the store, stores the entry in one transaction with
it, and gives the handler's success. It fails as the handler does,
or with EventLogServerStoreError when the payload does not
encode; an event not in schema is a defect.
| Parameter | Type | Description |
|---|---|---|
schema | EventLogSchema[EventGroup[AnyEvent]] |
Returns ServerWrite
def make_write(self, schema: EventLogSchema[EventGroup[AnyEvent]]) -> ServerWrite:
"""The server's own ``write(store_id=, event=, payload=)`` for the
events of ``schema``: it runs the event's handler as the server's
identity for the store, stores the entry in one transaction with
it, and gives the handler's success. It fails as the handler does,
or with ``EventLogServerStoreError`` when the payload does not
encode; an event not in ``schema`` is a defect."""
events: dict[str, AnyEvent] = {}
for group in schema.groups:
events.update(group.events)
@overload
def write[P, A, E](
*, store_id: StoreId, event: Event[P, A, E], payload: P
) -> Effect[A, E | EventLogServerStoreError]: ...
@overload
def write(*, store_id: StoreId, event: str, payload: object) -> Effect[object, object]: ...
def write(
*, store_id: StoreId, event: AnyEvent | str, payload: object
) -> Effect[object, object]:
tag = event if isinstance(event, str) else event.tag
public_key = _server_write_public_key(store_id)
schema_event = events.get(tag)
if schema_event is None:
return die(LookupError(f'Event schema not found for: "{tag}"'))
def body():
encoded = yield from encode_effect(schema_event.payload_codec)(payload).map_error(
lambda _: EventLogServerStoreError(
"PersistenceFailure", public_key, store_id, "Failed to encode event"
)
)
entry = new(Entry, disable_checks=True)(
id=make_entry_id_unsafe(),
event=tag,
primary_key=schema_event.primary_key(payload),
payload=encoded,
)
from pyfect.effect import provide_service
result = yield from self._handler(
public_key=public_key,
store_id=store_id,
entry=entry,
conflicts=(),
payload=(payload,),
).pipe(provide_service(Identity, _client_identity(public_key)))
yield from self._storage.write(store_id, [entry])
return result
return self._storage.with_transaction(gen(body))
return write