(&mut self, dim: NodeDim)
| 146 | } |
| 147 | |
| 148 | pub fn get_sixel(&mut self, dim: NodeDim) -> Option<Result<Sixel>> { |
| 149 | let Node { sixel_cache, state, content, .. } = self; |
| 150 | |
| 151 | // first check the SIXEL blob cache |
| 152 | if let Some(data) = (*sixel_cache.read().unwrap()).get(&dim) { |
| 153 | return Some(Ok(data.clone())); |
| 154 | } |
| 155 | |
| 156 | let state_cont = std::mem::replace(&mut *state.write().unwrap(), ContentState::Empty); |
| 157 | |
| 158 | let (res, state_cont) = match state_cont { |
| 159 | ContentState::Empty => { |
| 160 | let state_cloned = state.clone(); |
| 161 | let content = content.clone(); |
| 162 | thread::spawn(move || { |
| 163 | let res = content.1.generate(content.0); |
| 164 | |
| 165 | *state_cloned.write().unwrap() = match res { |
| 166 | Ok(res) => ContentState::Ok(res), |
| 167 | Err(err) => ContentState::Err(err), |
| 168 | }; |
| 169 | }); |
| 170 | |
| 171 | (None, ContentState::Running) |
| 172 | }, |
| 173 | ContentState::Err(error) => |
| 174 | (Some(Err(error)), ContentState::Empty), |
| 175 | ContentState::Ok(content) => { |
| 176 | // start thread to calculate SIXEL blob |
| 177 | let sixel_cache = sixel_cache.clone(); |
| 178 | let state = state.clone(); |
| 179 | |
| 180 | thread::spawn(move || { |
| 181 | let res = content.clone().wand_to_sixel(dim.clone()); |
| 182 | sixel_cache.write().unwrap().insert(dim, res); |
| 183 | *state.write().unwrap() = ContentState::Ok(content); |
| 184 | }); |
| 185 | |
| 186 | (None, ContentState::Running) |
| 187 | }, |
| 188 | ContentState::Running => (None, ContentState::Running), |
| 189 | }; |
| 190 | |
| 191 | let _ = std::mem::replace(&mut *state.write().unwrap(), state_cont); |
| 192 | |
| 193 | res |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | pub struct Content { |
no test coverage detected