-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metadata column to SQLiteYStore, fix update broadcast
- Loading branch information
1 parent
0d9a54b
commit 6cddae8
Showing
5 changed files
with
130 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .websocket_provider import WebsocketProvider # noqa | ||
from .websocket_server import WebsocketServer, YRoom # noqa | ||
from .ydoc import YDoc # noqa | ||
from .yutils import YMessageType # noqa | ||
|
||
__version__ = "0.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import asyncio | ||
from types import TracebackType | ||
from typing import Optional, Type | ||
|
||
import y_py as Y | ||
|
||
from .yutils import create_update_message | ||
|
||
|
||
class YDoc(Y.YDoc): | ||
"""A YDoc with a custom transaction context manager that allows to put updates into a queue. | ||
`Y.YDoc.observe_after_transaction` catches all updates, while updates made through this YDoc | ||
can be processed separately, which is useful for e.g. updates made only from the backend. | ||
""" | ||
|
||
_begin_transaction = Y.YDoc.begin_transaction | ||
_update_queue: asyncio.Queue | ||
_ready: bool | ||
|
||
def init(self, update_queue: asyncio.Queue): | ||
self._ready = False | ||
self._update_queue = update_queue | ||
|
||
def begin_transaction(self): | ||
return Transaction(self, self._update_queue, self._ready) | ||
|
||
|
||
class Transaction: | ||
|
||
ydoc: YDoc | ||
update_queue: asyncio.Queue | ||
state: bytes | ||
transaction: Y.YTransaction | ||
ready: bool | ||
|
||
def __init__(self, ydoc: YDoc, update_queue: asyncio.Queue, ready: bool): | ||
self.ydoc = ydoc | ||
self.update_queue = update_queue | ||
self.ready = ready | ||
|
||
def __enter__(self): | ||
if self.ready: | ||
self.state = Y.encode_state_vector(self.ydoc) | ||
self.transaction = self.ydoc._begin_transaction() | ||
return self.transaction.__enter__() | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc_value: Optional[BaseException], | ||
exc_tb: Optional[TracebackType], | ||
) -> bool: | ||
res = self.transaction.__exit__(exc_type, exc_value, exc_tb) # type: ignore | ||
if self.ready: | ||
update = Y.encode_state_as_update(self.ydoc, self.state) | ||
message = create_update_message(update) | ||
self.update_queue.put_nowait(message) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters