Skip to content

Commit

Permalink
Improve code architecture (#146)
Browse files Browse the repository at this point in the history
* 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 <david.brochart@gmail.com>

* Update jupyter_collaboration/loaders.py

Co-authored-by: David Brochart <david.brochart@gmail.com>

* Update jupyter_collaboration/websocketserver.py

Co-authored-by: David Brochart <david.brochart@gmail.com>

* Create clean up tasks

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Carlos Herrero <26092748+hbcarlos@users.noreply.github.com>
Co-authored-by: David Brochart <david.brochart@gmail.com>
  • Loading branch information
4 people committed May 18, 2023
1 parent c894eb3 commit 94fcd92
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 204 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ docs/source/changelog.md
!.yarn/sdks
!.yarn/versions
packages/docprovider/junit.xml
.jupyter_ystore.db
10 changes: 7 additions & 3 deletions docs/source/developer/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ Installing JupyterLab Real-Time Collaboration
The development version of the server requires `node <https://nodejs.org/en/download/>`_ and `pip <https://pip.pypa.io/en/stable/installing/>`_.

Once you have installed the dependencies mentioned above, use the following
steps::
steps:

.. code-block:: shell
pip install --upgrade pip
git clone https://github.com/jupyterlab/jupyter_collaboration
cd jupyter_collaboration
pip install -e .
pip install -e ".[dev,test]"
jupyter labextension develop --overwrite .
If you are using a system-wide Python installation and you only want to install the server for you,
you can add ``--user`` to the install commands.
Expand Down Expand Up @@ -97,7 +101,7 @@ Running Tests

Install dependencies::

pip install -e .[test]
pip install -e .[dev,test]
pip install -e examples/simple # to test the examples

To run the Python tests, use::
Expand Down
50 changes: 45 additions & 5 deletions jupyter_collaboration/app.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import asyncio

from jupyter_server.extension.application import ExtensionApp
from traitlets import Float, Int, Type
from traitlets import Float, Type
from ypy_websocket.ystore import BaseYStore

from .handlers import DocSessionHandler, YDocWebSocketHandler
from .loaders import FileLoaderMapping
from .stores import SQLiteYStore
from .utils import EVENTS_SCHEMA_PATH
from .websocketserver import JupyterWebsocketServer


class YDocExtension(ExtensionApp):
name = "jupyter_collaboration"

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

document_cleanup_delay = Int(
document_cleanup_delay = Float(
60,
allow_none=True,
config=True,
Expand Down Expand Up @@ -61,12 +66,47 @@ def initialize_settings(self):
)

def initialize_handlers(self):
# Set configurable parameters to YStore class
for k, v in self.config.get(self.ystore_class.__name__, {}).items():
setattr(self.ystore_class, k, v)

self.ywebsocket_server = JupyterWebsocketServer(
rooms_ready=False,
auto_clean_rooms=False,
ystore_class=self.ystore_class,
log=self.log,
)

# self.settings is local to the ExtensionApp but here we need
# the global app settings in which the file id manager will register
# itself maybe at a later time.
self.file_loaders = FileLoaderMapping(
self.serverapp.web_app.settings, self.log, self.file_poll_interval
)

self.handlers.extend(
[
(r"/api/collaboration/room/(.*)", YDocWebSocketHandler),
(
r"/api/collaboration/room/(.*)",
YDocWebSocketHandler,
{
"document_cleanup_delay": self.document_cleanup_delay,
"document_save_delay": self.document_save_delay,
"file_loaders": self.file_loaders,
"ystore_class": self.ystore_class,
"ywebsocket_server": self.ywebsocket_server,
},
),
(r"/api/collaboration/session/(.*)", DocSessionHandler),
]
)

async def stop_extension(self):
YDocWebSocketHandler.clean_up()
# Cancel tasks and clean up
await asyncio.wait(
[
asyncio.create_task(self.ywebsocket_server.clean()),
asyncio.create_task(self.file_loaders.clear()),
],
timeout=3,
)
Loading

0 comments on commit 94fcd92

Please sign in to comment.