From c81c17c0fba0ba49c4ceff4a3b2a11ebdd39e13f Mon Sep 17 00:00:00 2001 From: stelar7 Date: Mon, 28 Apr 2025 15:55:59 +0200 Subject: [PATCH] LibWeb/IDB: Implement IDBObjectStore::get --- Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp | 31 +++++++++++++++++++ Libraries/LibWeb/IndexedDB/IDBObjectStore.h | 1 + Libraries/LibWeb/IndexedDB/IDBObjectStore.idl | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp index bb590f0c0ba..3564c3c213d 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp @@ -362,4 +362,35 @@ WebIDL::ExceptionOr> IDBObjectStore::count(Optional> IDBObjectStore::get(JS::Value query) +{ + auto& realm = this->realm(); + + // 1. Let transaction be this's transaction. + auto transaction = this->transaction(); + + // 2. Let store be this's object store. + auto store = this->store(); + + // FIXME: 3. If store has been deleted, throw an "InvalidStateError" DOMException. + + // 4. If transaction’s state is not active, then throw a "TransactionInactiveError" DOMException. + if (transaction->state() != IDBTransaction::TransactionState::Active) + return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while getting"_string); + + // 5. Let range be the result of converting a value to a key range with query and true. Rethrow any exceptions. + auto range = TRY(convert_a_value_to_a_key_range(realm, query, true)); + + // 6. Let operation be an algorithm to run retrieve a value from an object store with the current Realm record, store, and range. + auto operation = GC::Function()>::create(realm.heap(), [&realm, store, range] -> WebIDL::ExceptionOr { + return retrieve_a_value_from_an_object_store(realm, store, range); + }); + + // 7. Return the result (an IDBRequest) of running asynchronously execute a request with this and operation. + auto result = asynchronously_execute_a_request(realm, GC::Ref(*this), operation); + dbgln_if(IDB_DEBUG, "Executing request for get with uuid {}", result->uuid()); + return result; +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h index 3c236333aaa..919c3edde78 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h @@ -48,6 +48,7 @@ public: [[nodiscard]] WebIDL::ExceptionOr> add(JS::Value value, Optional const& key); [[nodiscard]] WebIDL::ExceptionOr> put(JS::Value value, Optional const& key); [[nodiscard]] WebIDL::ExceptionOr> count(Optional); + [[nodiscard]] WebIDL::ExceptionOr> get(JS::Value); protected: explicit IDBObjectStore(JS::Realm&, GC::Ref, GC::Ref); diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl index 4d3296626d0..1261331c328 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl @@ -14,7 +14,7 @@ interface IDBObjectStore { [NewObject] IDBRequest add(any value, optional any key); [FIXME, NewObject] IDBRequest delete(any query); [FIXME, NewObject] IDBRequest clear(); - [FIXME, NewObject] IDBRequest get(any query); + [NewObject] IDBRequest get(any query); [FIXME, NewObject] IDBRequest getKey(any query); [FIXME, NewObject] IDBRequest getAll(optional any query, optional [EnforceRange] unsigned long count); [FIXME, NewObject] IDBRequest getAllKeys(optional any query, optional [EnforceRange] unsigned long count);