diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp index 168f39218bb..92491b96245 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp @@ -506,4 +506,35 @@ WebIDL::ExceptionOr> IDBObjectStore::clear() return result; } +// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-getkey +WebIDL::ExceptionOr> IDBObjectStore::get_key(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 key"_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 key from an object store with store and range. + auto operation = GC::Function()>::create(realm.heap(), [&realm, store, range] -> WebIDL::ExceptionOr { + return retrieve_a_key_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 key with uuid {}", result->uuid()); + return result; +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h index 140984cc551..b7793f04dd9 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h @@ -54,6 +54,7 @@ public: [[nodiscard]] WebIDL::ExceptionOr> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); [[nodiscard]] WebIDL::ExceptionOr> delete_(JS::Value); [[nodiscard]] WebIDL::ExceptionOr> clear(); + [[nodiscard]] WebIDL::ExceptionOr> get_key(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 ce85a2772bf..daa26684672 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl @@ -15,7 +15,7 @@ interface IDBObjectStore { [NewObject] IDBRequest delete(any query); [NewObject] IDBRequest clear(); [NewObject] IDBRequest get(any query); - [FIXME, NewObject] IDBRequest getKey(any query); + [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); [NewObject] IDBRequest count(optional any query);