From 5dfc6c0c97686ff01539fdf9548678fa507c5167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Thu, 10 Jun 2021 18:34:21 +0800 Subject: Anticipate asynchronous data fetch --- client.cxx | 21 +++++++++++++++++---- qicclient/client.hxx | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/client.cxx b/client.cxx index ff28eaa..6e4a3f4 100644 --- a/client.cxx +++ b/client.cxx @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "qicclient/catalog.hxx" #ifndef __EMSCRIPTEN__ @@ -9,10 +10,19 @@ #endif #include "qicclient/client.hxx" +static QICClient::Client *client; + +static void callback(icclient_catalog* catalog) +{ + client->emitCatalog(catalog); + icclient_catalog_free(catalog); +} + namespace QICClient { Client::Client(char const* url, char const* certificate) { + client = this; icclient_init(url, certificate); } @@ -24,15 +34,18 @@ namespace QICClient { void Client::results(QString const& prodGroup, icclient_handler handler) { icclient_catalog* catalog = nullptr; - icclient_results(prodGroup.toLatin1().constData(), handler, &catalog); - if (catalog) emit gotResults(new Catalog{catalog}); + icclient_results(prodGroup.toLatin1().constData(), callback, &catalog, handler); } void Client::allProducts(icclient_handler handler) { icclient_catalog* catalog = nullptr; - icclient_allproducts(handler, &catalog); - if (catalog) emit gotResults(new Catalog{catalog}); + icclient_allproducts(callback, &catalog, handler); + } + + void Client::emitCatalog(icclient_catalog* catalog) + { + emit gotResults(new Catalog{catalog}); } void Client::flyPage(QString const& sku,icclient_handler handler) diff --git a/qicclient/client.hxx b/qicclient/client.hxx index dfe1888..d508b16 100644 --- a/qicclient/client.hxx +++ b/qicclient/client.hxx @@ -38,6 +38,8 @@ namespace QICClient { */ void allProducts(icclient_handler handler); + void emitCatalog(icclient_catalog *catalog); + /*! * \brief For fetching data about a specific product. * \param sku The SKU of the item to order. -- cgit v1.2.3