Cache published project previews for the next desktop launch
This commit is contained in:
1 parent
11ec5c853c
commit
f727188c41
8 files changed
+350
-17
No files matched your search
@@ -28,6 +28,7 @@ use tiny_http::{Header, Method, Request, Response, Server};
|
||||
mod bibliography;
|
||||
mod figure_conversion;
|
||||
mod library;
|
||||
mod preview_cache;
|
||||
mod preview_socket;
|
||||
#[path = "../../src/projects.rs"]
|
||||
#[allow(dead_code)]
|
||||
@@ -84,6 +85,7 @@ impl PreviewReply {
|
||||
}
|
||||
|
||||
struct Work {
|
||||
cache: Option<preview_cache::PreviewCache>,
|
||||
respond_to: PreviewReply,
|
||||
job: CompileJob,
|
||||
bibliography_pass: Option<CompileJob>,
|
||||
@@ -333,7 +335,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
)?;
|
||||
let compiler = NativeCompiler::default().with_automatic_entry_checkpoints();
|
||||
let editor = finish_prepare(staged, &run, &compiler, None)?;
|
||||
(port, Some(EditorSession::new(run, editor, compiler)))
|
||||
(port, Some(EditorSession::new(run, editor, compiler, None)))
|
||||
};
|
||||
let mut session = session.map(|session| Arc::new(Mutex::new(session)));
|
||||
let mut opening: Option<OpeningEditor> = None;
|
||||
@@ -436,6 +438,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
reply(request, 200, opening.sources.clone());
|
||||
continue;
|
||||
}
|
||||
("cached-preview", _) if pending.is_some() || project_open => {
|
||||
let cache = pending.and_then(|opening| opening.cache.clone())
|
||||
.or_else(|| {
|
||||
if project_open {
|
||||
session.as_ref()?.lock().unwrap().cache.clone()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
std::thread::spawn(move || {
|
||||
match cache.and_then(|cache| cache.read().ok()) {
|
||||
Some(bytes) => respond(request, 200, "application/json; charset=utf-8", bytes),
|
||||
None => reply(request, 404, json!({"error": "No cached preview"})),
|
||||
}
|
||||
});
|
||||
continue;
|
||||
}
|
||||
("ready", Some(opening)) => {
|
||||
let ready = opening.ready.clone();
|
||||
std::thread::spawn(move || match ready.wait() {
|
||||
@@ -486,6 +505,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
id,
|
||||
run,
|
||||
compiler,
|
||||
cache,
|
||||
first_preview_revision,
|
||||
queue,
|
||||
save_number,
|
||||
@@ -514,9 +534,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let files = editable.iter().filter_map(|(name, path)| {
|
||||
project.file(path).map(|file| (name.as_str(), file.data.as_ref()))
|
||||
});
|
||||
let payload = project_payload(
|
||||
let mut payload = project_payload(
|
||||
run, entry, declared, files, queue.current.load(Ordering::Acquire), id.as_deref(),
|
||||
)?;
|
||||
if cache.as_ref().is_some_and(preview_cache::PreviewCache::exists) {
|
||||
payload["cached_preview"] = json!(true);
|
||||
}
|
||||
reply(request, 200, payload);
|
||||
}
|
||||
(&Method::Post, "/save") => {
|
||||
@@ -676,6 +699,7 @@ type ReadyEditor = Result<Arc<Mutex<EditorSession>>, String>;
|
||||
|
||||
struct OpeningEditor {
|
||||
id: String,
|
||||
cache: Option<preview_cache::PreviewCache>,
|
||||
sources: Value,
|
||||
ready: Arc<OnceLock<ReadyEditor>>,
|
||||
compiler: NativeCompiler,
|
||||
@@ -687,14 +711,19 @@ impl OpeningEditor {
|
||||
id: String,
|
||||
run: PathBuf,
|
||||
staged: StagedProject,
|
||||
cache: Option<preview_cache::PreviewCache>,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let files = staged.editable.iter().filter_map(|(name, path)| {
|
||||
staged.files.get(path).map(|bytes| (name.as_str(), bytes.as_ref()))
|
||||
});
|
||||
let mut sources = project_payload(&run, &staged.entry, &staged.declared, files, 1, Some(&id))?;
|
||||
sources["preparing"] = json!(true);
|
||||
if cache.as_ref().is_some_and(preview_cache::PreviewCache::exists) {
|
||||
sources["cached_preview"] = json!(true);
|
||||
}
|
||||
let opening = Self {
|
||||
id,
|
||||
cache,
|
||||
sources,
|
||||
ready: Arc::new(OnceLock::new()),
|
||||
compiler: NativeCompiler::default().with_automatic_entry_checkpoints(),
|
||||
@@ -704,10 +733,11 @@ impl OpeningEditor {
|
||||
let ready = opening.ready.clone();
|
||||
let compiler = opening.compiler.clone();
|
||||
let superseded = opening.superseded.clone();
|
||||
let cache = opening.cache.clone();
|
||||
std::thread::Builder::new().name("texrra-open-project".into()).spawn(move || {
|
||||
let result = finish_prepare(staged, &run, &compiler, Some(&superseded))
|
||||
.map(|editor| {
|
||||
let mut session = EditorSession::new(run, editor, compiler);
|
||||
let mut session = EditorSession::new(run, editor, compiler, cache);
|
||||
session.id = Some(id);
|
||||
Arc::new(Mutex::new(session))
|
||||
})
|
||||
@@ -754,6 +784,7 @@ fn project_payload<'a>(
|
||||
|
||||
struct EditorSession {
|
||||
id: Option<String>,
|
||||
cache: Option<preview_cache::PreviewCache>,
|
||||
run: PathBuf,
|
||||
compiler: NativeCompiler,
|
||||
first_preview_revision: u64,
|
||||
@@ -768,6 +799,7 @@ impl EditorSession {
|
||||
run: PathBuf,
|
||||
editor: EditorProject,
|
||||
compiler: NativeCompiler,
|
||||
cache: Option<preview_cache::PreviewCache>,
|
||||
) -> Self {
|
||||
// Resource discovery warms the compiler's caches. Only preview jobs
|
||||
// may expose a revision to the editor.
|
||||
@@ -790,6 +822,7 @@ impl EditorSession {
|
||||
});
|
||||
Self {
|
||||
id: None,
|
||||
cache,
|
||||
run,
|
||||
compiler,
|
||||
first_preview_revision,
|
||||
@@ -851,6 +884,7 @@ impl EditorSession {
|
||||
let bibliography_pass = self.bibliography_compiler.as_ref().map(NativeCompiler::start);
|
||||
self.queue.current.store(edit.sequence, Ordering::Release);
|
||||
let replaced = self.queue.next.lock().unwrap().replace(Work {
|
||||
cache: self.cache.as_ref().filter(|cache| cache.matches_saved_sources(project)).cloned(),
|
||||
respond_to,
|
||||
job,
|
||||
bibliography_pass,
|
||||
@@ -1222,6 +1256,7 @@ fn worker(
|
||||
.unwrap()
|
||||
.take();
|
||||
let Some(Work {
|
||||
cache,
|
||||
respond_to,
|
||||
job,
|
||||
bibliography_pass,
|
||||
@@ -1293,7 +1328,7 @@ fn worker(
|
||||
.as_mut()
|
||||
.filter(|p| p.revision == revision)
|
||||
.expect("the worker owns the current publication");
|
||||
let state = match result {
|
||||
let state = match &result {
|
||||
Ok(_) => json!({
|
||||
"state": "published",
|
||||
"compile_ms": compile_ms,
|
||||
@@ -1302,6 +1337,12 @@ fn worker(
|
||||
Err(error) => json!({ "state": "failed", "error": error.to_string() }),
|
||||
};
|
||||
publication.result = Some(state);
|
||||
drop(current);
|
||||
if let (Some(cache), Ok(document)) = (cache, result)
|
||||
&& observer.is_current()
|
||||
{
|
||||
cache.refresh(document);
|
||||
}
|
||||
continue;
|
||||
};
|
||||
let document = match result {
|
||||
@@ -1321,15 +1362,20 @@ fn worker(
|
||||
continue;
|
||||
}
|
||||
match preview {
|
||||
Ok(update) => respond_to.send(
|
||||
200,
|
||||
observer.preview_payload(
|
||||
update,
|
||||
document.execution.execution.steps,
|
||||
&document.entry_checkpoint_work,
|
||||
Some(compile_ms),
|
||||
),
|
||||
),
|
||||
Ok(update) => {
|
||||
respond_to.send(
|
||||
200,
|
||||
observer.preview_payload(
|
||||
update,
|
||||
document.execution.execution.steps,
|
||||
&document.entry_checkpoint_work,
|
||||
Some(compile_ms),
|
||||
),
|
||||
);
|
||||
if let Some(cache) = cache {
|
||||
cache.refresh(document);
|
||||
}
|
||||
}
|
||||
Err(error) => respond_to.send(422, json!({ "error": error, "sequence": sequence })),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user