[libshumate] map-source: Add fill_tile_async



commit 060fc44fccf567ddea2f962c4c6702da7e0f5540
Author: James Westman <james jwestman net>
Date:   Tue Mar 23 21:04:48 2021 -0500

    map-source: Add fill_tile_async
    
    Adds an asynchronous method for filling tiles, which calls a callback
    when the process is complete. Currently this is done by listening to the
    notify::state property of the tile, but in the future it should be
    implemented by the map source subclasses themselves.
    
    This has several advantages:
    - It makes it easier to write tests for map sources
    - Error handling can be done using the error parameter of the finish
      function, rather than with an error tile source
    - It will be much easier to move the memory cache out of the map source
      chain

 shumate/shumate-map-source.c | 71 ++++++++++++++++++++++++++++++++++++++++++++
 shumate/shumate-map-source.h |  8 +++++
 2 files changed, 79 insertions(+)
---
diff --git a/shumate/shumate-map-source.c b/shumate/shumate-map-source.c
index 20a8b81..1078993 100644
--- a/shumate/shumate-map-source.c
+++ b/shumate/shumate-map-source.c
@@ -551,3 +551,74 @@ shumate_map_source_fill_tile (ShumateMapSource *map_source,
   shumate_tile_set_state (tile, SHUMATE_STATE_LOADING);
   SHUMATE_MAP_SOURCE_GET_CLASS (map_source)->fill_tile (map_source, tile, cancellable);
 }
+
+
+static void on_tile_notify_state (GObject *object, GParamSpec *pspec, gpointer user_data);
+
+/**
+ * shumate_map_source_fill_tile_async:
+ * @self: a #ShumateMapSource
+ * @tile: a #ShumateTile
+ * @cancellable: (nullable): a #GCancellable
+ * @callback: a #GAsyncReadyCallback to execute upon completion
+ * @user_data: closure data for @callback
+ *
+ * Asynchronous version of shumate_map_source_fill_tile().
+ */
+void
+shumate_map_source_fill_tile_async (ShumateMapSource *self,
+                                    ShumateTile *tile,
+                                    GCancellable *cancellable,
+                                    GAsyncReadyCallback callback,
+                                    gpointer user_data)
+{
+  g_autoptr(GTask) task = NULL;
+
+  g_return_if_fail (SHUMATE_IS_MAP_SOURCE (self));
+  g_return_if_fail (SHUMATE_IS_TILE (tile));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, shumate_map_source_fill_tile_async);
+
+  g_signal_connect (tile, "notify::state", G_CALLBACK (on_tile_notify_state), g_object_ref (task));
+
+  shumate_map_source_fill_tile (self, tile, cancellable);
+}
+
+static void
+on_tile_notify_state (GObject *object, GParamSpec *pspec, gpointer user_data)
+{
+  GTask *task = user_data;
+  ShumateTile *tile = SHUMATE_TILE (object);
+
+  if (shumate_tile_get_state (tile) == SHUMATE_STATE_DONE)
+    {
+      g_signal_handlers_disconnect_by_data (object, user_data);
+      g_task_return_boolean (task, TRUE);
+      g_object_unref (task);
+      return;
+    }
+}
+
+/**
+ * shumate_map_source_fill_tile_finish:
+ * @self: a #ShumateMapSource
+ * @result: a #GAsyncResult provided to callback
+ * @error: a location for a #GError, or %NULL
+ *
+ * Gets the success value of a completed shumate_map_source_fill_tile_async()
+ * operation.
+ *
+ * Returns: %TRUE if the tile was filled with valid data, otherwise %FALSE
+ */
+gboolean
+shumate_map_source_fill_tile_finish (ShumateMapSource *self,
+                                     GAsyncResult *result,
+                                     GError **error)
+{
+  g_return_val_if_fail (SHUMATE_IS_MAP_SOURCE (self), FALSE);
+  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
diff --git a/shumate/shumate-map-source.h b/shumate/shumate-map-source.h
index 4a3326c..09ee4e9 100644
--- a/shumate/shumate-map-source.h
+++ b/shumate/shumate-map-source.h
@@ -104,6 +104,14 @@ double shumate_map_source_get_meters_per_pixel (ShumateMapSource *map_source,
 void shumate_map_source_fill_tile (ShumateMapSource *map_source,
                                    ShumateTile      *tile,
                                    GCancellable     *cancellable);
+void shumate_map_source_fill_tile_async (ShumateMapSource    *self,
+                                         ShumateTile         *tile,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data);
+gboolean shumate_map_source_fill_tile_finish (ShumateMapSource *self,
+                                              GAsyncResult     *result,
+                                              GError          **error);
 
 G_END_DECLS
 


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