Provider
elva.provider
Module holding provider components.
Classes:
-
WebsocketProvider–Handler for Y messages sent and received over a websocket connection.
Attributes:
-
WebsocketProviderState–The states for the
WebsocketProvidercomponent.
WebsocketProviderState = create_component_state('WebsocketProviderState', ('CONNECTED',))
module-attribute
The states for the WebsocketProvider component.
WebsocketProvider(ydoc, identifier, host, *args, port=None, safe=True, on_exception=None, **kwargs)
Bases: Component
Handler for Y messages sent and received over a websocket connection.
This component follows the Yjs protocol spec.
Parameters:
-
ydoc(Doc) –instance if the synchronized Y Document.
-
identifier(str) –identifier of the synchronized Y Document.
-
host(str) –hostname or IP address of the Y Document synchronizing websocket server.
-
port(int, default:None) –port of the Y Document synchronizing websocket server.
-
safe(bool, default:True) –flag whether to establish a secured (
True) or unsecured (False) connection. -
on_exception(Awaitable | None, default:None) –callback to which the current connection exception and a reference to the connection option mapping is given.
-
*args(tuple[Any], default:()) –positional arguments passed to
connect. -
**kwargs(dict[Any], default:{}) –keyword arguments passed to
connect.
Methods:
-
before–Hook subscribing to changes in the Y Document and starting the Awareness component.
-
run–Hook starting the connection loop as task.
-
cleanup–
Attributes:
-
basic_authorization_header(dict) –Mapping of
AuthorizationHTTP request header to encodedBasic Authenticationinformation. -
tried_credentials(bool) –Flag whether given credentials have already been tried.
-
ydoc(Doc) –Instance of the synchronized Y Document.
-
awareness(Awareness) –Instance of the awareness states.
-
options(dict) –Mapping of arguments to the signature of
connect. -
on_exception(Callable | Awaitable | None) –Callback to which the current connection exception and a reference to the connection option mapping is given.
-
states(WebsocketProviderState) –The states the websocket provider can have.
Source code in src/elva/provider.py
basic_authorization_header
instance-attribute
Mapping of Authorization HTTP request header to encoded Basic Authentication information.
tried_credentials
instance-attribute
Flag whether given credentials have already been tried.
_ydoc_subscription
instance-attribute
(while running) Object holding subscription information to changes in ydoc.
_awareness_subscription
instance-attribute
(while running) Identifier for the callback to which changes in awareness are sent to .
ydoc = ydoc
instance-attribute
Instance of the synchronized Y Document.
awareness = Awareness(ydoc)
instance-attribute
Instance of the awareness states.
_signature = signature(connect).bind(uri, *args, **kwargs)
instance-attribute
Object holding the positional and keyword arguments for connect.
options = self._signature.arguments
instance-attribute
Mapping of arguments to the signature of connect.
on_exception = on_exception
instance-attribute
Callback to which the current connection exception and a reference to the connection option mapping is given.
states
property
The states the websocket provider can have.
_connect()
async
Hook running the main connection loop in a shielded cancel scope.
Source code in src/elva/provider.py
_handle_connection()
async
Hook connecting and listening for incoming data.
It retries on HTTP response status codes 3xx and 5xx automatically
or gives the opportunity to update the connection options with
credentials via the on_exception hook.
Source code in src/elva/provider.py
_on_exception(exc)
async
Wrapper method around the on_exception attribute.
If on_exception was not given, it defaults to re-raising exc.
Parameters:
-
exc(WebSocketException) –exception raised by
connect.
Source code in src/elva/provider.py
before()
async
Hook subscribing to changes in the Y Document and starting the Awareness component.
Source code in src/elva/provider.py
run()
async
Hook starting the connection loop as task.
Source code in src/elva/provider.py
cleanup()
async
Hook cancelling the subscriptions to changes in ydoc and awareness,
draining the buffer, sending the last messages and
closing the websocket connection gracefully.
Source code in src/elva/provider.py
_send()
async
Hook listening for messages on the internal buffer and sending them.
Source code in src/elva/provider.py
_recv()
async
Hook listening for incoming messages on the websocket connection and processing them.
Source code in src/elva/provider.py
_on_transaction_event(event)
Hook called on changes in ydoc.
When called, the event data are encoded as Y update message and sent over the established websocket connection.
Parameters:
-
event(TransactionEvent) –object holding event information.
Source code in src/elva/provider.py
_on_awareness_change(topic, change)
Hook called on changes in awareness.
When called, updates from origin local are encoded as AWARENESS update message.
Messages from every other origin are ignored, as they came from remote and were already applied.
Parameters:
-
topic(Literal['update', 'change']) –The categorization of the awareness state change, either
"update"for all updates, even only renewals, or"change"for changes in the state itself. -
change(tuple[dict, str]) –a tuple of actions (
"added","updated","removed") and the origin of the awareness state change.
Source code in src/elva/provider.py
_on_connect()
async
Hook initializing cross synchronization.
When called, it sends a Y sync step 1 message and a Y sync step 2 message with respect to the null state, effectively doing a pro-active cross synchronization.
Source code in src/elva/provider.py
_on_recv(data)
async
Hook called on received data over the websocket connection.
When called, data is assumed to be a YMessage and tried to be decoded.
On successful decoding, the payload is dispatched to the appropriate method.
Parameters:
-
data(bytes) –message received from the synchronizing server.
Source code in src/elva/provider.py
_on_sync_step1(state)
async
Dispatch method called on received Y sync step 1 message.
It answers the message with a Y sync step 2 message according to the Yjs protocol spec.
Parameters:
-
state(bytes) –payload included in the incoming Y sync step 1 message.
Source code in src/elva/provider.py
_on_sync_update(update)
async
Dispatch method called on received Y sync update message.
The update gets applied to the internal Y Document instance.
Parameters:
-
update(bytes) –payload included in the incoming Y sync update message.
Source code in src/elva/provider.py
_on_awareness(state)
async
Dispatch method called on received Y awareness message.
Currently, this is defined as a no-op.
Parameters:
-
state(bytes) –payload included in the incoming Y awareness message.