mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:18:14 +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:
parent
eeec1c1293
commit
18d45d1082
2 changed files with 20 additions and 0 deletions
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/SharedBuffer.h>
|
#include <AK/SharedBuffer.h>
|
||||||
|
#include <LibCore/EventLoop.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibProtocol/Client.h>
|
#include <LibProtocol/Client.h>
|
||||||
#include <LibProtocol/Download.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)
|
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
|
||||||
{
|
{
|
||||||
if (url.protocol() == "file") {
|
if (url.protocol() == "file") {
|
||||||
|
|
|
@ -42,6 +42,7 @@ public:
|
||||||
static ResourceLoader& the();
|
static ResourceLoader& the();
|
||||||
|
|
||||||
void load(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
|
void load(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
|
||||||
|
void load_sync(const URL&, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback = nullptr);
|
||||||
|
|
||||||
Function<void()> on_load_counter_change;
|
Function<void()> on_load_counter_change;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue