Skip to content

Commit d40aaf7

Browse files
authored
Fix ts errors (#73)
* Cleaned up unused code. * linting * Fixed ts errors, simplified code, eslint. * Removed unused imports * Added a note to explain ts cast * Fixed lint warnings
1 parent eb3b6e9 commit d40aaf7

File tree

5 files changed

+79
-131
lines changed

5 files changed

+79
-131
lines changed

src/docprovider/filebrowser.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ export const ynotebook: JupyterFrontEndPlugin<void> = {
130130
'experimentalEnableDocumentWideUndoRedo'
131131
).composite as boolean;
132132

133-
// @ts-ignore
134-
disableDocumentWideUndoRedo = !enableDocWideUndo ?? true;
133+
disableDocumentWideUndoRedo = !(enableDocWideUndo ?? false);
135134
};
136135

137136
updateSettings(settings);

src/executionindicator.tsx

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import React from 'react';
44

55
import { ISessionContext } from '@jupyterlab/apputils';
66
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
7-
import {
8-
VDomRenderer
9-
} from '@jupyterlab/ui-components';
7+
import { VDomRenderer } from '@jupyterlab/ui-components';
108

11-
import {
12-
Notebook,
13-
ExecutionIndicatorComponent,
14-
ExecutionIndicator as E,
9+
import {
10+
Notebook,
11+
ExecutionIndicatorComponent,
12+
ExecutionIndicator as E
1513
} from '@jupyterlab/notebook';
1614

17-
1815
/**
1916
* A VDomRenderer widget for displaying the execution status.
2017
*/
@@ -61,36 +58,38 @@ export class AwarenessExecutionIndicator extends VDomRenderer<AwarenessExecution
6158
}
6259

