LibWeb/IDB: Implement IDBTransaction::objectStore

This commit is contained in:
stelar7 2025-04-09 23:24:44 +02:00
parent 6c90b5da61
commit 714093e266
3 changed files with 33 additions and 1 deletions

View file

@ -7,6 +7,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBObjectStore.h>
#include <LibWeb/IndexedDB/IDBTransaction.h>
#include <LibWeb/IndexedDB/Internal/Algorithms.h>
@ -118,4 +119,32 @@ WebIDL::ExceptionOr<void> IDBTransaction::commit()
return {};
}
GC::Ptr<ObjectStore> IDBTransaction::object_store_named(String const& name) const
{
for (auto const& store : m_scope) {
if (store->name() == name)
return store;
}
return nullptr;
}
// https://w3c.github.io/IndexedDB/#dom-idbtransaction-objectstore
WebIDL::ExceptionOr<GC::Ref<IDBObjectStore>> IDBTransaction::object_store(String const& name)
{
auto& realm = this->realm();
// 1. If this's state is finished, then throw an "InvalidStateError" DOMException.
if (m_state == TransactionState::Finished)
return WebIDL::InvalidStateError::create(realm, "Transaction is finished"_string);
// 2. Let store be the object store named name in this's scope, or throw a "NotFoundError" DOMException if none.
auto store = object_store_named(name);
if (!store)
return WebIDL::NotFoundError::create(realm, "Object store not found"_string);
// 3. Return an object store handle associated with store and this.
return IDBObjectStore::create(realm, *store, *this);
}
}

View file

@ -59,8 +59,11 @@ public:
[[nodiscard]] bool is_readwrite() const { return m_mode == Bindings::IDBTransactionMode::Readwrite; }
[[nodiscard]] bool is_finished() const { return m_state == TransactionState::Finished; }
GC::Ptr<ObjectStore> object_store_named(String const& name) const;
WebIDL::ExceptionOr<void> abort();
WebIDL::ExceptionOr<void> commit();
WebIDL::ExceptionOr<GC::Ref<IDBObjectStore>> object_store(String const& name);
void set_onabort(WebIDL::CallbackType*);
WebIDL::CallbackType* onabort();

View file

@ -9,7 +9,7 @@ interface IDBTransaction : EventTarget {
readonly attribute IDBTransactionDurability durability;
[SameObject, ImplementedAs=connection] readonly attribute IDBDatabase db;
readonly attribute DOMException? error;
[FIXME] IDBObjectStore objectStore(DOMString name);
IDBObjectStore objectStore(DOMString name);
undefined commit();
undefined abort();