Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 46481cf

Browse files
committed
fix: do not create unneccessary eventListeners
1 parent 61aa990 commit 46481cf

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/events.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export const NotebookEventType = {
99
NotebookResponse: `${PREFIX}NotebookResponse`,
1010
NotebookChange: `${PREFIX}NotebookChange`,
1111
NotebookLoadContent: `${PREFIX}NotebookLoadContent`,
12-
NotebookUnmount: `${PREFIX}NotebookUnmount`
12+
NotebookUnmount: `${PREFIX}NotebookUnmount`,
13+
NotebookRetry: `${PREFIX}NotebookRetry`
1314
} as const;
1415

1516
export type NotebookEventTypeKeys = keyof typeof NotebookEventType;
@@ -19,7 +20,7 @@ export type NotebookEventTypeValue =
1920
export interface NotebookCell {
2021
cell_type: string;
2122
execution_count: null;
22-
metadata: { trusted: boolean; editable: boolean; deletable: boolean };
23+
metadata: { trusted: boolean; editable: boolean; deletable?: boolean };
2324
outputs: [];
2425
source: string[] | string;
2526
}

src/index.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import {
55
} from '@jupyterlab/application';
66
import { ICommandPalette, IThemeManager } from '@jupyterlab/apputils';
77
import { IDocumentManager } from '@jupyterlab/docmanager';
8-
import {
9-
INotebookTracker,
10-
NotebookPanel,
11-
NotebookActions
12-
} from '@jupyterlab/notebook';
8+
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
139
import { ITranslator } from '@jupyterlab/translation';
1410
import {
1511
NotebookEventType,
@@ -18,12 +14,9 @@ import {
1814
onNotebookRequested,
1915
send
2016
} from './events';
17+
import { kernels } from './kernels';
2118

22-
export const kernels = {
23-
gluePySpark: 'glue_python_kernel',
24-
glueSpark: 'glue_scala_kernel'
25-
};
26-
19+
const cellNodes = new Set();
2720
const activate = (
2821
app: JupyterFrontEnd,
2922
labShell: ILabShell,
@@ -101,25 +94,33 @@ const activate = (
10194
buttonContainer.appendChild(downloadButton);
10295

10396
/* NOTE: Try this if you need to run content programatically */
104-
NotebookActions.run(
105-
notebookPanel.content,
106-
notebookPanel.sessionContext
107-
);
108-
109-
/**
110-
* Every time the notebook changes we send it to Glue Studio to keep track of its state.
111-
* But only after the notebook has been initialized.
112-
*/
113-
notebookTracker.activeCellChanged.connect(async (tracker, cell) => {
114-
// Note: Datasets only store string values that's why we compare against a string
115-
if (body.dataset.glueInitialized === 'true') {
116-
onNotebookChange(tracker);
117-
}
118-
});
97+
// NotebookActions.run(
98+
// notebookPanel.content,
99+
// notebookPanel.sessionContext
100+
// );
119101
}
120102
);
121103
});
122104

105+
/**
106+
* Every time the notebook changes we send it to Glue Studio to keep track of its state.
107+
* But only after the notebook has been initialized.
108+
*/
109+
notebookTracker.activeCellChanged.connect(async (tracker, cell) => {
110+
const body = document.querySelector('body');
111+
// Note: Datasets only store string values that's why we compare against a string
112+
if (body.dataset.glueInitialized === 'true' && !cellNodes.has(cell.node)) {
113+
onNotebookChange(tracker);
114+
115+
const cb = () => {
116+
onNotebookChange(tracker);
117+
};
118+
119+
cell.model.contentChanged.connect(cb);
120+
cellNodes.add(cell.node);
121+
}
122+
});
123+
123124
/**
124125
* This event is fired once the current notebook widget is loaded. In the container
125126
* window, if the iframe is rendered without the notebook widget, the users will see an

src/kernels.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const kernels = {
2+
gluePySpark: 'glue_pyspark',
3+
glueSpark: 'glue_spark'
4+
};

0 commit comments

Comments
 (0)