1
0
mirror of synced 2025-11-09 21:07:12 +00:00
This commit is contained in:
Tom Gowan
2019-04-26 21:52:18 +10:00
parent 4694d4eb1d
commit 6eca607ca3
3 changed files with 31 additions and 24 deletions

View File

@@ -1,12 +1,12 @@
use std::sync::mpsc;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::thread;
use std::time::Duration;
use std::path::{Path, PathBuf};
use crate::error::Error;
use crate::layouts::Entry;
use crate::CompiledShaders;
use std::sync::mpsc::{Sender, Receiver};
use crate::error::Error;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;
pub struct Watch {
_handler: Handler,
@@ -26,18 +26,24 @@ pub struct Message {
impl Watch {
pub fn new<T>(vertex: T, fragment: T) -> Self
where
where
T: AsRef<Path>,
{
let (handler, rx) = create_watch(vertex.as_ref().to_path_buf(), fragment.as_ref().to_path_buf());
Watch{_handler: handler, rx}
{
let (handler, rx) = create_watch(
vertex.as_ref().to_path_buf(),
fragment.as_ref().to_path_buf(),
);
Watch {
_handler: handler,
rx,
}
}
}
impl Loader {
fn create(vertex: PathBuf, fragment: PathBuf) -> (Self, Receiver<Result<Message, Error>>) {
let (tx, rx) = mpsc::channel();
let loader = Loader{
let (tx, rx) = mpsc::channel();
let loader = Loader {
vertex,
fragment,
tx,
@@ -50,8 +56,8 @@ impl Loader {
match crate::load(&self.vertex, &self.fragment) {
Ok(shaders) => {
let entry = crate::parse(&shaders);
self.tx.send(Ok(Message{ shaders, entry })).ok()
},
self.tx.send(Ok(Message { shaders, entry })).ok()
}
Err(e) => self.tx.send(Err(e)).ok(),
};
}
@@ -72,8 +78,10 @@ impl Drop for Handler {
}
}
fn create_watch(vert_path: PathBuf, frag_path: PathBuf) -> (Handler, mpsc::Receiver<Result<Message, Error>>) {
fn create_watch(
vert_path: PathBuf,
frag_path: PathBuf,
) -> (Handler, mpsc::Receiver<Result<Message, Error>>) {
let (notify_tx, notify_rx) = mpsc::channel();
let (thread_tx, thread_rx) = mpsc::channel();
let mut watcher: RecommendedWatcher =
@@ -86,14 +94,15 @@ fn create_watch(vert_path: PathBuf, frag_path: PathBuf) -> (Handler, mpsc::Recei
.watch(&frag_path, RecursiveMode::NonRecursive)
.expect("failed to add fragment shader to notify");
let (loader, rx) = Loader::create(vert_path, frag_path);
let handle = thread::spawn(move || 'watch_loop: loop {
if let Ok(_) = thread_rx.try_recv() {
break 'watch_loop;
}
if let Ok(notify::DebouncedEvent::Create(_)) = notify_rx.recv_timeout(Duration::from_secs(1)) {
if let Ok(notify::DebouncedEvent::Create(_)) =
notify_rx.recv_timeout(Duration::from_secs(1))
{
loader.reload();
}
});