Skip to content

Commit

Permalink
invoke callback
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylEnkidu committed Jan 3, 2025
1 parent cbbb54c commit 6265d7b
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions Firestore/core/swift/src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,48 @@ void Pipeline::GetPipelineResult(PipelineSnapshotListener callback) const {
/*include_document_metadata_changes=*/true,
/*wait_for_sync_when_online=*/true);

class ListenOnce : public EventListener<std::vector<PipelineResult>> {
public:
ListenOnce(PipelineSnapshotListener listener)
: listener_(std::move(listener)) {
}

void OnEvent(
StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
if (!maybe_snapshot.ok()) {
listener_->OnEvent(std::move(maybe_snapshot));
return;
}

std::vector<PipelineResult> snapshot =
std::move(maybe_snapshot).ValueOrDie();

// Remove query first before passing event to user to avoid user actions
// affecting the now stale query.
std::unique_ptr<ListenerRegistration> registration =
registration_promise_.get_future().get();
registration->Remove();

listener_->OnEvent(std::move(snapshot));
};

void Resolve(std::unique_ptr<ListenerRegistration> registration) {
registration_promise_.set_value(std::move(registration));
}

private:
PipelineSnapshotListener listener_;

std::promise<std::unique_ptr<ListenerRegistration>> registration_promise_;
};

auto listener = absl::make_unique<ListenOnce>(std::move(callback));
auto* listener_unowned = listener.get();
callback->OnEvent(StatusOr<std::vector<PipelineResult>>(
{(PipelineResult::GetTestResult(firestore_))}));

// class ListenOnce : public EventListener<std::vector<PipelineResult>> {
// public:
// ListenOnce(PipelineSnapshotListener listener)
// : listener_(std::move(listener)) {
// }
//
// void OnEvent(
// StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
// if (!maybe_snapshot.ok()) {
// listener_->OnEvent(std::move(maybe_snapshot));
// return;
// }
//
// std::vector<PipelineResult> snapshot =
// std::move(maybe_snapshot).ValueOrDie();
//
// // Remove query first before passing event to user to avoid user
// actions
// // affecting the now stale query.
// std::unique_ptr<ListenerRegistration> registration =
// registration_promise_.get_future().get();
// registration->Remove();
//
// listener_->OnEvent(std::move(snapshot));
// };
//
// void Resolve(std::unique_ptr<ListenerRegistration> registration) {
// registration_promise_.set_value(std::move(registration));
// }
//
// private:
// PipelineSnapshotListener listener_;
//
// std::promise<std::unique_ptr<ListenerRegistration>>
// registration_promise_;
// };
//
// auto listener = absl::make_unique<ListenOnce>(std::move(callback));
// auto* listener_unowned = listener.get();

// std::unique_ptr<ListenerRegistration> registration =
// AddSnapshotListener(std::move(options), std::move(listener));
Expand Down

0 comments on commit 6265d7b

Please sign in to comment.