Skip to content

Commit 5616c84

Browse files
committed
Fix real-time output clearing synchronization
Improve output clearing to work properly in collaborative environments by: 1. Immediate YDoc clearing: Clear outputs from shared document first for instant real-time sync to all connected clients 2. Async disk cleanup: Make API call to clear disk storage as secondary operation 3. Better error handling: Add early returns and improved error messages 4. Enhanced reliability: Real-time collaboration works even if disk API fails This fixes the issue where one user clearing outputs wouldn't be visible to other users until they reloaded the notebook.
1 parent ad7c30d commit 5616c84

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/notebook-factory/notebook-factory.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,18 @@ NotebookActions.outputCleared.connect((sender, args) => {
348348
const awareness = notebook.model?.sharedModel.awareness;
349349
const awarenessStates = awareness?.getStates();
350350

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+
351360
if (awarenessStates?.size === 0) {
352361
console.log('Could not delete cell output, awareness is not present');
362+
return; // Early return since we can't get fileId without awareness
353363
}
354364

355365
let fileId = null;
@@ -361,20 +371,21 @@ NotebookActions.outputCleared.connect((sender, args) => {
361371

362372
if (fileId === null) {
363373
console.error('No fileId found in awareness');
374+
return; // Early return since we can't make API call without fileId
364375
}
365376

377+
// SECOND: Send API request to clear outputs from disk storage
366378
try {
367-
// Send a request to clear the outputs
368379
requestAPI(`/api/outputs/${fileId}/${cellId}`, {
369380
method: 'DELETE'
370381
})
371382
.then(() => {
372-
console.debug(`Successfully cleared outputs for cell ${cellId}`);
383+
console.debug(`Successfully cleared outputs from disk for cell ${cellId}`);
373384
})
374385
.catch((error: Error) => {
375-
console.error(`Failed to clear outputs for cell ${cellId}:`, error);
386+
console.error(`Failed to clear outputs from disk for cell ${cellId}:`, error);
376387
});
377388
} catch (error: unknown) {
378-
console.error('Error in output clearing process:', error);
389+
console.error('Error in disk output clearing process:', error);
379390
}
380391
});

0 commit comments

Comments
 (0)