Skip to content

Commit

Permalink
fix: e2e hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Mar 14, 2024
1 parent 0cae777 commit d3a1b62
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
7 changes: 4 additions & 3 deletions crates/mako/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use mako_core::notify_debouncer_full::new_debouncer;
use mako_core::tokio::sync::broadcast;
use mako_core::tracing::debug;
use mako_core::tungstenite::Message;
use mako_core::{hyper, hyper_staticfile, hyper_tungstenite, tokio};
use mako_core::{hyper, hyper_staticfile, hyper_tungstenite};

use crate::compiler::{Compiler, Context};
use crate::plugin::{PluginGenerateEndParams, PluginGenerateStats};
use crate::tokio_runtime;
use crate::watch::Watcher;

pub struct DevServer {
Expand Down Expand Up @@ -93,7 +94,7 @@ impl DevServer {
debug!("new websocket connection");
let (response, websocket) = hyper_tungstenite::upgrade(req, None).unwrap();
let txws = txws.clone();
tokio::spawn(async move {
tokio_runtime::spawn(async move {
let receiver = txws.subscribe();
Self::handle_websocket(websocket, receiver).await.unwrap();
});
Expand Down Expand Up @@ -138,7 +139,7 @@ impl DevServer {
) -> Result<()> {
let websocket = websocket.await?;
let (mut sender, mut ws_recv) = websocket.split();
let task = tokio::spawn(async move {
let task = tokio_runtime::spawn(async move {
loop {
if let Ok(msg) = receiver.recv().await {
if sender
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod targets;
#[cfg(test)]
mod test_helper;
mod thread_pool;
mod tokio_runtime;
mod transform;
mod transform_in_generate;
mod transformers;
Expand Down
3 changes: 2 additions & 1 deletion crates/mako/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod targets;
#[cfg(test)]
mod test_helper;
mod thread_pool;
mod tokio_runtime;
mod transform;
mod transform_in_generate;
mod transformers;
Expand Down Expand Up @@ -128,7 +129,7 @@ async fn run() -> Result<()> {
let notify = Arc::new(Notify::new());
let to_be_notify = notify.clone();

tokio::spawn(async move {
tokio_runtime::spawn(async move {
let compiler = compiler.clone();

to_be_notify.notified().await;
Expand Down
5 changes: 3 additions & 2 deletions crates/mako/src/plugins/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ use std::path::Path;
use std::sync::Arc;

use mako_core::anyhow::Result;
use mako_core::fs_extra;
use mako_core::glob::glob;
use mako_core::notify::event::{CreateKind, DataChange, ModifyKind, RenameMode};
use mako_core::notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use mako_core::tokio::sync::mpsc::channel;
use mako_core::tracing::debug;
use mako_core::{fs_extra, tokio};

use crate::compiler::Context;
use crate::plugin::Plugin;
use crate::stats::StatsJsonMap;
use crate::tokio_runtime;

pub struct CopyPlugin {}

impl CopyPlugin {
fn watch(context: &Arc<Context>) {
let context = context.clone();
tokio::spawn(async move {
tokio_runtime::spawn(async move {
let (tx, mut rx) = channel(2);
let mut watcher = RecommendedWatcher::new(
move |res| {
Expand Down
20 changes: 20 additions & 0 deletions crates/mako/src/tokio_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::future::Future;

use mako_core::lazy_static::lazy_static;
use mako_core::tokio;

lazy_static! {
static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.thread_name("mako-tokio-runtime")
.build()
.expect("failed to create tokio runtime.");
}

pub fn spawn<F>(future: F) -> tokio::task::JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
TOKIO_RUNTIME.spawn(future)
}

0 comments on commit d3a1b62

Please sign in to comment.