mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-12 02:59:45 +00:00
LibDevTools: Add a helper to handle asynchronous request replies
This removes some boilerplate around executing async requests, such as calling dbgln_if on any errors, handling weak pointers to `this`, and dealing with block tokens.
This commit is contained in:
parent
e1ed8722e0
commit
49adec1396
Notes:
github-actions[bot]
2025-03-12 16:48:58 +00:00
Author: https://github.com/trflynn89
Commit: 49adec1396
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3904
9 changed files with 140 additions and 283 deletions
|
@ -4,7 +4,6 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/Time.h>
|
||||
|
@ -15,6 +14,18 @@
|
|||
|
||||
namespace DevTools {
|
||||
|
||||
static void received_console_result(JsonObject& response, String result_id, String input, JsonValue result)
|
||||
{
|
||||
response.set("type"sv, "evaluationResult"_string);
|
||||
response.set("timestamp"sv, AK::UnixDateTime::now().milliseconds_since_epoch());
|
||||
response.set("resultID"sv, move(result_id));
|
||||
response.set("input"sv, move(input));
|
||||
response.set("result"sv, move(result));
|
||||
response.set("exception"sv, JsonValue {});
|
||||
response.set("exceptionMessage"sv, JsonValue {});
|
||||
response.set("helperResult"sv, JsonValue {});
|
||||
}
|
||||
|
||||
NonnullRefPtr<ConsoleActor> ConsoleActor::create(DevToolsServer& devtools, String name, WeakPtr<TabActor> tab)
|
||||
{
|
||||
return adopt_ref(*new ConsoleActor(devtools, move(name), move(tab)));
|
||||
|
@ -55,18 +66,10 @@ void ConsoleActor::handle_message(StringView type, JsonObject const& message)
|
|||
}
|
||||
|
||||
if (auto tab = m_tab.strong_ref()) {
|
||||
auto block_token = block_responses();
|
||||
|
||||
devtools().delegate().evaluate_javascript(tab->description(), *text,
|
||||
[result_id, input = *text, weak_self = make_weak_ptr<ConsoleActor>(), block_token = move(block_token)](ErrorOr<JsonValue> result) mutable {
|
||||
if (result.is_error()) {
|
||||
dbgln_if(DEVTOOLS_DEBUG, "Unable to inspect DOM node: {}", result.error());
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto self = weak_self.strong_ref())
|
||||
self->received_console_result(move(result_id), move(input), result.release_value(), move(block_token));
|
||||
});
|
||||
async_handler([result_id, input = *text](auto&, auto result, auto& response) {
|
||||
received_console_result(response, move(result_id), move(input), move(result));
|
||||
}));
|
||||
}
|
||||
|
||||
return;
|
||||
|
@ -75,19 +78,4 @@ void ConsoleActor::handle_message(StringView type, JsonObject const& message)
|
|||
send_unrecognized_packet_type_error(type);
|
||||
}
|
||||
|
||||
void ConsoleActor::received_console_result(String result_id, String input, JsonValue result, BlockToken block_token)
|
||||
{
|
||||
JsonObject message;
|
||||
message.set("type"sv, "evaluationResult"_string);
|
||||
message.set("timestamp"sv, AK::UnixDateTime::now().milliseconds_since_epoch());
|
||||
message.set("resultID"sv, move(result_id));
|
||||
message.set("input"sv, move(input));
|
||||
message.set("result"sv, move(result));
|
||||
message.set("exception"sv, JsonValue {});
|
||||
message.set("exceptionMessage"sv, JsonValue {});
|
||||
message.set("helperResult"sv, JsonValue {});
|
||||
|
||||
send_message(move(message), move(block_token));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue