From abb11a26a806f2f566f4c7bdd660f279d13b138c Mon Sep 17 00:00:00 2001 From: stelar7 Date: Fri, 11 Apr 2025 11:10:05 +0200 Subject: [PATCH] LibWeb/IDB: Implement fire_a_success_event --- .../LibWeb/IndexedDB/Internal/Algorithms.cpp | 38 +++++++++++++++++++ .../LibWeb/IndexedDB/Internal/Algorithms.h | 1 + 2 files changed, 39 insertions(+) diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index a515b616235..359b5d593a6 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -1070,4 +1070,42 @@ void fire_an_error_event(JS::Realm& realm, GC::Ref request) } } +// https://w3c.github.io/IndexedDB/#fire-a-success-event +void fire_a_success_event(JS::Realm& realm, GC::Ref request) +{ + // 1. Let event be the result of creating an event using Event. + // 2. Set event’s type attribute to "success". + // 3. Set event’s bubbles and cancelable attributes to false. + auto event = DOM::Event::create(realm, HTML::EventNames::success, { .bubbles = false, .cancelable = false }); + + // 4. Let transaction be request’s transaction. + auto transaction = request->transaction(); + + // 5. Let legacyOutputDidListenersThrowFlag be initially false. + bool legacy_output_did_listeners_throw_flag = false; + + // 6. If transaction’s state is inactive, then set transaction’s 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 transaction’s state is active, then: + if (transaction->state() == IDBTransaction::TransactionState::Active) { + // 1. Set transaction’s 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. + if (legacy_output_did_listeners_throw_flag) { + abort_a_transaction(*transaction, WebIDL::AbortError::create(realm, "An error occurred"_string)); + return; + } + + // 3. If transaction’s request list is empty, then run commit a transaction with transaction. + if (transaction->request_list().is_empty()) + commit_a_transaction(realm, *transaction); + } +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index f95ec649497..bcb8de51984 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -34,5 +34,6 @@ WebIDL::ExceptionOr> evaluate_key_path_on_a_value(JS::Realm&, WebIDL::ExceptionOr>> extract_a_key_from_a_value_using_a_key_path(JS::Realm&, JS::Value, KeyPath const&, bool = false); bool check_that_a_key_could_be_injected_into_a_value(JS::Realm&, JS::Value, KeyPath const&); void fire_an_error_event(JS::Realm&, GC::Ref); +void fire_a_success_event(JS::Realm&, GC::Ref); }