feat(notebook): reload, journal and state panel polish
D3 (partial): reload re-parses the document while preserving kernels, marks changed cells dirty; Journal and Reload view actions; filtered Node variable names for the state panel; per-session timeout parsing before spawn.
This commit is contained in:
1 parent
3001ed7ba5
commit
9873df7099
6 files changed
+131
-6
No files matched your search
@@ -370,6 +370,53 @@ impl DocumentSession {
|
||||
})
|
||||
}
|
||||
|
||||
/// Re-read the document, preserving kernels/state/journal. Changed cells become dirty.
|
||||
pub fn reload(&mut self, root: &Path) -> Result<(), String> {
|
||||
let mut fresh = Self::open(&self.path, root)?;
|
||||
let old: HashMap<String, Cell> = self
|
||||
.cells
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|c| (c.name.clone(), c))
|
||||
.collect();
|
||||
for c in &mut fresh.cells {
|
||||
if let Some(o) = old.get(&c.name) {
|
||||
c.runs = o.runs;
|
||||
c.last_ms = o.last_ms;
|
||||
c.last_ok = o.last_ok;
|
||||
c.last_error = o.last_error.clone();
|
||||
c.status = if o.source_hash != c.source_hash || o.kernel != c.kernel {
|
||||
CellStatus::Dirty
|
||||
} else if o.status == CellStatus::Running {
|
||||
CellStatus::Dirty
|
||||
} else {
|
||||
o.status
|
||||
};
|
||||
}
|
||||
}
|
||||
fresh.host = std::mem::replace(&mut self.host, HostContext::default());
|
||||
fresh.kernel_sessions = std::mem::take(&mut self.kernel_sessions);
|
||||
fresh.value_store = std::mem::take(&mut self.value_store);
|
||||
fresh.journal = std::mem::take(&mut self.journal);
|
||||
fresh.seq = self.seq;
|
||||
fresh.cursor = self.cursor.min(fresh.order.len());
|
||||
fresh.opened_ms = self.opened_ms;
|
||||
let known: HashSet<String> = fresh.kernels.iter().map(|k| k.name.clone()).collect();
|
||||
let orphans: Vec<String> = fresh
|
||||
.kernel_sessions
|
||||
.keys()
|
||||
.filter(|k| !known.contains(*k))
|
||||
.cloned()
|
||||
.collect();
|
||||
for k in orphans {
|
||||
if let Some(id) = fresh.kernel_sessions.remove(&k) {
|
||||
let _ = foreign_session::close(&mut fresh.host, &Value::Text(id));
|
||||
}
|
||||
}
|
||||
*self = fresh;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn title(&self) -> String {
|
||||
self.path
|
||||
.file_stem()
|
||||
@@ -916,4 +963,33 @@ console.log(typeof shared === "undefined" ? "isolated" : "leaked");
|
||||
s.close();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_marks_changed_cell_dirty() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join(format!(
|
||||
"target/notebook-reload-{}-{}.mq.md",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
));
|
||||
let src = "# main\n\n`a` =\n```node\nconsole.log(1);\n```\n";
|
||||
std::fs::write(&path, src).unwrap();
|
||||
let mut s = DocumentSession::open(&path, &root).unwrap();
|
||||
let r = s.run_cell("a", None).unwrap();
|
||||
assert_eq!(field(&r, "ok"), Some(Value::Bool(true)));
|
||||
assert_eq!(s.cells[0].status, CellStatus::Clean);
|
||||
let src2 = "# main\n\n`a` =\n```node\nconsole.log(2);\n```\n";
|
||||
std::fs::write(&path, src2).unwrap();
|
||||
s.reload(&root).unwrap();
|
||||
assert_eq!(s.cells[0].status, CellStatus::Dirty);
|
||||
assert!(s.cells[0].source.contains("console.log(2)"));
|
||||
s.close();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
}
|
||||
@@ -2842,6 +2842,8 @@ fn notebook_ui() -> &'static str {
|
||||
<button type="button" id="nb-restart">Restart & Run All</button>
|
||||
<button type="button" id="nb-step">Step</button>
|
||||
<button type="button" id="nb-state">State</button>
|
||||
<button type="button" id="nb-journal">Journal</button>
|
||||
<button type="button" id="nb-reload">Reload</button>
|
||||
<span id="nb-status">opening…</span>
|
||||
</div>
|
||||
<div class="notebook-bar" id="nb-state-actions" hidden>
|
||||
@@ -3016,6 +3018,21 @@ fn notebook_ui() -> &'static str {
|
||||
status(r.ok ? "pulled " + n + " ← " + k : "pull failed");
|
||||
});
|
||||
});
|
||||
bind("nb-journal", function () {
|
||||
post("/api/session/journal", {}).then(function (r) {
|
||||
var pre = document.getElementById("nb-state-out");
|
||||
if (!pre) return;
|
||||
pre.hidden = false;
|
||||
pre.textContent = JSON.stringify(r.journal || r, null, 2);
|
||||
});
|
||||
});
|
||||
bind("nb-reload", function () {
|
||||
post("/api/session/reload", {}).then(function (r) {
|
||||
if (!r.ok) { status(r.error || "reload failed"); return; }
|
||||
renderSummary(r.session);
|
||||
status("reloaded");
|
||||
});
|
||||
});
|
||||
document.addEventListener("click", function (e) {
|
||||
var btn = e.target && e.target.closest ? e.target.closest(".code-run") : null;
|
||||
if (!btn || !state.open) return;
|
||||
@@ -3428,6 +3445,8 @@ mod tests {
|
||||
"nb-restart",
|
||||
"nb-step",
|
||||
"nb-state",
|
||||
"nb-journal",
|
||||
"nb-reload",
|
||||
"nb-kernel-input",
|
||||
"nb-push",
|
||||
"nb-pull",
|
||||
|
||||
@@ -179,6 +179,11 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
|
||||
notebook_api::api_open(&root.root, body)
|
||||
})
|
||||
}
|
||||
"/api/session/reload" => {
|
||||
return api_debug_post(request, |body| {
|
||||
notebook_api::api_reload(&root.root, body)
|
||||
})
|
||||
}
|
||||
"/api/session/close" => {
|
||||
return api_debug_post(request, |body| {
|
||||
notebook_api::api_close(&root.root, body)
|
||||
|
||||
@@ -75,7 +75,30 @@ pub fn api_open(root: &Path, body: &str) -> serde_json::Value {
|
||||
};
|
||||
match get_or_open(root, &abs) {
|
||||
Ok(s) => match s.lock() {
|
||||
Ok(sess) => ok_session(&sess, serde_json::json!({})),
|
||||
Ok(mut sess) => {
|
||||
if let Err(e) = sess.reload(root) {
|
||||
return err(e);
|
||||
}
|
||||
ok_session(&sess, serde_json::json!({}))
|
||||
}
|
||||
Err(_) => err("session lock poisoned".into()),
|
||||
},
|
||||
Err(e) => err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn api_reload(root: &Path, body: &str) -> serde_json::Value {
|
||||
let v = body_value(body);
|
||||
let (_rel, abs) = match path_of(root, &v) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return err(e),
|
||||
};
|
||||
match get_or_open(root, &abs) {
|
||||
Ok(s) => match s.lock() {
|
||||
Ok(mut sess) => match sess.reload(root) {
|
||||
Ok(()) => ok_session(&sess, serde_json::json!({})),
|
||||
Err(e) => err(e),
|
||||
},
|
||||
Err(_) => err("session lock poisoned".into()),
|
||||
},
|
||||
Err(e) => err(e),
|
||||
|
||||
Reference in new issue
Block a user