LibWeb/IDB: Implement retrieve_multiple_values_from_an_object_store

This commit is contained in:
stelar7 2025-05-08 23:36:18 +02:00 committed by Shannon Booth
commit ddaae635ac
Notes: github-actions[bot] 2025-05-12 20:29:36 +00:00
4 changed files with 49 additions and 0 deletions

View file

@ -5,6 +5,7 @@
*/
#include <AK/Math.h>
#include <AK/NumericLimits.h>
#include <AK/QuickSort.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
@ -1869,4 +1870,35 @@ JS::Value retrieve_a_key_from_an_object_store(JS::Realm& realm, GC::Ref<ObjectSt
return convert_a_key_to_a_value(realm, record.value().key);
}
// https://w3c.github.io/IndexedDB/#retrieve-multiple-values-from-an-object-store
GC::Ref<JS::Array> retrieve_multiple_values_from_an_object_store(JS::Realm& realm, GC::Ref<ObjectStore> store, 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 stores list of records whose key is in range.
auto records = store->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 serialized be records value. If an error occurs while reading the value from the underlying storage, return a newly created "NotReadableError" DOMException.
auto serialized = record.value;
// 2. Let entry be ! StructuredDeserialize(serialized, targetRealm).
auto entry = MUST(HTML::structured_deserialize(realm.vm(), serialized, realm));
// 3. Append entry to list.
MUST(list->create_data_property_or_throw(i, entry));
}
// 5. Return list converted to a sequence<any>.
return list;
}
}

View file

@ -12,6 +12,7 @@
#include <LibWeb/IndexedDB/IDBRequest.h>
#include <LibWeb/IndexedDB/Internal/Key.h>
#include <LibWeb/StorageAPI/StorageKey.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::IndexedDB {
@ -47,5 +48,6 @@ WebIDL::ExceptionOr<JS::Value> retrieve_a_value_from_an_object_store(JS::Realm&,
GC::Ptr<IDBCursor> iterate_a_cursor(JS::Realm&, GC::Ref<IDBCursor>, GC::Ptr<Key> = nullptr, GC::Ptr<Key> = nullptr, u64 = 1);
JS::Value clear_an_object_store(GC::Ref<ObjectStore>);
JS::Value retrieve_a_key_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>);
GC::Ref<JS::Array> retrieve_multiple_values_from_an_object_store(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBKeyRange>, Optional<WebIDL::UnsignedLong>);
}

View file

@ -89,4 +89,18 @@ void ObjectStore::clear_records()
m_records.clear();
}
GC::ConservativeVector<Record> ObjectStore::first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count)
{
GC::ConservativeVector<Record> records(range->heap());
for (auto const& record : m_records) {
if (range->is_in_range(record.key))
records.append(record);
if (count.has_value() && records.size() >= *count)
break;
}
return records;
}
}

View file

@ -56,6 +56,7 @@ public:
u64 count_records_in_range(GC::Ref<IDBKeyRange> range);
Optional<Record&> first_in_range(GC::Ref<IDBKeyRange> range);
void clear_records();
GC::ConservativeVector<Record> first_n_in_range(GC::Ref<IDBKeyRange> range, Optional<WebIDL::UnsignedLong> count);
protected:
virtual void visit_edges(Visitor&) override;