|
5 | 5 | ICodeCellModel |
6 | 6 | } from '@jupyterlab/cells'; |
7 | 7 | import { IChangedArgs } from '@jupyterlab/coreutils'; |
8 | | -import { Notebook, NotebookPanel } from '@jupyterlab/notebook'; |
| 8 | +import { Notebook, NotebookPanel, NotebookActions } from '@jupyterlab/notebook'; |
9 | 9 | import { CellChange, createMutex, ISharedCodeCell } from '@jupyter/ydoc'; |
10 | 10 | import { IOutputAreaModel, OutputAreaModel } from '@jupyterlab/outputarea'; |
11 | 11 | import { requestAPI } from '../handler'; |
@@ -340,3 +340,52 @@ export class RtcNotebookContentFactory |
340 | 340 | return new ResettableNotebook(options); |
341 | 341 | } |
342 | 342 | } |
| 343 | + |
| 344 | +// Add a handler for the outputCleared signal |
| 345 | +NotebookActions.outputCleared.connect((sender, args) => { |
| 346 | + const { notebook, cell } = args; |
| 347 | + const cellId = cell.model.sharedModel.getId(); |
| 348 | + const awareness = notebook.model?.sharedModel.awareness; |
| 349 | + const awarenessStates = awareness?.getStates(); |
| 350 | + |
| 351 | + // FIRST: Clear outputs in YDoc for immediate real-time sync to all clients |
| 352 | + try { |
| 353 | + const sharedCodeCell = cell.model.sharedModel as ISharedCodeCell; |
| 354 | + sharedCodeCell.setOutputs([]); |
| 355 | + console.debug(`Cleared outputs in YDoc for cell ${cellId}`); |
| 356 | + } catch (error: unknown) { |
| 357 | + console.error('Error clearing YDoc outputs:', error); |
| 358 | + } |
| 359 | + |
| 360 | + if (awarenessStates?.size === 0) { |
| 361 | + console.log('Could not delete cell output, awareness is not present'); |
| 362 | + return; // Early return since we can't get fileId without awareness |
| 363 | + } |
| 364 | + |
| 365 | + let fileId = null; |
| 366 | + for (const [_, state] of awarenessStates || []) { |
| 367 | + if (state && 'file_id' in state) { |
| 368 | + fileId = state['file_id']; |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + if (fileId === null) { |
| 373 | + console.error('No fileId found in awareness'); |
| 374 | + return; // Early return since we can't make API call without fileId |
| 375 | + } |
| 376 | + |
| 377 | + // SECOND: Send API request to clear outputs from disk storage |
| 378 | + try { |
| 379 | + requestAPI(`/api/outputs/${fileId}/${cellId}`, { |
| 380 | + method: 'DELETE' |
| 381 | + }) |
| 382 | + .then(() => { |
| 383 | + console.debug(`Successfully cleared outputs from disk for cell ${cellId}`); |
| 384 | + }) |
| 385 | + .catch((error: Error) => { |
| 386 | + console.error(`Failed to clear outputs from disk for cell ${cellId}:`, error); |
| 387 | + }); |
| 388 | + } catch (error: unknown) { |
| 389 | + console.error('Error in disk output clearing process:', error); |
| 390 | + } |
| 391 | +}); |
0 commit comments