Skip to content

Commit 2ac1866

Browse files
committed
do not create a new NotebookModel on reset
1 parent c557ee3 commit 2ac1866

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/notebook-factory/notebook.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { INotebookModel, Notebook, NotebookModel } from '@jupyterlab/notebook';
1+
import { INotebookModel, Notebook } from '@jupyterlab/notebook';
22
import { YNotebook } from '../docprovider/custom_ydocs';
33

44
/**
@@ -17,33 +17,30 @@ export class ResettableNotebook extends Notebook {
1717
return super.model;
1818
}
1919

20-
set model(newValue: INotebookModel | null) {
21-
// if current model exists, remove the `resetSignal` observer
22-
if (this.model) {
23-
const ynotebook = this.model.sharedModel as YNotebook;
20+
set model(newModel: INotebookModel | null) {
21+
// if there is an existing model, remove the `resetSignal` observer
22+
const oldModel = this.model;
23+
if (oldModel) {
24+
const ynotebook = oldModel.sharedModel as YNotebook;
2425
ynotebook.resetSignal.disconnect(this._resetSignalSlot);
2526
}
2627

2728
// call parent property setter
28-
super.model = newValue;
29+
super.model = newModel;
2930

3031
// return early if `newValue === null`
31-
if (!newValue) {
32+
if (!newModel) {
3233
return;
3334
}
3435

3536
// otherwise, listen to `YNotebook.resetSignal`.
36-
const ynotebook = newValue.sharedModel as YNotebook;
37+
const ynotebook = newModel.sharedModel as YNotebook;
3738
ynotebook.resetSignal.connect(this._resetSignalSlot);
3839
}
3940

4041
/**
41-
* Function called when the YDoc has been reset. This recreates the notebook
42-
* model using this model's options.
43-
*
44-
* TODO (?): we may want to use NotebookModelFactory, but that factory only
45-
* seems to set some configuration options. The NotebookModel constructor
46-
* does not require any arguments so this is OK for now.
42+
* Function called when the YDoc has been reset. This simply refreshes the UI
43+
* to reflect the new YDoc state.
4744
*/
4845
_onReset() {
4946
if (!this.model) {
@@ -53,12 +50,8 @@ export class ResettableNotebook extends Notebook {
5350
return;
5451
}
5552

56-
this.model = new NotebookModel({
57-
collaborationEnabled: this.model.collaborative,
58-
sharedModel: this.model.sharedModel
59-
// other options in `NotebookModel.IOptions` are either unused or
60-
// forwarded to `YNotebook`, which is preserved here
61-
});
53+
// Refresh the UI by emitting to the `modelContentChanged` signal
54+
this.onModelContentChanged(this.model);
6255
}
6356

6457
_resetSignalSlot: () => void;

0 commit comments

Comments
 (0)