[gnome-software/1731-search-for-apps-text-shows-up-instead-of-a-spinner-screen-randomly: 15/15] gs-search-page: Ignore finished non-current searches




commit dfe013035ed6e8d348c5bfdb9abfc69ac0f8be8c
Author: Milan Crha <mcrha redhat com>
Date:   Mon Aug 22 12:54:23 2022 +0200

    gs-search-page: Ignore finished non-current searches
    
    The search can take various time and even the cancellable is cancelled
    due to the search term change or anything like that, the code can be
    far away from the place where the cancellable could be checked, which
    means even the search was obsolete it could modify the content of the page.
    
    Let the code check whether the finished search is still the current
    search and ignore any result from it when it's an obsolete search.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1731

 src/gs-search-page.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
---
diff --git a/src/gs-search-page.c b/src/gs-search-page.c
index b86f7389a..37e3b9cc7 100644
--- a/src/gs-search-page.c
+++ b/src/gs-search-page.c
@@ -96,19 +96,29 @@ gs_search_page_app_to_show_created_cb (GObject *source_object,
        }
 }
 
+typedef struct {
+       GsSearchPage *self;
+       GCancellable *search_cancellable;
+} GetSearchData;
+
 static void
 gs_search_page_get_search_cb (GObject *source_object,
                               GAsyncResult *res,
                               gpointer user_data)
 {
        guint i;
+       g_autofree GetSearchData *search_data = user_data;
        GsApp *app;
-       GsSearchPage *self = GS_SEARCH_PAGE (user_data);
+       GsSearchPage *self = search_data->self;
        GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
        GtkWidget *app_row;
        g_autoptr(GError) error = NULL;
        g_autoptr(GsAppList) list = NULL;
 
+       /* different cancellable means another search had been started before this one finished */
+       if (search_data->search_cancellable != self->search_cancellable)
+               return;
+
        /* don't do the delayed spinner */
        gs_search_page_waiting_cancel (self);
 
@@ -265,6 +275,7 @@ gs_search_page_load (GsSearchPage *self)
        g_autoptr(GsPluginJob) plugin_job = NULL;
        g_autoptr(GsAppQuery) query = NULL;
        const gchar *keywords[2] = { NULL, };
+       g_autofree GetSearchData *search_data = NULL;
 
        self->changed = FALSE;
 
@@ -277,6 +288,11 @@ gs_search_page_load (GsSearchPage *self)
        gs_search_page_waiting_cancel (self);
        self->waiting_id = g_timeout_add (250, gs_search_page_waiting_show_cb, self);
 
+       search_data = g_new0 (GetSearchData, 1);
+       search_data->self = self;
+       /* use the cancellable as an indication that another search had been started before this one finished 
*/
+       search_data->search_cancellable = self->search_cancellable;
+
        keywords[0] = self->value;
        query = gs_app_query_new ("keywords", keywords,
                                  "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON |
@@ -298,7 +314,7 @@ gs_search_page_load (GsSearchPage *self)
        gs_plugin_loader_job_process_async (self->plugin_loader, plugin_job,
                                            self->search_cancellable,
                                            gs_search_page_get_search_cb,
-                                           self);
+                                           g_steal_pointer (&search_data));
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]