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 1d2d404 commit 8c57f53
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ serde_json = { workspace = true }
serde_yaml = "0.9.22"
svgr-rs = "0.1.3"
thiserror = "1.0.43"
tokio = { version = "1", features = ["rt", "sync"] }
tokio = { version = "1", features = ["net", "rt-multi-thread", "sync"] }
tokio-tungstenite = "0.19.0"
toml = "0.7.6"
tracing = "0.1.37"
Expand Down
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
9 changes: 3 additions & 6 deletions crates/mako/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::sync::Arc;

use mako_core::anyhow::{anyhow, Result};
use mako_core::clap::Parser;
use mako_core::tokio;
#[cfg(feature = "profile")]
use mako_core::tokio::sync::Notify;
use mako_core::tracing::debug;
Expand Down Expand Up @@ -52,6 +51,7 @@ mod targets;
#[cfg(test)]
mod test_helper;
mod thread_pool;
mod tokio_runtime;
mod transform;
mod transform_in_generate;
mod transformers;
Expand All @@ -76,10 +76,7 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
fn main() -> Result<()> {
let fut = async { run().await };

tokio::runtime::Builder::new_current_thread()
.build()
.expect("Failed to create tokio runtime.")
.block_on(fut)
tokio_runtime::block_on(fut)
}

async fn run() -> Result<()> {
Expand Down Expand Up @@ -128,7 +125,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
25 changes: 25 additions & 0 deletions crates/mako/src/tokio_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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()
.enable_io()
.worker_threads(2)
.thread_name("tokio-worker")
.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)
}

pub fn block_on<F: Future>(future: F) -> F::Output {
TOKIO_RUNTIME.block_on(future)
}

0 comments on commit 8c57f53

Please sign in to comment.