Skip to content

Commit

Permalink
Simplify signals and add shared attribute executed to track whether t…
Browse files Browse the repository at this point in the history
…he notebook is already executed or not.
  • Loading branch information
hbcarlos committed Aug 12, 2021
1 parent 6867200 commit b0a6722
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = {
],
'@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
'@typescript-eslint/no-explicit-any': 'off',
// TODO: Remove ban-ts-comment
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-use-before-define': 'off',
Expand Down
10 changes: 5 additions & 5 deletions packages/jupyterlab-gridstack/src/editor/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ export type DashboardCellView = {
};

/**
* Signal argument to lock/unlock a GridstackItem.
* Signal argument for cell change.
*/
export type LockSignal = {
export type CellChange = {
/**
* Cell id.
*/
cellId: string;
id: string;
/**
* Action.
* Cell view info.
*/
lock: boolean;
info: DashboardCellView;
};

/**
Expand Down
96 changes: 50 additions & 46 deletions packages/jupyterlab-gridstack/src/editor/gridstack/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ import { SimplifiedOutputArea } from '@jupyterlab/outputarea';

import { IObservableUndoableList } from '@jupyterlab/observables';

import { createMutex } from '@jupyterlab/shared-models';
import { YNotebook, createMutex } from '@jupyterlab/shared-models';

import { Widget } from '@lumino/widgets';

import { Signal, ISignal } from '@lumino/signaling';

import * as Y from 'yjs';

import { GridStackItemWidget, GridStackItemModel, ItemState } from '../item';

import {
DashboardView,
DashboardCellView,
validateDashboardView,
validateDashboardCellView,
LockSignal
CellChange
} from '../format';

export const VIEW = 'grid_default';
Expand All @@ -60,8 +62,8 @@ export class GridStackModel {
this._notebookConfig = options.notebookConfig;

this._ready = new Signal<this, null>(this);
this._cellRemoved = new Signal<this, string>(this);
this._cellPinned = new Signal<this, LockSignal>(this);
this._cellRemoved = new Signal<this, CellChange>(this);
this._cellPinned = new Signal<this, CellChange>(this);
this._stateChanged = new Signal<this, null>(this);
this._contentChanged = new Signal<this, null>(this);

Expand All @@ -70,27 +72,29 @@ export class GridStackModel {
type: 'grid',
maxColumns: 12,
cellMargin: 2,
defaultCellHeight: 60
defaultCellHeight: 40
};

this._context.sessionContext.ready.then(() => {
this._checkMetadata();
this._checkCellsMetadata();

const ymodel = this._context.model.sharedModel as YNotebook;
// TODO: Remove @ts-ignore
// @ts-ignore
this._ystate = ymodel.ystate;
if (this._ystate.get('executed') !== true) {
ymodel.transact(() => {
this._ystate.set('executed', false);
}, false);
}

this._context.save().then(v => {
this._ready.emit(null);
});
});

this._context.model.contentChanged.connect(() => {
console.debug('Content changed');
this._updateCells();
}, this);
this._context.model.stateChanged.connect(() => {
console.debug('State changed');
this._updateCells();
}, this);
this._context.model.sharedModel.changed.connect(() => {
console.debug('Shared model changed');
this._updateCells();
}, this);
}
Expand All @@ -105,14 +109,14 @@ export class GridStackModel {
/**
* A signal emitted when a cell is removed.
*/
get cellRemoved(): ISignal<this, string> {
get cellRemoved(): ISignal<this, CellChange> {
return this._cellRemoved;
}

/**
* A signal emitted when a cell pinned.
*/
get cellPinned(): ISignal<this, LockSignal> {
get cellPinned(): ISignal<this, CellChange> {
return this._cellPinned;
}

Expand Down Expand Up @@ -173,6 +177,10 @@ export class GridStackModel {
this._notebookConfig = value;
}

set executed(value: boolean) {
this._ystate.set('executed', value);
}

/**
* Getter for the dashboard metadata info.
*/
Expand All @@ -197,7 +205,7 @@ export class GridStackModel {
(data as Record<string, any>).extensions.jupyter_dashboards.views[
VIEW
] = this._info;
//this._context.model.metadata.set('extensions', data);
//this._context.model.metadata.set('extensions', data.extensions);
this._context.model.sharedModel.setMetadata(data);
this._context.model.dirty = true;
});
Expand Down Expand Up @@ -227,7 +235,6 @@ export class GridStackModel {
const cell = this._context.model.cells.get(i);

if (cell.id === id) {
//const data = cell.metadata.get('extensions') as Record<string, any>;
const data = cell.sharedModel.getMetadata().extensions as Record<
string,
any
Expand All @@ -251,13 +258,11 @@ export class GridStackModel {

if (cell.id === id) {
this._mutex(() => {
//const data = cell.metadata.get('extensions') as Record<string, any>;
const data = cell.sharedModel.getMetadata().extensions as Record<
string,
any
>;
data.jupyter_dashboards.views[VIEW] = info;
//cell.metadata.set('extensions', data);
cell.sharedModel.setMetadata({ extensions: data });
this._context.model.dirty = true;
});
Expand All @@ -277,15 +282,17 @@ export class GridStackModel {

if (cell.id === id) {
this._mutex(() => {
//const data = cell.metadata.get('extensions') as Record<string, any>;
const data = cell.sharedModel.getMetadata().extensions as Record<
string,
any
>;
data.jupyter_dashboards.views[VIEW].hidden = true;
//cell.metadata.set('extensions', data);
cell.sharedModel.setMetadata({ extensions: data });
this._context.model.dirty = true;
this._cellRemoved.emit({
id,
info: data.jupyter_dashboards.views[VIEW]
});
});
break;
}
Expand All @@ -303,15 +310,17 @@ export class GridStackModel {

if (cell.id === id) {
this._mutex(() => {
//const data = cell.metadata.get('extensions') as Record<string, any>;
const data = cell.sharedModel.getMetadata().extensions as Record<
string,
any
>;
data.jupyter_dashboards.views[VIEW].locked = lock;
//cell.metadata.set('extensions', data);
cell.sharedModel.setMetadata({ extensions: data });
this._context.model.dirty = true;
this._cellPinned.emit({
id,
info: data.jupyter_dashboards.views[VIEW]
});
});
break;
}
Expand Down Expand Up @@ -394,7 +403,7 @@ export class GridStackModel {
* @param cell - `ICellModel`.
*/
public execute(cell: ICellModel): void {
if (cell.type !== 'code') {
if (cell.type !== 'code' || this._ystate.get('executed')) {
return;
}

Expand All @@ -410,7 +419,16 @@ export class GridStackModel {
cell.value.text,
codeCell.outputArea,
this._context.sessionContext
).catch(reason => console.error(reason));
)
.then(resp => {
if (
resp?.header.msg_type === 'execute_reply' &&
resp.content.status === 'ok'
) {
(cell as CodeCellModel).executionCount = resp.content.execution_count;
}
})
.catch(reason => console.error(reason));
}

private readonly _mutex = createMutex();
Expand All @@ -429,11 +447,6 @@ export class GridStackModel {
* Check the dashboard notebook's metadata.
*/
private _checkMetadata(): void {
/* let data = this._context.model.metadata.get('extensions') as Record<
string,
any
>; */

let data = this._context.model.sharedModel.getMetadata()
.extensions as Record<string, any>;

Expand Down Expand Up @@ -461,8 +474,6 @@ export class GridStackModel {
this._info = data.jupyter_dashboards?.views[VIEW] as DashboardView;
}

//this._context.model.metadata.set('extensions', data);
console.debug('Metadata:', data);
this._mutex(() => {
this._context.model.sharedModel.updateMetadata({ extensions: data });
});
Expand All @@ -482,19 +493,16 @@ export class GridStackModel {
switch (change) {
case ItemState.CLOSED:
this.hideCell(item.cellId);
this._cellRemoved.emit(item.cellId);
item.stateChanged.disconnect(this._itemChanged);
item.dispose();
break;

case ItemState.LOCKED:
this.lockCell(item.cellId, true);
this._cellPinned.emit({ cellId: item.cellId, lock: true });
break;

case ItemState.UNLOCKED:
this.lockCell(item.cellId, false);
this._cellPinned.emit({ cellId: item.cellId, lock: false });
break;

default:
Expand All @@ -508,7 +516,6 @@ export class GridStackModel {
* @param cell - `ICellModel`.
*/
private _checkCellMetadata(cell: ICellModel): void {
//let data = cell.metadata.get('extensions') as Record<string, any>;
let data = cell.sharedModel.getMetadata().extensions as Record<string, any>;

if (!data) {
Expand All @@ -522,12 +529,11 @@ export class GridStackModel {
col: null,
width: 2,
height: 2,
locked: false
locked: true
}
}
}
};
//cell.metadata.set('extensions', data);
this._mutex(() => {
cell.sharedModel.setMetadata({ extensions: data });
});
Expand All @@ -541,11 +547,10 @@ export class GridStackModel {
col: null,
width: 2,
height: 2,
locked: false
locked: true
}
}
};
//cell.metadata.set('extensions', data);
this._mutex(() => {
cell.sharedModel.setMetadata({ extensions: data });
});
Expand All @@ -558,24 +563,23 @@ export class GridStackModel {
col: null,
width: 2,
height: 2,
locked: false
locked: true
};
//cell.metadata.set('extensions', data);
this._mutex(() => {
cell.sharedModel.setMetadata({ extensions: data });
});
}
console.debug('Cell Medatada:', data);
}

private _context: DocumentRegistry.IContext<INotebookModel>;
private _editorConfig: StaticNotebook.IEditorConfig;
private _notebookConfig: StaticNotebook.INotebookConfig;
private _info: DashboardView;
private _ystate: Y.Map<any> = new Y.Map();

private _ready: Signal<this, null>;
private _cellRemoved: Signal<this, string>;
private _cellPinned: Signal<this, LockSignal>;
private _cellRemoved: Signal<this, CellChange>;
private _cellPinned: Signal<this, CellChange>;
private _stateChanged: Signal<this, null>;
private _contentChanged: Signal<this, null>;
}
Expand Down
Loading

0 comments on commit b0a6722

Please sign in to comment.