LibWeb/IDB: Implement IDBObjectStore::get

This commit is contained in:
stelar7 2025-04-28 15:55:59 +02:00 committed by Jelle Raaijmakers
parent 18a008d073
commit c81c17c0fb
Notes: github-actions[bot] 2025-04-29 15:07:40 +00:00
3 changed files with 33 additions and 1 deletions

View file

@ -362,4 +362,35 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::count(Optional<JS::Valu
return result;
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-get
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> 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 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 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<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [&realm, store, range] -> WebIDL::ExceptionOr<JS::Value> {
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;
}
}

View file

@ -48,6 +48,7 @@ public:
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> add(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>> get(JS::Value);
protected:
explicit IDBObjectStore(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>);

View file

@ -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);