LibWeb/IDB: Implement IDBIndex::getKey

This commit is contained in:
stelar7 2025-05-13 22:21:57 +02:00 committed by Jelle Raaijmakers
commit 47450bc15c
Notes: github-actions[bot] 2025-05-14 15:19:29 +00:00
5 changed files with 48 additions and 1 deletions

View file

@ -172,4 +172,35 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get(JS::Value query)
return result; return result;
} }
// https://w3c.github.io/IndexedDB/#dom-idbindex-getkey
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_key(JS::Value query)
{
auto& realm = this->realm();
// 1. Let transaction be thiss transaction.
auto transaction = this->transaction();
// 2. Let index be thiss index.
auto index = this->index();
// FIXME: 3. If index or indexs object 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 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 value from an index with index and range.
auto operation = GC::Function<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [&realm, index, range] -> WebIDL::ExceptionOr<JS::Value> {
return retrieve_a_value_from_an_index(realm, index, 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;
}
} }

View file

@ -30,6 +30,7 @@ public:
bool unique() const { return m_index->unique(); } bool unique() const { return m_index->unique(); }
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_key(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
// The transaction of an index handle is the transaction of its associated object store handle. // The transaction of an index handle is the transaction of its associated object store handle.

View file

@ -8,7 +8,7 @@ interface IDBIndex {
readonly attribute boolean multiEntry; readonly attribute boolean multiEntry;
readonly attribute boolean unique; readonly attribute boolean unique;
[NewObject] IDBRequest get(any query); [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 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);
[FIXME, NewObject] IDBRequest count(optional any query); [FIXME, NewObject] IDBRequest count(optional any query);

View file

@ -1946,4 +1946,18 @@ JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm& realm, GC::Ref<In
return MUST(HTML::structured_deserialize(realm.vm(), serialized, realm)); return MUST(HTML::structured_deserialize(realm.vm(), serialized, realm));
} }
// https://w3c.github.io/IndexedDB/#retrieve-a-value-from-an-index
JS::Value retrieve_a_value_from_an_index(JS::Realm& realm, GC::Ref<Index> index, GC::Ref<IDBKeyRange> range)
{
// 1. Let record be the first record in indexs list of records whose key is in range, if any.
auto record = index->first_in_range(range);
// 2. If record was not found, return undefined.
if (!record.has_value())
return JS::js_undefined();
// 3. Return the result of converting a key to a value with records value.
return convert_a_key_to_a_value(realm, record->value);
}
} }

View file

@ -52,5 +52,6 @@ JS::Value retrieve_a_key_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>,
GC::Ref<JS::Array> retrieve_multiple_values_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>); GC::Ref<JS::Array> retrieve_multiple_values_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>); GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>); JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>);
JS::Value retrieve_a_value_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>);
} }