@jupyter/docprovider 1.0.0 → 1.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/awareness.d.ts +68 -0
- package/lib/awareness.js +75 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/tokens.d.ts +24 -0
- package/lib/ydrive.js +4 -3
- package/package.json +1 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { User } from '@jupyterlab/services';
|
|
2
|
+
import { IDisposable } from '@lumino/disposable';
|
|
3
|
+
import { ISignal } from '@lumino/signaling';
|
|
4
|
+
import { WebsocketProvider } from 'y-websocket';
|
|
5
|
+
import { IAwareness, IAwarenessProvider } from './tokens';
|
|
6
|
+
export declare enum MessageType {
|
|
7
|
+
CHAT = 125
|
|
8
|
+
}
|
|
9
|
+
export interface IChatMessage {
|
|
10
|
+
username: string;
|
|
11
|
+
msg: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A class to provide Yjs synchronization over WebSocket.
|
|
15
|
+
*
|
|
16
|
+
* We specify custom messages that the server can interpret. For reference please look in yjs_ws_server.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
export declare class WebSocketAwarenessProvider extends WebsocketProvider implements IAwarenessProvider, IDisposable {
|
|
20
|
+
/**
|
|
21
|
+
* Construct a new WebSocketAwarenessProvider
|
|
22
|
+
*
|
|
23
|
+
* @param options The instantiation options for a WebSocketAwarenessProvider
|
|
24
|
+
*/
|
|
25
|
+
constructor(options: WebSocketAwarenessProvider.IOptions);
|
|
26
|
+
get isDisposed(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* A signal to subscribe for incoming messages.
|
|
29
|
+
*/
|
|
30
|
+
get chatMessage(): ISignal<this, IChatMessage>;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Send a message to every collaborator.
|
|
34
|
+
*
|
|
35
|
+
* @param msg message
|
|
36
|
+
*/
|
|
37
|
+
sendMessage(msg: string): void;
|
|
38
|
+
private _onUserChanged;
|
|
39
|
+
private _isDisposed;
|
|
40
|
+
private _awareness;
|
|
41
|
+
private _chatMessage;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A namespace for WebSocketAwarenessProvider statics.
|
|
45
|
+
*/
|
|
46
|
+
export declare namespace WebSocketAwarenessProvider {
|
|
47
|
+
/**
|
|
48
|
+
* The instantiation options for a WebSocketAwarenessProvider.
|
|
49
|
+
*/
|
|
50
|
+
interface IOptions {
|
|
51
|
+
/**
|
|
52
|
+
* The server URL
|
|
53
|
+
*/
|
|
54
|
+
url: string;
|
|
55
|
+
/**
|
|
56
|
+
* The room ID
|
|
57
|
+
*/
|
|
58
|
+
roomID: string;
|
|
59
|
+
/**
|
|
60
|
+
* The awareness object
|
|
61
|
+
*/
|
|
62
|
+
awareness: IAwareness;
|
|
63
|
+
/**
|
|
64
|
+
* The user data
|
|
65
|
+
*/
|
|
66
|
+
user: User.IManager;
|
|
67
|
+
}
|
|
68
|
+
}
|
package/lib/awareness.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/* -----------------------------------------------------------------------------
|
|
2
|
+
| Copyright (c) Jupyter Development Team.
|
|
3
|
+
| Distributed under the terms of the Modified BSD License.
|
|
4
|
+
|----------------------------------------------------------------------------*/
|
|
5
|
+
import { Signal } from '@lumino/signaling';
|
|
6
|
+
import * as decoding from 'lib0/decoding';
|
|
7
|
+
import * as encoding from 'lib0/encoding';
|
|
8
|
+
import { WebsocketProvider } from 'y-websocket';
|
|
9
|
+
export var MessageType;
|
|
10
|
+
(function (MessageType) {
|
|
11
|
+
MessageType[MessageType["CHAT"] = 125] = "CHAT";
|
|
12
|
+
})(MessageType || (MessageType = {}));
|
|
13
|
+
/**
|
|
14
|
+
* A class to provide Yjs synchronization over WebSocket.
|
|
15
|
+
*
|
|
16
|
+
* We specify custom messages that the server can interpret. For reference please look in yjs_ws_server.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
export class WebSocketAwarenessProvider extends WebsocketProvider {
|
|
20
|
+
/**
|
|
21
|
+
* Construct a new WebSocketAwarenessProvider
|
|
22
|
+
*
|
|
23
|
+
* @param options The instantiation options for a WebSocketAwarenessProvider
|
|
24
|
+
*/
|
|
25
|
+
constructor(options) {
|
|
26
|
+
super(options.url, options.roomID, options.awareness.doc, {
|
|
27
|
+
awareness: options.awareness
|
|
28
|
+
});
|
|
29
|
+
this._isDisposed = false;
|
|
30
|
+
this._awareness = options.awareness;
|
|
31
|
+
const user = options.user;
|
|
32
|
+
user.ready
|
|
33
|
+
.then(() => this._onUserChanged(user))
|
|
34
|
+
.catch(e => console.error(e));
|
|
35
|
+
user.userChanged.connect(this._onUserChanged, this);
|
|
36
|
+
this._chatMessage = new Signal(this);
|
|
37
|
+
this.messageHandlers[MessageType.CHAT] = (encoder, decoder, provider, emitSynced, messageType) => {
|
|
38
|
+
const content = decoding.readVarString(decoder);
|
|
39
|
+
const data = JSON.parse(content);
|
|
40
|
+
console.debug('Chat:', data);
|
|
41
|
+
this._chatMessage.emit(data);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
get isDisposed() {
|
|
45
|
+
return this._isDisposed;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A signal to subscribe for incoming messages.
|
|
49
|
+
*/
|
|
50
|
+
get chatMessage() {
|
|
51
|
+
return this._chatMessage;
|
|
52
|
+
}
|
|
53
|
+
dispose() {
|
|
54
|
+
if (this._isDisposed) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this.destroy();
|
|
58
|
+
this._isDisposed = true;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Send a message to every collaborator.
|
|
62
|
+
*
|
|
63
|
+
* @param msg message
|
|
64
|
+
*/
|
|
65
|
+
sendMessage(msg) {
|
|
66
|
+
console.debug('Send message:', msg);
|
|
67
|
+
const encoder = encoding.createEncoder();
|
|
68
|
+
encoding.writeVarUint(encoder, MessageType.CHAT);
|
|
69
|
+
encoding.writeVarString(encoder, msg);
|
|
70
|
+
this.ws.send(encoding.toUint8Array(encoder));
|
|
71
|
+
}
|
|
72
|
+
_onUserChanged(user) {
|
|
73
|
+
this._awareness.setLocalStateField('user', user.identity);
|
|
74
|
+
}
|
|
75
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/tokens.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { DocumentChange, YDocument } from '@jupyter/ydoc';
|
|
2
2
|
import { Contents } from '@jupyterlab/services';
|
|
3
3
|
import { Token } from '@lumino/coreutils';
|
|
4
|
+
import { ISignal } from '@lumino/signaling';
|
|
5
|
+
import type { Awareness } from 'y-protocols/awareness';
|
|
6
|
+
import { IChatMessage } from './awareness';
|
|
4
7
|
/**
|
|
5
8
|
* The collaborative drive.
|
|
6
9
|
*/
|
|
@@ -31,3 +34,24 @@ export interface ISharedModelFactory extends Contents.ISharedFactory {
|
|
|
31
34
|
*/
|
|
32
35
|
registerDocumentFactory(type: Contents.ContentType, factory: SharedDocumentFactory): void;
|
|
33
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* The awareness interface.
|
|
39
|
+
*
|
|
40
|
+
* TODO: Move to @jupyter/YDoc
|
|
41
|
+
*/
|
|
42
|
+
export type IAwareness = Awareness;
|
|
43
|
+
/**
|
|
44
|
+
* A provider interface for global awareness features.
|
|
45
|
+
*/
|
|
46
|
+
export interface IAwarenessProvider {
|
|
47
|
+
/**
|
|
48
|
+
* A signal to subscribe for incoming messages.
|
|
49
|
+
*/
|
|
50
|
+
get chatMessage(): ISignal<this, IChatMessage>;
|
|
51
|
+
/**
|
|
52
|
+
* Send a message to every collaborator.
|
|
53
|
+
*
|
|
54
|
+
* @param msg message
|
|
55
|
+
*/
|
|
56
|
+
sendMessage(msg: string): void;
|
|
57
|
+
}
|
package/lib/ydrive.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// Copyright (c) Jupyter Development Team.
|
|
2
2
|
// Distributed under the terms of the Modified BSD License.
|
|
3
|
-
import { URLExt } from '@jupyterlab/coreutils';
|
|
3
|
+
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
4
4
|
import { Drive } from '@jupyterlab/services';
|
|
5
5
|
import { WebSocketProvider } from './yprovider';
|
|
6
|
+
const DISABLE_RTC = PageConfig.getOption('disableRTC') === 'true' ? true : false;
|
|
6
7
|
/**
|
|
7
8
|
* The url for the default drive service.
|
|
8
9
|
*/
|
|
@@ -125,7 +126,7 @@ class SharedModelFactory {
|
|
|
125
126
|
/**
|
|
126
127
|
* Whether the IDrive supports real-time collaboration or not.
|
|
127
128
|
*/
|
|
128
|
-
this.collaborative =
|
|
129
|
+
this.collaborative = !DISABLE_RTC;
|
|
129
130
|
this._documentFactories = new Map();
|
|
130
131
|
}
|
|
131
132
|
/**
|
|
@@ -150,7 +151,7 @@ class SharedModelFactory {
|
|
|
150
151
|
console.warn(`Only defined format are supported; got ${options.format}.`);
|
|
151
152
|
return;
|
|
152
153
|
}
|
|
153
|
-
if (!options.collaborative) {
|
|
154
|
+
if (!this.collaborative || !options.collaborative) {
|
|
154
155
|
// Bail if the document model does not support collaboration
|
|
155
156
|
// the `sharedModel` will be the default one.
|
|
156
157
|
return;
|