|
| 1 | +// TODO If possible, calloop could poll inotify/kqueue without a thread |
| 2 | + |
| 3 | +use calloop::channel; |
| 4 | + |
| 5 | +use crate::{Config, Error}; |
| 6 | + |
| 7 | +pub struct ConfigWatchSource { |
| 8 | + channel: channel::Channel<(Config, Vec<String>)>, |
| 9 | + _watcher: notify::RecommendedWatcher, |
| 10 | +} |
| 11 | + |
| 12 | +impl ConfigWatchSource { |
| 13 | + pub fn new(config: &Config) -> Result<Self, Error> { |
| 14 | + let (sender, channel) = channel::sync_channel(32); |
| 15 | + let _watcher = config.watch(move |config, keys| { |
| 16 | + let _ = sender.send((config.clone(), keys.to_owned())); |
| 17 | + })?; |
| 18 | + Ok(Self { channel, _watcher }) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl calloop::EventSource for ConfigWatchSource { |
| 23 | + type Event = (Config, Vec<String>); |
| 24 | + type Metadata = (); |
| 25 | + type Ret = (); |
| 26 | + type Error = calloop::channel::ChannelError; |
| 27 | + |
| 28 | + fn process_events<F>( |
| 29 | + &mut self, |
| 30 | + readiness: calloop::Readiness, |
| 31 | + token: calloop::Token, |
| 32 | + mut cb: F, |
| 33 | + ) -> Result<calloop::PostAction, Self::Error> |
| 34 | + where |
| 35 | + F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret, |
| 36 | + { |
| 37 | + self.channel |
| 38 | + .process_events(readiness, token, |event, ()| match event { |
| 39 | + calloop::channel::Event::Msg(msg) => cb(msg, &mut ()), |
| 40 | + calloop::channel::Event::Closed => {} |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + fn register( |
| 45 | + &mut self, |
| 46 | + poll: &mut calloop::Poll, |
| 47 | + token_factory: &mut calloop::TokenFactory, |
| 48 | + ) -> Result<(), calloop::Error> { |
| 49 | + self.channel.register(poll, token_factory) |
| 50 | + } |
| 51 | + |
| 52 | + fn reregister( |
| 53 | + &mut self, |
| 54 | + poll: &mut calloop::Poll, |
| 55 | + token_factory: &mut calloop::TokenFactory, |
| 56 | + ) -> Result<(), calloop::Error> { |
| 57 | + self.channel.reregister(poll, token_factory) |
| 58 | + } |
| 59 | + |
| 60 | + fn unregister(&mut self, poll: &mut calloop::Poll) -> Result<(), calloop::Error> { |
| 61 | + self.channel.unregister(poll) |
| 62 | + } |
| 63 | +} |
0 commit comments