summaryrefslogtreecommitdiffstats
path: root/src/context/context.rs
blob: 2cf26615b241cc655222a92626d8922248d61f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::collections::VecDeque;
use std::sync::mpsc;
use std::thread;

use crate::config;
use crate::context::{LocalStateContext, TabContext};
use crate::io::{IOWorkerObserver, IOWorkerThread};
use crate::util::event::{Event, Events};

pub struct JoshutoContext {
    pub exit: bool,
    pub config_t: config::JoshutoConfig,
    events: Events,
    tab_context: TabContext,
    local_state: Option<LocalStateContext>,
    message_queue: VecDeque<String>,
    worker_queue: VecDeque<IOWorkerThread>,
    worker: Option<IOWorkerObserver>,
}

impl JoshutoContext {
    pub fn new(config_t: config::JoshutoConfig) -> Self {
        Self {
            exit: false,
            events: Events::new(),
            tab_context: TabContext::new(),
            local_state: None,
            message_queue: VecDeque::with_capacity(4),
            worker_queue: VecDeque::new(),
            worker: None,
            config_t,
        }
    }

    pub fn tab_context_ref(&self) -> &TabContext {
        &self.tab_context
    }
    pub fn tab_context_mut(&mut self) -> &mut TabContext {
        &mut self.tab_context
    }

    pub fn message_queue_ref(&self) -> &VecDeque<String> {
        &self.message_queue
    }
    pub fn push_msg(&mut self, msg: String) {
        self.message_queue.push_back(msg);
    }
    pub fn pop_msg(&mut self) -> Option<String> {
        self.message_queue.pop_front()
    }

    // event related
    pub fn poll_event(&self) -> Result<Event, mpsc::RecvError> {
        self.events.next()
    }
    pub fn get_event_tx(&self) -> mpsc::Sender<Event> {
        self.events.event_tx.clone()
    }
    pub fn flush_event(&self) {
        self.events.flush();
    }

    // local state related
    pub fn set_local_state(&mut self, state: LocalStateContext) {
        self.local_state = Some(state);
    }
    pub fn get_local_state(&self) -> Option<&LocalStateContext> {
        self.local_state.as_ref()
    }
    pub fn take_local_state(&mut self) -> Option<LocalStateContext> {
        self.local_state.take()
    }

    // worker related
    pub fn add_worker(&mut self, thread: IOWorkerThread) {
        self.worker_queue.push_back(thread);
    }
    pub fn worker_is_busy(&self) -> bool {
        self.worker.is_some()
    }
    pub fn worker_len(&self) -> usize {
        self.worker_queue.len()
    }
    pub fn worker_is_empty(&self) -> bool {
        self.worker_queue.is_empty()
    }
    pub fn set_worker_msg(&mut self, msg: String) {
        if let Some(s) = self.worker.as_mut() {
            s.set_msg(msg);
        }
    }
    pub fn worker_msg(&self) -> Option<&str> {
        let worker = self.worker.as_ref()?;
        Some(worker.get_msg())
    }

    pub fn start_next_job(&mut self) {
        let tx = self.get_event_tx();

        if let Some(worker) = self.worker_queue.pop_front() {
            let src = worker.paths[0].clone();
            let dest = worker.dest.clone();
            let handle = thread::spawn(move || {
                let (wtx, wrx) = mpsc::channel();
                // start worker
                let worker_handle = thread::spawn(move || worker.start(wtx));
                // relay worker info to event loop
                while let Ok(progress) = wrx.recv() {
                    tx.send(Event::IOWorkerProgress(progress));
                }
                let result = worker_handle.join();

                match result {
                    Ok(res) => {
                        let _ = tx.send(Event::IOWorkerResult(res));
                    }
                    Err(e) => {
                        let err = std::io::Error::new(std::io::ErrorKind::Other, "Sending Error");
                        let _ = tx.send(Event::IOWorkerResult(Err(err)));
                    }
                }
            });
            let observer = IOWorkerObserver::new(handle, src, dest);
            self.worker = Some(observer);
        }
    }

    pub fn remove_job(&mut self) -> Option<IOWorkerObserver> {
        self.worker.take()
    }
}