Skip to content

Commit 94fcd92

Browse files
fcollonvalgithub-actions[bot]hbcarlosdavidbrochart
authored
Improve code architecture (#146)
* Improve code architecture * Restructure loaders * Clean websocket server * Something is broken * Finalize PR * Self review * Automatic application of license header * Update jupyter_collaboration/loaders.py * Update jupyter_collaboration/websocketserver.py * Update jupyter_collaboration/app.py Co-authored-by: David Brochart <[email protected]> * Update jupyter_collaboration/loaders.py Co-authored-by: David Brochart <[email protected]> * Update jupyter_collaboration/websocketserver.py Co-authored-by: David Brochart <[email protected]> * Create clean up tasks --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Carlos Herrero <[email protected]> Co-authored-by: David Brochart <[email protected]>
1 parent c894eb3 commit 94fcd92

File tree

10 files changed

+504
-204
lines changed

10 files changed

+504
-204
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,4 @@ docs/source/changelog.md
133133
!.yarn/sdks
134134
!.yarn/versions
135135
packages/docprovider/junit.xml
136+
.jupyter_ystore.db

docs/source/developer/contributing.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ Installing JupyterLab Real-Time Collaboration
3232
The development version of the server requires `node <https://nodejs.org/en/download/>`_ and `pip <https://pip.pypa.io/en/stable/installing/>`_.
3333

3434
Once you have installed the dependencies mentioned above, use the following
35-
steps::
35+
steps:
36+
37+
.. code-block:: shell
3638
3739
pip install --upgrade pip
3840
git clone https://github.com/jupyterlab/jupyter_collaboration
3941
cd jupyter_collaboration
40-
pip install -e .
42+
pip install -e ".[dev,test]"
43+
jupyter labextension develop --overwrite .
44+
4145
4246
If you are using a system-wide Python installation and you only want to install the server for you,
4347
you can add ``--user`` to the install commands.
@@ -97,7 +101,7 @@ Running Tests
97101

98102
Install dependencies::
99103

100-
pip install -e .[test]
104+
pip install -e .[dev,test]
101105
pip install -e examples/simple # to test the examples
102106

103107
To run the Python tests, use::

jupyter_collaboration/app.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
3+
from __future__ import annotations
4+
5+
import asyncio
36

47
from jupyter_server.extension.application import ExtensionApp
5-
from traitlets import Float, Int, Type
8+
from traitlets import Float, Type
69
from ypy_websocket.ystore import BaseYStore
710

811
from .handlers import DocSessionHandler, YDocWebSocketHandler
12+
from .loaders import FileLoaderMapping
913
from .stores import SQLiteYStore
1014
from .utils import EVENTS_SCHEMA_PATH
15+
from .websocketserver import JupyterWebsocketServer
1116

1217

1318
class YDocExtension(ExtensionApp):
1419
name = "jupyter_collaboration"
1520

16-
file_poll_interval = Int(
21+
file_poll_interval = Float(
1722
1,
1823
config=True,
1924
help="""The period in seconds to check for file changes on disk.
2025
Defaults to 1s, if 0 then file changes will only be checked when
2126
saving changes from the front-end.""",
2227
)
2328

24-
document_cleanup_delay = Int(
29+
document_cleanup_delay = Float(
2530
60,
2631
allow_none=True,
2732
config=True,
@@ -61,12 +66,47 @@ def initialize_settings(self):
6166
)
6267

6368
def initialize_handlers(self):
69+
# Set configurable parameters to YStore class
70+
for k, v in self.config.get(self.ystore_class.__name__, {}).items():
71+
setattr(self.ystore_class, k, v)
72+
73+
self.ywebsocket_server = JupyterWebsocketServer(
74+
rooms_ready=False,
75+
auto_clean_rooms=False,
76+
ystore_class=self.ystore_class,
77+
log=self.log,
78+
)
79+
80+
# self.settings is local to the ExtensionApp but here we need
81+
# the global app settings in which the file id manager will register
82+
# itself maybe at a later time.
83+
self.file_loaders = FileLoaderMapping(
84+
self.serverapp.web_app.settings, self.log, self.file_poll_interval
85+
)
86+
6487
self.handlers.extend(
6588
[
66-
(r"/api/collaboration/room/(.*)", YDocWebSocketHandler),
89+
(
90+
r"/api/collaboration/room/(.*)",
91+
YDocWebSocketHandler,
92+
{
93+
"document_cleanup_delay": self.document_cleanup_delay,
94+
"document_save_delay": self.document_save_delay,
95+
"file_loaders": self.file_loaders,
96+
"ystore_class": self.ystore_class,
97+
"ywebsocket_server": self.ywebsocket_server,
98+
},
99+
),
67100
(r"/api/collaboration/session/(.*)", DocSessionHandler),
68101
]
69102
)
70103

71104
async def stop_extension(self):
72-
YDocWebSocketHandler.clean_up()
105+
# Cancel tasks and clean up
106+
await asyncio.wait(
107+
[
108+
asyncio.create_task(self.ywebsocket_server.clean()),
109+
asyncio.create_task(self.file_loaders.clear()),
110+
],
111+
timeout=3,
112+
)

0 commit comments

Comments
 (0)