Skip to content

Commit

Permalink
fix: handle compile events in standalone preview server (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Feb 21, 2025
1 parent d21ebc3 commit 22ce78d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
10 changes: 7 additions & 3 deletions crates/tinymist/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,20 @@ impl ProjectState {
})
}

pub fn interrupt(&mut self, intr: Interrupt<LspCompilerFeat>) {
pub fn do_interrupt(compiler: &mut LspProjectCompiler, intr: Interrupt<LspCompilerFeat>) {
if let Interrupt::Compiled(compiled) = &intr {
let proj = self.compiler.projects().find(|p| p.id == compiled.id);
let proj = compiler.projects().find(|p| p.id == compiled.id);
if let Some(proj) = proj {
proj.ext
.compiled(&proj.verse.revision, proj.handler.as_ref(), compiled);
}
}

self.compiler.process(intr);
compiler.process(intr);
}

pub fn interrupt(&mut self, intr: Interrupt<LspCompilerFeat>) {
Self::do_interrupt(&mut self.compiler, intr);
}

pub(crate) fn stop(&mut self) {
Expand Down
19 changes: 13 additions & 6 deletions crates/tinymist/src/tool/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use typst_shim::syntax::LinkedNodeExt;

use crate::project::{
CompileHandlerImpl, CompileServerOpts, LspCompiledArtifact, LspInterrupt, ProjectClient,
ProjectCompiler,
ProjectCompiler, ProjectState,
};
use crate::*;
use actor::preview::{PreviewActor, PreviewRequest, PreviewTab};
Expand Down Expand Up @@ -679,29 +679,36 @@ pub async fn preview_main(args: PreviewCliArgs) -> Result<()> {
notified_revision: Mutex::default(),
});

let mut server = ProjectCompiler::new(
let mut compiler = ProjectCompiler::new(
verse,
dep_tx,
CompileServerOpts {
handler: compile_handle,
enable_watch: true,
},
);
let registered = preview_state.register(&server.primary.id, previewer.compile_watcher());
let registered = preview_state.register(&compiler.primary.id, previewer.compile_watcher());
if !registered {
tinymist_std::bail!("failed to register preview");
}

let handle = Arc::new(PreviewProjectHandler {
project_id: server.primary.id.clone(),
let handle: Arc<PreviewProjectHandler> = Arc::new(PreviewProjectHandler {
project_id: compiler.primary.id.clone(),
client: Box::new(intr_tx),
});

compiler.primary.reason.by_entry_update = true;
let service = async move {
let handler = compiler.handler.clone();
handler.on_any_compile_reason(&mut compiler);

let mut intr_rx = intr_rx;
while let Some(intr) = intr_rx.recv().await {
server.process(intr);
log::debug!("Project compiler received: {intr:?}");
ProjectState::do_interrupt(&mut compiler, intr);
}

log::info!("Project compiler exited");
};

(service, handle)
Expand Down

0 comments on commit 22ce78d

Please sign in to comment.