use std::{
future::Future,
marker::PhantomPinned,
ops::{Deref, DerefMut},
};
use tokio::sync::watch;
type CompilerInner = rspack_core::Compiler;
pub(crate) struct Compiler(CompilerInner, PhantomPinned);
impl From<CompilerInner> for Compiler {
fn from(value: CompilerInner) -> Self {
Self(value, PhantomPinned)
}
}
impl Deref for Compiler {
type Target = CompilerInner;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Compiler {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub(crate) struct CompilerState(watch::Sender<bool>);
impl CompilerState {
pub(crate) fn init() -> Self {
Self(watch::Sender::new(false))
}
}
impl CompilerState {
pub(crate) fn running(&self) -> bool {
*self.0.borrow()
}
pub(crate) fn enter(&self) -> CompilerStateGuard {
self.0.send_replace(true);
CompilerStateGuard(self.0.clone())
}
pub(crate) fn wait_idle(&self) -> impl Future<Output = ()> + Send + 'static {
let mut receiver = self.0.subscribe();
async move {
let _ = receiver.wait_for(|running| !running).await;
}
}
}
pub(crate) struct CompilerStateGuard(watch::Sender<bool>);
impl Drop for CompilerStateGuard {
fn drop(&mut self) {
self.0.send_replace(false);
}
}