Skip to content

Commit b8cd536

Browse files
committed
fix: temp patch for value after error
1 parent ad6e488 commit b8cd536

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

renderer/components/PlutoOutput.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
setup_mathjax,
1313
useState,
1414
useErrorBoundary,
15+
useMemo,
1516
} from "@plutojl/rainbow/ui";
1617
import { type RendererContext } from "vscode-notebook-renderer";
1718

@@ -85,7 +86,35 @@ export function PlutoOutput({ state, context }: PlutoOutputProps) {
8586
});
8687
return () => d?.dispose();
8788
}, [state.cell_id, context]);
89+
90+
const OUTPUT = useMemo(() => {
91+
// This is probably a bug in the immer bundling; the mime edits don't propagate ;/
92+
// TODO: @pankgeorg investigate pls
93+
const fixedMime =
94+
localState.output.mime === "application/vnd.pluto.stacktrace+object" &&
95+
(typeof localState.output.body !== "object" ||
96+
!("stacktrace" in localState.output.body))
97+
? "text/plain"
98+
: localState.output.mime;
99+
if (localState.output?.mime)
100+
return html`<${OutputBody}
101+
persist_js_state="${localState.output.persist_js_state}"
102+
body="${localState.output?.body}"
103+
mime="${fixedMime}"
104+
sanitize_html="${false /* Maybe reconsider */}"
105+
></${OutputBody}>`;
106+
return "Loading...";
107+
}, [
108+
localState,
109+
localState.cell_id,
110+
localState.output.mime,
111+
localState.output.body,
112+
localState.running,
113+
localState.errored,
114+
]);
115+
88116
if (error) {
117+
console.error(error);
89118
return html`<div onclick=${resetError}>
90119
An error occured. Click <button onClick=${resetError}>here</button> to
91120
reset the view
@@ -108,14 +137,7 @@ export function PlutoOutput({ state, context }: PlutoOutputProps) {
108137
></progress>
109138
</div>`
110139
: null}
111-
${localState.output?.mime
112-
? html`<${OutputBody}
113-
persist_js_state="${localState.output.persist_js_state}"
114-
body="${localState.output?.body}"
115-
mime="${localState.output?.mime}"
116-
sanitize_html="${false /* Maybe reconsider */}"
117-
></${OutputBody}>`
118-
: "Loading..."}
140+
${OUTPUT}
119141
${terminal?.length
120142
? html`<details>
121143
<summary>stdout</summary>

src/controller.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,31 @@ export class PlutoNotebookController {
383383
// );
384384
// }
385385
}
386+
private updateAllCellsFromState = (
387+
notebook: vscode.NotebookDocument,
388+
update: UpdateEvent
389+
) => {
390+
// Optimistically send data. May be ignored.
391+
// If not ignored, this makes sure logs, stdout and progress
392+
// is communicated
393+
const fullNotebookState = update.notebook;
394+
Object.entries(fullNotebookState?.cell_results ?? {}).forEach(
395+
([cell_id, state]) => {
396+
this.sendMessageToRenderer(notebook, {
397+
type: "setState",
398+
state,
399+
cell_id,
400+
});
401+
}
402+
);
403+
};
386404

387405
/**
388406
* Handles streaming updates from the Pluto worker via patches.
389407
*/
390408
private onPlutoNotebookUpdate = (notebook: vscode.NotebookDocument) => {
391409
return (event: UpdateEvent) => {
410+
console.log({ event });
392411
try {
393412
const patches = event.data?.patches as Patch[] | undefined;
394413
const fullNotebookState = event.notebook;
@@ -399,7 +418,7 @@ export class PlutoNotebookController {
399418
);
400419
return;
401420
}
402-
421+
let anyWeird = false;
403422
for (const patch of patches) {
404423
const path = patch.path;
405424
const [action, ...rest] = path;
@@ -462,12 +481,18 @@ export class PlutoNotebookController {
462481
case "last_save_time":
463482
break;
464483
default:
484+
anyWeird = true;
465485
this.outputChannel.appendLine(
466486
`[UNHANDLED] ${patch.path.join(".")} action ${patch.op}`
467487
);
468488
}
469489
}
490+
if (anyWeird) {
491+
console.log("Not sure if all ok, updating everything");
492+
this.updateAllCellsFromState(notebook, event);
493+
}
470494
} catch (e: unknown) {
495+
this.updateAllCellsFromState(notebook, event);
471496
this.outputChannel.appendLine(
472497
`Failed to process patch update: ${
473498
e instanceof Error ? e.message : String(e)

src/serializer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class PlutoNotebookSerializer implements vscode.NotebookSerializer {
3030
pluto_notebook_id: parsed.notebook_id,
3131
pluto_version: parsed.pluto_version,
3232
};
33-
3433
return notebookData;
3534
} catch (error) {
3635
// Fallback: treat as single code cell if parsing fails

0 commit comments

Comments
 (0)