| 366 | explicit Callback(std::shared_ptr<State> state) : state_(std::move(state)) {} |
| 367 | |
| 368 | void operator()(const Result<T> result) { |
| 369 | Future<T> to_deliver; |
| 370 | bool finished; |
| 371 | { |
| 372 | auto guard = state_->mutex.Lock(); |
| 373 | bool ready_to_deliver = false; |
| 374 | if (!result.ok()) { |
| 375 | // Clear any cached results |
| 376 | while (!state_->queue.empty()) { |
| 377 | state_->queue.pop(); |
| 378 | } |
| 379 | ready_to_deliver = true; |
| 380 | state_->finished = true; |
| 381 | } else if (IsIterationEnd<T>(result.ValueUnsafe())) { |
| 382 | ready_to_deliver = state_->queue.empty(); |
| 383 | state_->finished = true; |
| 384 | } else { |
| 385 | ready_to_deliver = state_->is_next(state_->previous_value, *result); |
| 386 | } |
| 387 | |
| 388 | if (ready_to_deliver && state_->waiting_future.is_valid()) { |
| 389 | to_deliver = state_->waiting_future; |
| 390 | if (result.ok()) { |
| 391 | state_->previous_value = *result; |
| 392 | } |
| 393 | } else { |
| 394 | state_->queue.push(result); |
| 395 | } |
| 396 | // Capture state_->finished so we can access it outside the mutex |
| 397 | finished = state_->finished; |
| 398 | } |
| 399 | // Must deliver result outside of the mutex |
| 400 | if (to_deliver.is_valid()) { |
| 401 | to_deliver.MarkFinished(result); |
| 402 | } else { |
| 403 | // Otherwise, if we didn't get the next item (or a terminal item), we |
| 404 | // need to keep looking |
| 405 | if (!finished) { |
| 406 | state_->source().AddCallback(Callback{state_}); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private: |
| 412 | const std::shared_ptr<State> state_; |
nothing calls this directly
no test coverage detected