6360
export namespace AwarenessExecutionIndicator {
64-
6561
export class Model extends E.Model {
66-
6762
/**
6863
* A weak map to hold execution status of multiple notebooks.
6964
*/
70-
71-
attachNotebook(data: { content?: Notebook; context?: ISessionContext; } | null): void {
72-
let nb = data?.content;
65+
66+
// (this as any) casts are required to avoid
67+
// ts errors when accessing private methods
68+
attachNotebook(
69+
data: { content?: Notebook; context?: ISessionContext } | null
70+
): void {
71+
const nb = data?.content;
7372
if (!nb) {
7473
return;
7574
}
76-
(this as any)._currentNotebook = nb;
75+
(this as any)._currentNotebook = nb;
7776
(this as any)._notebookExecutionProgress.set(nb, {
7877
executionStatus: 'idle',
7978
kernelStatus: 'idle',
8079
totalTime: 0,
8180
interval: 0,
8281
timeout: 0,
8382
scheduledCell: new Set<string>(),
84-
scheduledCellNumber: 0,
83+
scheduledCellNumber: 0,
8584
needReset: true
8685
});
8786
const state = (this as any)._notebookExecutionProgress.get(nb);
88-
87+
8988
const contextStatusChanged = (ctx: ISessionContext) => {
9089
if (state) {
91-
let awarenessStates = nb?.model?.sharedModel.awareness.getStates();
90+
const awarenessStates = nb?.model?.sharedModel.awareness.getStates();
9291
if (awarenessStates) {
93-
for (let [_, clientState] of awarenessStates) {
92+
for (const [, clientState] of awarenessStates) {
9493
if ('kernel' in clientState) {
9594
state.kernelStatus = clientState['kernel']['execution_state'];
9695
this.stateChanged.emit(void 0);
@@ -100,11 +99,9 @@ export namespace AwarenessExecutionIndicator {
10099
}
101100
}
102101
};
103-
102+
104103
nb?.model?.sharedModel.awareness.on('change', contextStatusChanged);
105104
super.attachNotebook(data);
106105
}
107106
}
108107
}
109-
110-

src/index.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { Title, Widget } from '@lumino/widgets';
99
import {
1010
INotebookTracker,
1111
NotebookPanel,
12-
// Notebook,
13-
// ExecutionIndicator,
1412
INotebookModel
1513
} from '@jupyterlab/notebook';
1614
import { IStatusBar } from '@jupyterlab/statusbar';
@@ -24,16 +22,12 @@ import {
2422
SessionContextDialogs
2523
} from '@jupyterlab/apputils';
2624
import { KeyboardEvent } from 'react';
27-
import {
28-
IToolbarWidgetRegistry,
29-
} from '@jupyterlab/apputils';
30-
import {
31-
AwarenessExecutionIndicator
32-
} from './executionindicator';
25+
import { IToolbarWidgetRegistry } from '@jupyterlab/apputils';
26+
import { AwarenessExecutionIndicator } from './executionindicator';
3327

3428
import { IEditorServices } from '@jupyterlab/codeeditor';
3529
import { requestAPI } from './handler';
36-
import { YNotebookContentFactory } from './notebook';
30+
import { RtcNotebookContentFactory } from './notebook';
3731

3832
import { rtcContentProvider, yfile, ynotebook, logger } from './docprovider';
3933

@@ -43,12 +37,10 @@ import * as Y from 'yjs';
4337
import { Awareness } from 'y-protocols/awareness';
4438
import { IAwareness } from '@jupyter/ydoc';
4539

46-
import {
47-
AwarenessKernelStatus
48-
} from './kernelstatus';
4940
import { ServerConnection } from '@jupyterlab/services';
5041
import { WebSocketAwarenessProvider } from './docprovider/awareness';
5142
import { URLExt } from '@jupyterlab/coreutils';
43+
import { AwarenessKernelStatus } from './kernelstatus';
5244
/**
5345
* Initialization data for the @jupyter/rtc-core extension.
5446
*/
@@ -98,7 +90,6 @@ export const rtcGlobalAwarenessPlugin: JupyterFrontEndPlugin<IAwareness> = {
9890
requires: [IStateDB],
9991
provides: IGlobalAwareness,
10092
activate: (app: JupyterFrontEnd, state: StateDB): IAwareness => {
101-
// @ts-ignore
10293
const { user } = app.serviceManager;
10394

10495
const ydoc = new Y.Doc();
@@ -132,20 +123,18 @@ export const rtcGlobalAwarenessPlugin: JupyterFrontEndPlugin<IAwareness> = {
132123
}
133124
};
134125

135-
class AwarenessExecutionIndicatorIcon implements DocumentRegistry.IWidgetExtension<
136-
NotebookPanel,
137-
INotebookModel
138-
> {
139-
createNew(panel: NotebookPanel): IDisposable {
140-
let item = new AwarenessExecutionIndicator()
141-
let nb = panel.content;
142-
item.model.attachNotebook({content: nb})
126+
class AwarenessExecutionIndicatorIcon
127+
implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel>
128+
{
129+
createNew(panel: NotebookPanel): IDisposable {
130+
const item = new AwarenessExecutionIndicator();
131+
const nb = panel.content;
132+
item.model.attachNotebook({ content: nb });
143133
panel.toolbar.insertAfter('kernelName', 'awarenessExecutionProgress', item);
144-
return item
134+
return item;
145135
}
146136
}
147137

148-
149138
/**
150139
* A plugin that provides a execution indicator item to the status bar.
151140
*/
@@ -162,17 +151,18 @@ export const executionIndicator: JupyterFrontEndPlugin<void> = {
162151
translator: ITranslator,
163152
statusBar: IStatusBar | null,
164153
settingRegistry: ISettingRegistry | null,
165-
toolbarRegistry: IToolbarWidgetRegistry,
154+
toolbarRegistry: IToolbarWidgetRegistry
166155
) => {
167-
console.log("JupyterLab extension activated: Awareness Execution Indicator")
156+
console.log(
157+
'JupyterLab extension activated: Awareness Execution Indicator'
158+
);
168159
app.docRegistry.addWidgetExtension(
169-
"Notebook",
160+
'Notebook',
170161
new AwarenessExecutionIndicatorIcon()
171-
)
162+
);
172163
}
173164
};
174165

175-
176166
/**
177167
* A plugin that provides a kernel status item to the status bar.
178168
*/
@@ -190,7 +180,9 @@ export const kernelStatus: JupyterFrontEndPlugin<IKernelStatusModel> = {
190180
translator_: ITranslator | null,
191181
labShell: ILabShell | null
192182
): IKernelStatusModel => {
193-
console.log("JupyterLab extension activated: Awareness Kernel Status Indicator")
183+
console.log(
184+
'JupyterLab extension activated: Awareness Kernel Status Indicator'
185+
);
194186
const translator = translator_ ?? nullTranslator;
195187
const sessionDialogs =
196188
sessionDialogs_ ?? new SessionContextDialogs({ translator });
@@ -249,7 +241,7 @@ export const kernelStatus: JupyterFrontEndPlugin<IKernelStatusModel> = {
249241
if (oldValue) {
250242
oldValue.title.changed.disconnect(onTitleChanged);
251243
}
252-
244+
253245
item.model.attachDocument(newValue);
254246
item.model.sessionContext =
255247
[...providers]
@@ -284,7 +276,6 @@ export const kernelStatus: JupyterFrontEndPlugin<IKernelStatusModel> = {
284276
}
285277
};
286278

287-
288279
/**
289280
* The notebook cell factory provider.
290281
*/
@@ -296,7 +287,7 @@ const factory: JupyterFrontEndPlugin<NotebookPanel.IContentFactory> = {
296287
autoStart: true,
297288
activate: (app: JupyterFrontEnd, editorServices: IEditorServices) => {
298289
const editorFactory = editorServices.factoryService.newInlineEditor;
299-
return new YNotebookContentFactory({ editorFactory });
290+
return new RtcNotebookContentFactory({ editorFactory });
300291
}
301292
};
302293

@@ -313,4 +304,3 @@ const plugins: JupyterFrontEndPlugin<unknown>[] = [
313304
];
314305

315306
export default plugins;
316-

src/kernelstatus.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
// Distributed under the terms of the Modified BSD License.
33

44
import { TextItem } from '@jupyterlab/statusbar';
5-
import {
6-
ITranslator,
7-
nullTranslator,
8-
} from '@jupyterlab/translation';
5+
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
96
import { VDomRenderer } from '@jupyterlab/ui-components';
107
import React, { KeyboardEvent } from 'react';
118
import { ISessionContext } from '@jupyterlab/apputils';
129
import { Widget } from '@lumino/widgets';
13-
import {
14-
KernelStatus as K
15-
} from '@jupyterlab/apputils';
10+
import { KernelStatus as K } from '@jupyterlab/apputils';
1611
import { NotebookPanel } from '@jupyterlab/notebook';
1712
import { Session } from '@jupyterlab/services';
1813

@@ -81,12 +76,11 @@ namespace KernelStatusComponent {
8176
}
8277
}
8378

84-
8579
export class AwarenessKernelStatus extends VDomRenderer<AwarenessKernelStatus.Model> {
8680
/**
8781
* Construct the kernel status widget.
8882
*/
89-
constructor(opts:K.IOptions, translator?: ITranslator) {
83+
constructor(opts: K.IOptions, translator?: ITranslator) {
9084
super(new AwarenessKernelStatus.Model(translator));
9185
this.translator = translator || nullTranslator;
9286
this._handleClick = opts.onClick;
@@ -119,23 +113,22 @@ export class AwarenessKernelStatus extends VDomRenderer<AwarenessKernelStatus.Mo
119113
private _handleKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
120114
}
121115

122-
123116
export namespace AwarenessKernelStatus {
124-
125117
export class Model extends K.Model {
126-
127-
attachDocument(widget: Widget | null) {
118+
attachDocument(widget: Widget | null) {
128119
if (!widget) {
129-
return
120+
return;
130121
}
131-
let panel = (widget as NotebookPanel);
122+
const panel = widget as NotebookPanel;
132123
const stateChanged = () => {
133124
if (this) {
134-
let awarenessStates = panel?.model?.sharedModel.awareness.getStates();
125+
const awarenessStates =
126+
panel?.model?.sharedModel.awareness.getStates();
135127
if (awarenessStates) {
136-
for (let [_, clientState] of awarenessStates) {
128+
for (const [, clientState] of awarenessStates) {
137129
if ('kernel' in clientState) {
138-
(this as any)._kernelStatus = clientState['kernel']['execution_state'];
130+
(this as any)._kernelStatus =
131+
clientState['kernel']['execution_state'];
139132
(this as any).stateChanged.emit(void 0);
140133
return;
141134
}
@@ -150,9 +143,13 @@ export namespace AwarenessKernelStatus {
150143
const oldState = (this as any)._getAllState();
151144
(this as any)._sessionContext = sessionContext;
152145
(this as any)._kernelName =
153-
sessionContext?.kernelDisplayName ?? (this as any)._trans.__('No Kernel');
154-
(this as any)._triggerChange(oldState, (this as any)._getAllState());
155-
sessionContext?.kernelChanged.connect(this._onKernelDisplayNameChanged, this);
146+
sessionContext?.kernelDisplayName ??
147+
(this as any)._trans.__('No Kernel');
148+
(this as any)._triggerChange(oldState, (this as any)._getAllState());
149+
sessionContext?.kernelChanged.connect(
150+
this._onKernelDisplayNameChanged,
151+
this
152+
);
156153
}
157154
/**
158155
* React to changes in the kernel.
@@ -167,7 +164,5 @@ export namespace AwarenessKernelStatus {
167164
(this as any)._kernelName = _sessionContext.kernelDisplayName;
168165
(this as any)._triggerChange(oldState, (this as any)._getAllState());
169166
}
170-
171167
}
172-
173-
}
168+
}

0 commit comments

Comments
 (0)