(executor: Executor)
| 97 | |
| 98 | impl DelayExecutor { |
| 99 | pub(crate) fn new(executor: Executor) -> (Self, DelayTX) { |
| 100 | let (tx, mut rx) = tokio::sync::mpsc::channel::<Command>(10); |
| 101 | let (skip_tx, mut skip_rx) = tokio::sync::mpsc::channel::<()>(10); |
| 102 | let (clear_tx, mut clear_rx) = tokio::sync::mpsc::channel::<()>(10); |
| 103 | |
| 104 | let timer = slint::Timer::default(); |
| 105 | let command = Arc::new(RwLock::new(VecDeque::new())); |
| 106 | let command_clone = command.clone(); |
| 107 | |
| 108 | let executor = Self { |
| 109 | timer, |
| 110 | executor, |
| 111 | command, |
| 112 | }; |
| 113 | tokio::spawn(async move { |
| 114 | let mut current_figure: VecDeque<Command> = VecDeque::new(); |
| 115 | |
| 116 | loop { |
| 117 | tokio::select! { |
| 118 | Some(figure) = rx.recv()=> { |
| 119 | current_figure.push_back(figure); |
| 120 | } |
| 121 | |
| 122 | // 延迟完成 |
| 123 | _ = async { |
| 124 | if let Some(Command::Figure {delay, ..}) |
| 125 | | Some(Command::Move {delay, ..}) = current_figure.front(){ |
| 126 | sleep(Duration::from_millis( |
| 127 | delay.clone().unwrap_or_default().parse::<u64>().unwrap_or(0), |
| 128 | )).await; |
| 129 | } else { |
| 130 | std::future::pending::<()>().await |
| 131 | } |
| 132 | } => { |
| 133 | if let Some(cmd) = current_figure.pop_front() { |
| 134 | command_clone.write().unwrap().push_back(cmd); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // 重置请求 |
| 139 | _ = skip_rx.recv() => { |
| 140 | while let Some(figure) = current_figure.pop_front() { |
| 141 | if let Command::Figure {..} = figure { |
| 142 | command_clone.write().unwrap().push_back(figure); |
| 143 | } else if let Command::Move {..} = figure { |
| 144 | if figure.action() != "nod" { |
| 145 | command_clone.write().unwrap().push_back(figure) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // 清空请求 |
| 152 | _ = clear_rx.recv() => { |
| 153 | current_figure.clear(); |
| 154 | } |
| 155 | } |
| 156 | } |
nothing calls this directly
no outgoing calls
no test coverage detected