|
| 1 | +/* ----------------------------------------------------------------------------- |
| 2 | +| Copyright (c) Jupyter Development Team. |
| 3 | +| Distributed under the terms of the Modified BSD License. |
| 4 | +|----------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { User } from '@jupyterlab/services'; |
| 7 | + |
| 8 | +import { IDisposable } from '@lumino/disposable'; |
| 9 | + |
| 10 | +import { IAwareness } from '@jupyter/ydoc'; |
| 11 | + |
| 12 | +import { WebsocketProvider } from 'y-websocket'; |
| 13 | + |
| 14 | +export interface IContent { |
| 15 | + type: string; |
| 16 | + body: string; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * A class to provide Yjs synchronization over WebSocket. |
| 21 | + * |
| 22 | + * We specify custom messages that the server can interpret. For reference please look in yjs_ws_server. |
| 23 | + * |
| 24 | + */ |
| 25 | +export class WebSocketAwarenessProvider |
| 26 | + extends WebsocketProvider |
| 27 | + implements IDisposable |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Construct a new WebSocketAwarenessProvider |
| 31 | + * |
| 32 | + * @param options The instantiation options for a WebSocketAwarenessProvider |
| 33 | + */ |
| 34 | + constructor(options: WebSocketAwarenessProvider.IOptions) { |
| 35 | + super(options.url, options.roomID, options.awareness.doc, { |
| 36 | + awareness: options.awareness |
| 37 | + }); |
| 38 | + |
| 39 | + this._awareness = options.awareness; |
| 40 | + |
| 41 | + this._user = options.user; |
| 42 | + this._user.ready |
| 43 | + .then(() => this._onUserChanged(this._user)) |
| 44 | + .catch(e => console.error(e)); |
| 45 | + this._user.userChanged.connect(this._onUserChanged, this); |
| 46 | + } |
| 47 | + |
| 48 | + get isDisposed(): boolean { |
| 49 | + return this._isDisposed; |
| 50 | + } |
| 51 | + |
| 52 | + dispose(): void { |
| 53 | + if (this._isDisposed) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + this._user.userChanged.disconnect(this._onUserChanged, this); |
| 58 | + this._isDisposed = true; |
| 59 | + this.destroy(); |
| 60 | + } |
| 61 | + |
| 62 | + private _onUserChanged(user: User.IManager): void { |
| 63 | + this._awareness.setLocalStateField('user', user.identity); |
| 64 | + } |
| 65 | + |
| 66 | + private _isDisposed = false; |
| 67 | + private _user: User.IManager; |
| 68 | + private _awareness: IAwareness; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * A namespace for WebSocketAwarenessProvider statics. |
| 73 | + */ |
| 74 | +export namespace WebSocketAwarenessProvider { |
| 75 | + /** |
| 76 | + * The instantiation options for a WebSocketAwarenessProvider. |
| 77 | + */ |
| 78 | + export interface IOptions { |
| 79 | + /** |
| 80 | + * The server URL |
| 81 | + */ |
| 82 | + url: string; |
| 83 | + |
| 84 | + /** |
| 85 | + * The room ID |
| 86 | + */ |
| 87 | + roomID: string; |
| 88 | + |
| 89 | + /** |
| 90 | + * The awareness object |
| 91 | + */ |
| 92 | + awareness: IAwareness; |
| 93 | + |
| 94 | + /** |
| 95 | + * The user data |
| 96 | + */ |
| 97 | + user: User.IManager; |
| 98 | + } |
| 99 | +} |
0 commit comments