mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 19:59:17 +00:00
LibWeb/IDB: Implement IDBIndex::get
This commit is contained in:
parent
73fa567e7a
commit
e74e571b56
Notes:
github-actions[bot]
2025-05-14 15:19:35 +00:00
Author: https://github.com/stelar7
Commit: e74e571b56
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4727
Reviewed-by: https://github.com/gmta ✅
Reviewed-by: https://github.com/shannonbooth
7 changed files with 62 additions and 3 deletions
|
@ -141,4 +141,35 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::open_cursor(JS::Value query,
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://w3c.github.io/IndexedDB/#dom-idbindex-get
|
||||||
|
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get(JS::Value query)
|
||||||
|
{
|
||||||
|
auto& realm = this->realm();
|
||||||
|
|
||||||
|
// 1. Let transaction be this’s transaction.
|
||||||
|
auto transaction = this->transaction();
|
||||||
|
|
||||||
|
// 2. Let index be this’s index.
|
||||||
|
auto index = this->index();
|
||||||
|
|
||||||
|
// FIXME: 3. If index or index’s object store has been deleted, throw an "InvalidStateError" DOMException.
|
||||||
|
|
||||||
|
// 4. If transaction’s 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 referenced value from an index with the current Realm record, index, and range.
|
||||||
|
auto operation = GC::Function<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [&realm, index, range] -> WebIDL::ExceptionOr<JS::Value> {
|
||||||
|
return retrieve_a_referenced_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 with uuid {}", result->uuid());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,12 +29,13 @@ public:
|
||||||
bool multi_entry() const { return m_index->multi_entry(); }
|
bool multi_entry() const { return m_index->multi_entry(); }
|
||||||
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>> 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.
|
||||||
GC::Ref<IDBTransaction> transaction() { return m_object_store_handle->transaction(); }
|
GC::Ref<IDBTransaction> transaction() { return m_object_store_handle->transaction(); }
|
||||||
GC::Ref<Index> index() { return m_index; }
|
GC::Ref<Index> index() { return m_index; }
|
||||||
|
|
||||||
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit IDBIndex(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBObjectStore>);
|
explicit IDBIndex(JS::Realm&, GC::Ref<Index>, GC::Ref<IDBObjectStore>);
|
||||||
virtual void initialize(JS::Realm&) override;
|
virtual void initialize(JS::Realm&) override;
|
||||||
|
|
|
@ -7,7 +7,7 @@ interface IDBIndex {
|
||||||
readonly attribute any keyPath;
|
readonly attribute any keyPath;
|
||||||
readonly attribute boolean multiEntry;
|
readonly attribute boolean multiEntry;
|
||||||
readonly attribute boolean unique;
|
readonly attribute boolean unique;
|
||||||
[FIXME, NewObject] IDBRequest get(any query);
|
[NewObject] IDBRequest get(any query);
|
||||||
[FIXME, NewObject] IDBRequest getKey(any query);
|
[FIXME, 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);
|
||||||
|
|
|
@ -1929,4 +1929,21 @@ GC::Ref<JS::Array> retrieve_multiple_keys_from_an_object_store(JS::Realm& realm,
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://w3c.github.io/IndexedDB/#retrieve-a-referenced-value-from-an-index
|
||||||
|
JS::Value retrieve_a_referenced_value_from_an_index(JS::Realm& realm, GC::Ref<Index> index, GC::Ref<IDBKeyRange> range)
|
||||||
|
{
|
||||||
|
// 1. Let record be the first record in index’s 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. Let serialized be record’s referenced value.
|
||||||
|
auto serialized = index->referenced_value(*record);
|
||||||
|
|
||||||
|
// 4. Return ! StructuredDeserialize(serialized, targetRealm).
|
||||||
|
return MUST(HTML::structured_deserialize(realm.vm(), serialized, realm));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/Variant.h>
|
#include <AK/Variant.h>
|
||||||
#include <LibJS/Runtime/Realm.h>
|
#include <LibJS/Runtime/Realm.h>
|
||||||
#include <LibWeb/HTML/DOMStringList.h>
|
#include <LibWeb/HTML/DOMStringList.h>
|
||||||
|
#include <LibWeb/IndexedDB/IDBKeyRange.h>
|
||||||
#include <LibWeb/IndexedDB/IDBRequest.h>
|
#include <LibWeb/IndexedDB/IDBRequest.h>
|
||||||
#include <LibWeb/IndexedDB/Internal/Key.h>
|
#include <LibWeb/IndexedDB/Internal/Key.h>
|
||||||
#include <LibWeb/StorageAPI/StorageKey.h>
|
#include <LibWeb/StorageAPI/StorageKey.h>
|
||||||
|
@ -50,5 +51,6 @@ 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>);
|
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>);
|
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>);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,4 +76,11 @@ void Index::clear_records()
|
||||||
m_records.clear();
|
m_records.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<IndexRecord&> Index::first_in_range(GC::Ref<IDBKeyRange> range)
|
||||||
|
{
|
||||||
|
return m_records.first_matching([&](auto const& record) {
|
||||||
|
return range->is_in_range(record.key);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] bool has_record_with_key(GC::Ref<Key> key);
|
[[nodiscard]] bool has_record_with_key(GC::Ref<Key> key);
|
||||||
void clear_records();
|
void clear_records();
|
||||||
|
Optional<IndexRecord&> first_in_range(GC::Ref<IDBKeyRange> range);
|
||||||
|
|
||||||
HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const;
|
HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue