mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:47:35 +00:00
LibWeb: Always defer callbacks in ResourceClient::set_resource()
Previously, we'd invoke the load/fail callbacks synchronously for resources that were already loaded and cached. This patch uses deferred_invoke() in the already-loaded case to ensure that we always invoke these callbacks in a consistent manner. This isn't to fix a specific issue, but rather because I kept seeing these callbacks being invoked synchronously on top of an already-tall call stack, and it was hard to reason about what was going on.
This commit is contained in:
parent
d60b3be29a
commit
d03680a9e7
1 changed files with 17 additions and 6 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||
#include <LibWeb/Loader/Resource.h>
|
||||
|
@ -141,13 +142,23 @@ void ResourceClient::set_resource(Resource* resource)
|
|||
|
||||
m_resource->register_client({}, *this);
|
||||
|
||||
// Make sure that reused resources also have their load callback fired.
|
||||
if (resource->is_loaded())
|
||||
resource_did_load();
|
||||
// For resources that are already loaded, we fire their load/fail callbacks via the event loop.
|
||||
// This ensures that these callbacks always happen in a consistent way, instead of being invoked
|
||||
// synchronously in some cases, and asynchronously in others.
|
||||
if (resource->is_loaded() || resource->is_failed()) {
|
||||
Core::deferred_invoke([this, strong_resource = NonnullRefPtr { *m_resource }] {
|
||||
if (m_resource != strong_resource.ptr())
|
||||
return;
|
||||
|
||||
// Make sure that reused resources also have their fail callback fired.
|
||||
if (resource->is_failed())
|
||||
resource_did_fail();
|
||||
// Make sure that reused resources also have their load callback fired.
|
||||
if (m_resource->is_loaded())
|
||||
resource_did_load();
|
||||
|
||||
// Make sure that reused resources also have their fail callback fired.
|
||||
if (m_resource->is_failed())
|
||||
resource_did_fail();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue