1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 17:25:07 +00:00

LibWeb: Add ResourceLoader::load_sync()

This function creates a nested event loop and runs a load() operation
inside it, returning only once the load has either succeeded or failed.

This will be used to implement blocking loads (ew!)
This commit is contained in:
Andreas Kling 2020-04-03 22:58:05 +02:00
parent eeec1c1293
commit 18d45d1082
2 changed files with 20 additions and 0 deletions

View file

@ -25,6 +25,7 @@
*/
#include <AK/SharedBuffer.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
@ -45,6 +46,24 @@ ResourceLoader::ResourceLoader()
{
}
void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
{
Core::EventLoop loop;
load(
url,
[&](auto& data) {
success_callback(data);
loop.quit(0);
},
[&](auto& string) {
error_callback(string);
loop.quit(0);
});
loop.exec();
}
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
{
if (url.protocol() == "file") {