LibWeb/IDB: Implement fire_an_error_event

This commit is contained in:
stelar7 2025-04-11 11:09:16 +02:00 committed by Andrew Kaster
commit 5c814e1a62
Notes: github-actions[bot] 2025-04-23 18:38:00 +00:00
2 changed files with 46 additions and 0 deletions

View file

@ -1025,4 +1025,49 @@ bool check_that_a_key_could_be_injected_into_a_value(JS::Realm& realm, JS::Value
return value.is_object() || MUST(value.is_array(realm.vm()));
}
// https://w3c.github.io/IndexedDB/#fire-an-error-event
void fire_an_error_event(JS::Realm& realm, GC::Ref<IDBRequest> request)
{
// 1. Let event be the result of creating an event using Event.
// 2. Set events type attribute to "error".
// 3. Set events bubbles and cancelable attributes to true.
auto event = DOM::Event::create(realm, HTML::EventNames::error, { .bubbles = true, .cancelable = true });
// 4. Let transaction be requests transaction.
auto transaction = request->transaction();
// 5. Let legacyOutputDidListenersThrowFlag be initially false.
bool legacy_output_did_listeners_throw_flag = false;
// 6. If transactions state is inactive, then set transactions state to active.
if (transaction->state() == IDBTransaction::TransactionState::Inactive)
transaction->set_state(IDBTransaction::TransactionState::Active);
// 7. Dispatch event at request with legacyOutputDidListenersThrowFlag.
DOM::EventDispatcher::dispatch(request, *event, false, legacy_output_did_listeners_throw_flag);
// 8. If transactions state is active, then:
if (transaction->state() == IDBTransaction::TransactionState::Active) {
// 1. Set transactions state to inactive.
transaction->set_state(IDBTransaction::TransactionState::Inactive);
// 2. If legacyOutputDidListenersThrowFlag is true, then run abort a transaction with transaction and a newly created "AbortError" DOMException and terminate these steps.
// This is done even if events canceled flag is false.
if (legacy_output_did_listeners_throw_flag) {
abort_a_transaction(*transaction, WebIDL::AbortError::create(realm, "Error event interrupted by exception"_string));
return;
}
// 3. If events canceled flag is false, then run abort a transaction using transaction and request's error, and terminate these steps.
if (!event->cancelled()) {
abort_a_transaction(*transaction, request->error());
return;
}
// 4. If transactions request list is empty, then run commit a transaction with transaction.
if (transaction->request_list().is_empty())
commit_a_transaction(realm, *transaction);
}
}
}