Skip to content

Commit

Permalink
feat(helper)[image]: avoid nested asyncs
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr committed Feb 15, 2024
1 parent 43ffe5e commit d7b3f51
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/Services/Helpers/Image.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class Tuba.Helper.Image {
throw new Oopsie.INSTANCE (@"Server returned $(msg.reason_phrase)");
}

// if (msg.response_headers.get_content_type (null) == "image/gif") {
// // return Gtk.MediaFile.for_input_stream (in_stream);
// }

return Gdk.Texture.for_pixbuf ((yield new Gdk.Pixbuf.from_stream_async (in_stream)).apply_embedded_orientation ());
}

Expand Down Expand Up @@ -49,6 +53,17 @@ public class Tuba.Helper.Image {
}
}

private static async Gdk.Paintable? fetch_paintable (string url) {
var download_msg = new Soup.Message ("GET", url);
try {
var in_stream = yield session.send_async (download_msg, 0, null);
return yield decode (download_msg, in_stream);
} catch (Error e) {
warning (@"Failed to download image at \"$url\": $(e.message)");
return null;
}
}

public static void request_paintable (string? url, string? blurhash, owned OnItemChangedFn cb) {
if (url == null || url == "") return;
new Helper.Image ();
Expand All @@ -64,30 +79,9 @@ public class Tuba.Helper.Image {
});
}

var download_msg = new Soup.Message ("GET", url);
session.send_async.begin (download_msg, 0, null, (obj, res) => {
try {
var in_stream = session.send_async.end (res);

decode.begin (download_msg, in_stream, (obj, async_res) => {
has_loaded = true;
Gdk.Paintable? paintable = null;
try {
paintable = decode.end (async_res);
} catch (Error e) {
warning (@"Failed to download image at \"$url\": $(e.message)");
cb (null);

return;
}

cb (paintable);
});
} catch (GLib.Error e) {
warning (e.message);
has_loaded = true;
cb (null);
}
fetch_paintable.begin (url, (obj, res) => {
cb (fetch_paintable.end (res));
has_loaded = true;
});
}
}

0 comments on commit d7b3f51

Please sign in to comment.