diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.cpp b/Libraries/LibWeb/IndexedDB/IDBIndex.cpp index 647c38bb9b7..4fa89c4916e 100644 --- a/Libraries/LibWeb/IndexedDB/IDBIndex.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBIndex.cpp @@ -296,4 +296,43 @@ WebIDL::ExceptionOr> IDBIndex::count(JS::Value query) return result; } +// https://w3c.github.io/IndexedDB/#dom-idbindex-openkeycursor +WebIDL::ExceptionOr> IDBIndex::open_key_cursor(JS::Value query, Bindings::IDBCursorDirection direction) +{ + auto& realm = this->realm(); + + // 1. Let transaction be this’s transaction. + auto transaction = this->transaction(); + + // 2. Let index be this’s index. + [[maybe_unused]] auto index = this->index(); + + // FIXME: 3. If index or index’s object 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 opening key cursor"_string); + + // 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions. + auto range = TRY(convert_a_value_to_a_key_range(realm, query)); + + // 6. Let cursor be a new cursor with its source handle set to this, undefined position, direction set to direction, got value flag set to false, undefined key and value, range set to range, and key only flag set to true. + auto cursor = IDBCursor::create(realm, GC::Ref(*this), {}, direction, IDBCursor::GotValue::No, {}, {}, range, IDBCursor::KeyOnly::Yes); + + // 7. Let operation be an algorithm to run iterate a cursor with the current Realm record and cursor. + auto operation = GC::Function()>::create(realm.heap(), [&realm, cursor] -> WebIDL::ExceptionOr { + return WebIDL::ExceptionOr(iterate_a_cursor(realm, cursor)); + }); + + // 8. Let request be the result of running asynchronously execute a request with this and operation. + auto request = asynchronously_execute_a_request(realm, GC::Ref(*this), operation); + dbgln_if(IDB_DEBUG, "Executing request for open key cursor with uuid {}", request->uuid()); + + // 9. Set cursor’s request to request. + cursor->set_request(request); + + // 10. Return request. + return request; +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.h b/Libraries/LibWeb/IndexedDB/IDBIndex.h index 0724b301458..a063bdd35b5 100644 --- a/Libraries/LibWeb/IndexedDB/IDBIndex.h +++ b/Libraries/LibWeb/IndexedDB/IDBIndex.h @@ -35,6 +35,7 @@ public: [[nodiscard]] WebIDL::ExceptionOr> get_all_keys(Optional, Optional); [[nodiscard]] WebIDL::ExceptionOr> count(JS::Value); [[nodiscard]] WebIDL::ExceptionOr> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); + [[nodiscard]] WebIDL::ExceptionOr> open_key_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); // The transaction of an index handle is the transaction of its associated object store handle. GC::Ref transaction() { return m_object_store_handle->transaction(); } diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.idl b/Libraries/LibWeb/IndexedDB/IDBIndex.idl index c0b880727b8..ea6f0321b15 100644 --- a/Libraries/LibWeb/IndexedDB/IDBIndex.idl +++ b/Libraries/LibWeb/IndexedDB/IDBIndex.idl @@ -13,5 +13,5 @@ interface IDBIndex { [NewObject] IDBRequest getAllKeys(optional any query, optional [EnforceRange] unsigned long count); [NewObject] IDBRequest count(optional any query); [NewObject] IDBRequest openCursor(optional any query, optional IDBCursorDirection direction = "next"); - [FIXME, NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next"); + [NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next"); };