Skip to content

Commit

Permalink
Migrate to @jupyter/ydoc and fix observe (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbcarlos authored Dec 16, 2022
1 parent f172644 commit 7d5af28
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@deathbeds/jupyterlab-rjsf": "^1.1.0",
"@jupyter-notebook/ydoc": "^0.2.0",
"@jupyter/ydoc": "^0.2.0",
"@jupyterlab/application": "^4.0.0-alpha.16",
"@jupyterlab/apputils": "^4.0.0-alpha.16",
"@jupyterlab/collaboration": "^4.0.0-alpha.16",
Expand Down
2 changes: 1 addition & 1 deletion src/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {

import { ContextMenu } from '@lumino/widgets';
import { CommandRegistry } from '@lumino/commands';
import { MapChange } from '@jupyter-notebook/ydoc';
import { MapChange } from '@jupyter/ydoc';
import { Annotation } from './annotation/view';
import { AnnotationModel } from './annotation/model';

Expand Down
28 changes: 7 additions & 21 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IChangedArgs } from '@jupyterlab/coreutils';
import { IModelDB, ModelDB } from '@jupyterlab/observables';
import { MapChange, YDocument } from '@jupyter-notebook/ydoc';
import { MapChange, YDocument } from '@jupyter/ydoc';
import { PartialJSONObject } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import Ajv from 'ajv';
Expand Down Expand Up @@ -241,18 +241,17 @@ export class JupyterCadDoc

this._options = this.ydoc.getMap<any>('options');
this._objects = this.ydoc.getArray<IJCadObjectDoc>('objects');
this._metadata = this.ydoc.getMap<string>('metadata');

this.undoManager.addToScope(this._objects);

this._objects.observe(this._objectsObserver);

this._metadata = this.ydoc.getMap<string>('metadata');
this._objects.observeDeep(this._objectsObserver);
this._metadata.observe(this._metaObserver);
}

dispose(): void {
// this._objects.unobserve(this._objectsObserver);
// this._options.unobserve(this._optionsObserver);
this._objects.unobserveDeep(this._objectsObserver);
this._metadata.unobserve(this._metaObserver);
}

get objects(): Y.Array<IJCadObjectDoc> {
Expand Down Expand Up @@ -352,27 +351,14 @@ export class JupyterCadDoc
return new JupyterCadDoc();
}

private _objectsObserver = (event: Y.YArrayEvent<IJCadObjectDoc>): void => {
event.changes.added.forEach(item => {
const type = (item.content as Y.ContentType).type as Y.Map<any>;
type.observe(this.emitChange);
});
event.changes.deleted.forEach(item => {
const type = (item.content as Y.ContentType).type as Y.Map<any>;
type.unobserve(this.emitChange);
});
const objectChange = [];
this._changed.emit({ objectChange });
private _objectsObserver = (event: Y.YEvent<any>[]): void => {
this._changed.emit({ objectChange: [] });
};

private _metaObserver = (event: Y.YMapEvent<string>): void => {
this._metadataChanged.emit(event.keys);
};

private emitChange = () => {
this._changed.emit({});
};

private _objects: Y.Array<IJCadObjectDoc>;
private _options: Y.Map<any>;
private _metadata: Y.Map<string>;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IJCadObject } from './_interface/jcad.d';
import { IChangedArgs } from '@jupyterlab/coreutils';
import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
import { MapChange, YDocument, StateChange } from '@jupyter-notebook/ydoc';
import { MapChange, YDocument, StateChange } from '@jupyter/ydoc';
import { ReactWidget } from '@jupyterlab/ui-components';
import { User } from '@jupyterlab/services';
import { ISignal, Signal } from '@lumino/signaling';
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.4.0.tgz#08d6c5e20cf7e4cc02fd181c4b0c225cd31dbb60"
integrity sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA==

"@jupyter-notebook/ydoc@^0.2.0", "@jupyter-notebook/ydoc@~0.2.0":
"@jupyter-notebook/ydoc@~0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@jupyter-notebook/ydoc/-/ydoc-0.2.0.tgz#0febf91cd80dd2206f668c60e8ee20f4983cca99"
integrity sha512-R8iHKwU+8OvdYwtgNj9dh3vv0OrAyHLNDmXjaSu6CJ1w10OP7MuQasvD18DAgTjqlHEUgnPoSJA2j0UlflSC1Q==
Expand All @@ -541,6 +541,18 @@
y-protocols "^1.0.5"
yjs "^13.5.40"

"@jupyter/ydoc@^0.2.0":
version "0.2.2"
resolved "https://registry.yarnpkg.com/@jupyter/ydoc/-/ydoc-0.2.2.tgz#a2be83d2a0e076cef7ed77302e69153a0a4d6c16"
integrity sha512-UtU7ZxpL0k+QF9So4wtGxaS2C+nno58dig7sQUaBn48wlQDiuypzKgUmF7I37srpu6f/ywon3JBuEjxuL1CIBQ==
dependencies:
"@jupyterlab/nbformat" "^3.0.0 || ^4.0.0-alpha.15"
"@lumino/coreutils" "^1.11.0 || ^2.0.0-alpha.6"
"@lumino/disposable" "^1.10.0 || ^2.0.0-alpha.6"
"@lumino/signaling" "^1.10.0 || ^2.0.0-alpha.6"
y-protocols "^1.0.5"
yjs "^13.5.40"

"@jupyterlab/application@^4.0.0-alpha.16":
version "4.0.0-alpha.16"
resolved "https://registry.yarnpkg.com/@jupyterlab/application/-/application-4.0.0-alpha.16.tgz#737f5875115fecddcd5b2b2f45114b35390b09f7"
Expand Down

0 comments on commit 7d5af28

Please sign in to comment.