1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 09:34:57 +09:00

LibWeb/IDB: Implement IDBIndex::getAllKeys

This commit is contained in:
stelar7 2025-05-13 22:46:23 +02:00 committed by Jelle Raaijmakers
parent 3fa1d1299c
commit b8bb8345a9
Notes: github-actions[bot] 2025-05-14 15:19:19 +00:00
5 changed files with 62 additions and 1 deletions

View file

@ -234,4 +234,35 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_all(Optional<JS::Value> q
return result;
}
// https://w3c.github.io/IndexedDB/#dom-idbindex-getallkeys
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_all_keys(Optional<JS::Value> query, Optional<WebIDL::UnsignedLong> count)
{
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 all keys"_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 operation be an algorithm to run retrieve multiple values from an index with index, range, and count if given.
auto operation = GC::Function<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [&realm, index, range, count] -> WebIDL::ExceptionOr<JS::Value> {
return retrieve_multiple_values_from_an_index(realm, index, range, count);
});
// 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 all keys with uuid {}", result->uuid());
return result;
}
}

View file

@ -32,6 +32,7 @@ public:
[[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>> get_all(Optional<JS::Value>, Optional<WebIDL::UnsignedLong>);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_all_keys(Optional<JS::Value>, Optional<WebIDL::UnsignedLong>);
[[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.

View file

@ -10,7 +10,7 @@ interface IDBIndex {
[NewObject] IDBRequest get(any query);
[NewObject] IDBRequest getKey(any query);
[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 getAllKeys(optional any query, optional [EnforceRange] unsigned long count);
[FIXME, 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");

View file

@ -1991,4 +1991,32 @@ GC::Ref<JS::Array> retrieve_multiple_referenced_values_from_an_index(JS::Realm&
return list;
}
// https://w3c.github.io/IndexedDB/#retrieve-multiple-values-from-an-index
GC::Ref<JS::Array> retrieve_multiple_values_from_an_index(JS::Realm& realm, GC::Ref<Index> index, GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{
// 1. If count is not given or is 0 (zero), let count be infinity.
if (count.has_value() && *count == 0)
count = OptionalNone();
// 2. Let records be a list containing the first count records in indexs list of records whose key is in range.
auto records = index->first_n_in_range(range, count);
// 3. Let list be an empty list.
auto list = MUST(JS::Array::create(realm, records.size()));
// 4. For each record of records:
for (u32 i = 0; i < records.size(); ++i) {
auto& record = records[i];
// 1. Let entry be the result of converting a key to a value with records value.
auto entry = convert_a_key_to_a_value(realm, record.value);
// 2. Append entry to list.
MUST(list->create_data_property_or_throw(i, entry));
}
// 7. Return list converted to a sequence<any>.
return list;
}
}

View file

@ -54,5 +54,6 @@ GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm&, GC::R
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>);
GC::Ref<JS::Array> retrieve_multiple_referenced_values_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
GC::Ref<JS::Array> retrieve_multiple_values_from_an_index(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
}