LibWeb/IDB: Implement IDBObjectStore::open_cursor

This commit is contained in:
stelar7 2025-04-29 17:37:40 +02:00 committed by Jelle Raaijmakers
parent 98d08b27e4
commit 05fe37619a
Notes: github-actions[bot] 2025-05-06 09:18:12 +00:00
3 changed files with 44 additions and 1 deletions

View file

@ -9,6 +9,7 @@
#include <LibWeb/Bindings/IDBObjectStorePrototype.h> #include <LibWeb/Bindings/IDBObjectStorePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBCursor.h>
#include <LibWeb/IndexedDB/IDBIndex.h> #include <LibWeb/IndexedDB/IDBIndex.h>
#include <LibWeb/IndexedDB/IDBObjectStore.h> #include <LibWeb/IndexedDB/IDBObjectStore.h>
@ -395,4 +396,44 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::get(JS::Value query)
return result; return result;
} }
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-opencursor
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::open_cursor(JS::Value query, Bindings::IDBCursorDirection direction)
{
auto& realm = this->realm();
// 1. Let transaction be this's transaction.
auto transaction = this->transaction();
// 2. Let store be this's object store.
[[maybe_unused]] auto store = this->store();
// FIXME: 3. If store has been deleted, throw an "InvalidStateError" DOMException.
// 4. If transactions 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 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, false));
// 6. Let cursor be a new cursor with its transaction set to transaction, undefined position, direction set to direction,
// got value flag set to false, undefined key and value, source set to store, range set to range, and key only flag set to false.
auto cursor = IDBCursor::create(realm, transaction, {}, direction, false, {}, {}, GC::Ref(*this), range, false);
// 7. Let operation be an algorithm to run iterate a cursor with the current Realm record and cursor.
auto operation = GC::Function<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [&realm, cursor] -> WebIDL::ExceptionOr<JS::Value> {
return WebIDL::ExceptionOr<JS::Value>(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 cursor with uuid {}", request->uuid());
// 9. Set cursors request to request.
cursor->set_request(request);
// 10. Return request.
return request;
}
} }

View file

@ -8,6 +8,7 @@
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <LibGC/Heap.h> #include <LibGC/Heap.h>
#include <LibWeb/Bindings/IDBCursorPrototype.h>
#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/IndexedDB/IDBTransaction.h> #include <LibWeb/IndexedDB/IDBTransaction.h>
#include <LibWeb/IndexedDB/Internal/Index.h> #include <LibWeb/IndexedDB/Internal/Index.h>
@ -50,6 +51,7 @@ public:
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> put(JS::Value value, Optional<JS::Value> const& key); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> put(JS::Value value, Optional<JS::Value> const& key);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> count(Optional<JS::Value>); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> count(Optional<JS::Value>);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
protected: protected:
explicit IDBObjectStore(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>); explicit IDBObjectStore(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>);

View file

@ -19,7 +19,7 @@ interface IDBObjectStore {
[FIXME, NewObject] IDBRequest getAll(optional any query, optional [EnforceRange] unsigned long count); [FIXME, NewObject] IDBRequest getAll(optional any query, optional [EnforceRange] unsigned long count);
[FIXME, NewObject] IDBRequest getAllKeys(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); [NewObject] IDBRequest count(optional any query);
[FIXME, NewObject] IDBRequest openCursor(optional any query, optional IDBCursorDirection direction = "next"); [NewObject] IDBRequest openCursor(optional any query, optional IDBCursorDirection direction = "next");
[FIXME, NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next"); [FIXME, NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next");
IDBIndex index(DOMString name); IDBIndex index(DOMString name);
[NewObject] IDBIndex createIndex(DOMString name, (DOMString or sequence<DOMString>) keyPath, optional IDBIndexParameters options = {}); [NewObject] IDBIndex createIndex(DOMString name, (DOMString or sequence<DOMString>) keyPath, optional IDBIndexParameters options = {});