Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for the document to be synced #148

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/source/developer/contributing.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.. Copyright (c) Jupyter Development Team.
.. Distributed under the terms of the Modified BSD License.

Developer documentation
=======================

Expand Down
3 changes: 3 additions & 0 deletions jupyter_collaboration/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from typing import Any, Dict, List

from ._version import __version__ # noqa
Expand Down
3 changes: 3 additions & 0 deletions jupyter_collaboration/loaders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import annotations

import asyncio
Expand Down
3 changes: 3 additions & 0 deletions jupyter_collaboration/rooms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import annotations

import asyncio
Expand Down
3 changes: 3 additions & 0 deletions jupyter_collaboration/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import pathlib
from enum import Enum
from typing import Tuple
Expand Down
5 changes: 5 additions & 0 deletions packages/collaboration-extension/src/filebrowser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/

import {
ILabShell,
IRouter,
Expand Down
15 changes: 15 additions & 0 deletions packages/docprovider/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@ import { ServerConnection, Contents } from '@jupyterlab/services';
*/
const DOC_SESSION_URL = 'api/collaboration/session';

/**
* Document session model
*/
export interface ISessionModel {
/**
* Document format; 'text', 'base64',...
*/
format: Contents.FileFormat;
/**
* Document type
*/
type: Contents.ContentType;
/**
* File unique identifier
*/
fileId: string;
/**
* Server session identifier
*/
sessionId: string;
}

Expand Down
54 changes: 31 additions & 23 deletions packages/docprovider/src/yprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DocumentChange, YDocument } from '@jupyter/ydoc';
import { Awareness } from 'y-protocols/awareness';
import { WebsocketProvider as YWebsocketProvider } from 'y-websocket';

import { ISessionModel, requestDocSession } from './requests';
import { requestDocSession } from './requests';

/**
* An interface for a document provider.
Expand Down Expand Up @@ -60,7 +60,7 @@ export class WebSocketProvider implements IDocumentProvider {
.catch(e => console.error(e));
user.userChanged.connect(this._onUserChanged, this);

this._connect();
this._connect().catch(e => console.warn(e));
}

/**
Expand All @@ -86,31 +86,31 @@ export class WebSocketProvider implements IDocumentProvider {
}
this._isDisposed = true;
this._yWebsocketProvider?.off('connection-close', this._onConnectionClosed);
this._yWebsocketProvider?.off('sync', this._onSync);
this._yWebsocketProvider?.destroy();
Signal.clearData(this);
}

private _connect(): void {
requestDocSession(this._format, this._contentType, this._path)
.then((session: ISessionModel) => {
this._yWebsocketProvider = new YWebsocketProvider(
this._serverUrl,
`${session.format}:${session.type}:${session.fileId}`,
this._sharedModel.ydoc,
{
disableBc: true,
params: { sessionId: session.sessionId },
awareness: this._awareness
}
);

this._yWebsocketProvider.on(
'connection-close',
this._onConnectionClosed
);
})
.then(r => this._ready.resolve())
.catch(e => console.warn(e));
private async _connect(): Promise<void> {
const session = await requestDocSession(
this._format,
this._contentType,
this._path
);

this._yWebsocketProvider = new YWebsocketProvider(
this._serverUrl,
`${session.format}:${session.type}:${session.fileId}`,
this._sharedModel.ydoc,
{
disableBc: true,
params: { sessionId: session.sessionId },
awareness: this._awareness
}
);

this._yWebsocketProvider.on('sync', this._onSync);
this._yWebsocketProvider.on('connection-close', this._onConnectionClosed);
}

private _onUserChanged(user: User.IManager): void {
Expand Down Expand Up @@ -141,6 +141,14 @@ export class WebSocketProvider implements IDocumentProvider {
}
};

private _onSync = (isSynced: boolean) => {
console.log(`_onSync ${isSynced}`);
if (isSynced) {
this._ready.resolve();
this._yWebsocketProvider?.off('sync', this._onSync);
}
};

private _awareness: Awareness;
private _contentType: string;
private _format: string;
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

[build-system]
build-backend = "hatchling.build"
requires = ["hatchling>=1.4.0", "hatch-nodejs-version", "jupyterlab>=4.0.0rc0"]
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

# setup.py shim for use with applications that require it.
__import__("setuptools").setup()
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

pytest_plugins = ["jupyter_server.pytest_plugin"]
3 changes: 3 additions & 0 deletions tests/test_ydoc.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

def test_ydoc():
pass