-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
yprovider.ts
201 lines (171 loc) · 5.12 KB
/
yprovider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { showErrorMessage, Dialog } from '@jupyterlab/apputils';
import { User } from '@jupyterlab/services';
import { TranslationBundle } from '@jupyterlab/translation';
import { PromiseDelegate } from '@lumino/coreutils';
import { IDisposable } from '@lumino/disposable';
import { Signal } from '@lumino/signaling';
import { DocumentChange, YDocument } from '@jupyter/ydoc';
import { Awareness } from 'y-protocols/awareness';
import { WebsocketProvider as YWebsocketProvider } from 'y-websocket';
import { requestDocSession } from './requests';
/**
* An interface for a document provider.
*/
export interface IDocumentProvider extends IDisposable {
/**
* Returns a Promise that resolves when the document provider is ready.
*/
readonly ready: Promise<void>;
}
/**
* A class to provide Yjs synchronization over WebSocket.
*
* We specify custom messages that the server can interpret. For reference please look in yjs_ws_server.
*
*/
export class WebSocketProvider implements IDocumentProvider {
/**
* Construct a new WebSocketProvider
*
* @param options The instantiation options for a WebSocketProvider
*/
constructor(options: WebSocketProvider.IOptions) {
this._isDisposed = false;
this._path = options.path;
this._contentType = options.contentType;
this._format = options.format;
this._serverUrl = options.url;
this._sharedModel = options.model;
this._awareness = options.model.awareness;
this._yWebsocketProvider = null;
this._trans = options.translator;
const user = options.user;
user.ready
.then(() => {
this._onUserChanged(user);
})
.catch(e => console.error(e));
user.userChanged.connect(this._onUserChanged, this);
this._connect().catch(e => console.warn(e));
}
/**
* Test whether the object has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* A promise that resolves when the document provider is ready.
*/
get ready(): Promise<void> {
return this._ready.promise;
}
/**
* Dispose of the resources held by the object.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
this._yWebsocketProvider?.off('connection-close', this._onConnectionClosed);
this._yWebsocketProvider?.off('sync', this._onSync);
this._yWebsocketProvider?.destroy();
Signal.clearData(this);
}
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 {
this._awareness.setLocalStateField('user', user.identity);
}
private _onConnectionClosed = (event: any): void => {
if (event.code === 1003) {
console.error('Document provider closed:', event.reason);
showErrorMessage(this._trans.__('Document session error'), event.reason, [
Dialog.okButton()
]);
// Dispose shared model immediately. Better break the document model,
// than overriding data on disk.
this._sharedModel.dispose();
}
};
private _onSync = (isSynced: boolean) => {
if (isSynced) {
if (this._yWebsocketProvider) {
this._yWebsocketProvider.off('sync', this._onSync);
const state = this._sharedModel.ydoc.getMap('state');
state.set('document_id', this._yWebsocketProvider.roomname);
}
this._ready.resolve();
}
};
private _awareness: Awareness;
private _contentType: string;
private _format: string;
private _isDisposed: boolean;
private _path: string;
private _ready = new PromiseDelegate<void>();
private _serverUrl: string;
private _sharedModel: YDocument<DocumentChange>;
private _yWebsocketProvider: YWebsocketProvider | null;
private _trans: TranslationBundle;
}
/**
* A namespace for WebSocketProvider statics.
*/
export namespace WebSocketProvider {
/**
* The instantiation options for a WebSocketProvider.
*/
export interface IOptions {
/**
* The server URL
*/
url: string;
/**
* The document file path
*/
path: string;
/**
* Content type
*/
contentType: string;
/**
* The source format
*/
format: string;
/**
* The shared model
*/
model: YDocument<DocumentChange>;
/**
* The user data
*/
user: User.IManager;
/**
* The jupyterlab translator
*/
translator: TranslationBundle;
}
}