LibWeb/IDB: Use helper method for checking transaction state

This commit is contained in:
stelar7 2025-05-13 23:14:02 +02:00 committed by Jelle Raaijmakers
parent 4c7c7845d3
commit c8e1b24864
Notes: github-actions[bot] 2025-05-14 15:18:37 +00:00
5 changed files with 31 additions and 28 deletions

View file

@ -116,7 +116,7 @@ WebIDL::ExceptionOr<void> IDBCursor::continue_(JS::Value key)
auto transaction = this->transaction();
// 2. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while continuing cursor"_string);
// FIXME: 3. If this's source or effective object store has been deleted, throw an "InvalidStateError" DOMException
@ -207,7 +207,7 @@ WebIDL::ExceptionOr<void> IDBCursor::advance(WebIDL::UnsignedLong count)
auto transaction = this->transaction();
// 3. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while advancing cursor"_string);
// FIXME: 4. If thiss source or effective object store has been deleted, throw an "InvalidStateError" DOMException.
@ -249,7 +249,7 @@ WebIDL::ExceptionOr<void> IDBCursor::continue_primary_key(JS::Value key_param, J
auto transaction = this->transaction();
// 2. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while continuing cursor"_string);
// FIXME: 3. If thiss source or effective object store has been deleted, throw an "InvalidStateError" DOMException.
@ -349,7 +349,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBCursor::update(JS::Value value)
auto transaction = this->transaction();
// 2. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while updating cursor"_string);
// 3. If transaction is a read-only transaction, throw a "ReadOnlyError" DOMException.
@ -415,7 +415,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBCursor::delete_()
auto transaction = this->transaction();
// 2. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while deleting cursor"_string);
// 3. If transaction is a read-only transaction, throw a "ReadOnlyError" DOMException.

View file

@ -108,7 +108,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBObjectStore>> IDBDatabase::create_object_store(St
return WebIDL::InvalidStateError::create(realm, "Upgrade transaction is null"_string);
// 3. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while creating object store"_string);
// 4. Let keyPath be optionss keyPath member if it is not undefined or null, or null otherwise.
@ -170,7 +170,7 @@ WebIDL::ExceptionOr<void> IDBDatabase::delete_object_store(String const& name)
return WebIDL::InvalidStateError::create(realm, "Upgrade transaction is null"_string);
// 3. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while deleting object store"_string);
// 4. Let store be the object store named name in database, or throw a "NotFoundError" DOMException if none.

View file

@ -63,7 +63,7 @@ WebIDL::ExceptionOr<void> IDBIndex::set_name(String const& value)
return WebIDL::InvalidStateError::create(realm, "Transaction is not an upgrade transaction"_string);
// 5. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while updating index name"_string);
// FIXME: 6. If index or indexs object store has been deleted, throw an "InvalidStateError" DOMException.
@ -117,7 +117,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::open_cursor(JS::Value query,
// 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while opening cursor"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -155,7 +155,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get(JS::Value query)
// 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)
if (!transaction->is_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.
@ -186,7 +186,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_key(JS::Value query)
// 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)
if (!transaction->is_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.
@ -217,7 +217,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_all(Optional<JS::Value> q
// 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while getting all"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -248,7 +248,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::get_all_keys(Optional<JS::Val
// 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)
if (!transaction->is_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.
@ -279,7 +279,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::count(JS::Value query)
// 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while counting"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -310,7 +310,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBIndex::open_key_cursor(JS::Value que
// 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while opening key cursor"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.

View file

@ -79,7 +79,7 @@ WebIDL::ExceptionOr<void> IDBObjectStore::set_name(String const& value)
return WebIDL::InvalidStateError::create(realm, "Attempted to set name outside of version change"_string);
// 6. If transactions state is not active, throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while updating object store name"_string);
// 7. If stores name is equal to name, terminate these steps.
@ -160,7 +160,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::create_index(String const
// FIXME: 4. If store has been deleted, throw an "InvalidStateError" DOMException.
// 5. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while creating index"_string);
// 6. If an index named name already exists in store, throw a "ConstraintError" DOMException.
@ -234,7 +234,7 @@ WebIDL::ExceptionOr<void> IDBObjectStore::delete_index(String const& name)
// FIXME: 4. If store has been deleted, throw an "InvalidStateError" DOMException.
// 5. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while deleting index"_string);
// 6. Let index be the index named name in store if one exists, or throw a "NotFoundError" DOMException otherwise.
@ -265,7 +265,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::add_or_put(GC::Ref<IDBO
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while running add/put"_string);
// 5. If transaction is a read-only transaction, throw a "ReadOnlyError" DOMException.
@ -373,7 +373,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::count(Optional<JS::Valu
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while doing count"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -404,7 +404,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::get(JS::Value query)
// FIXME: 3. If 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)
if (!transaction->is_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.
@ -435,7 +435,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::open_cursor(JS::Value q
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while opening cursor"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -475,7 +475,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::delete_(JS::Value query
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while deleting object store"_string);
// 5. If transaction is a read-only transaction, throw a "ReadOnlyError" DOMException.
@ -510,7 +510,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::clear()
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while clearing object store"_string);
// 5. If transaction is a read-only transaction, throw a "ReadOnlyError" DOMException.
@ -542,7 +542,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::get_key(JS::Value query
// FIXME: 3. If 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)
if (!transaction->is_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.
@ -573,7 +573,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::get_all(Optional<JS::Va
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while getting all"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -604,7 +604,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::open_key_cursor(JS::Val
// FIXME: 3. If 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)
if (!transaction->is_active())
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while opening key cursor"_string);
// 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions.
@ -643,7 +643,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::get_all_keys(Optional<J
// FIXME: 3. If 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)
if (!transaction->is_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.

View file

@ -59,6 +59,9 @@ public:
[[nodiscard]] bool is_readonly() const { return m_mode == Bindings::IDBTransactionMode::Readonly; }
[[nodiscard]] bool is_readwrite() const { return m_mode == Bindings::IDBTransactionMode::Readwrite; }
[[nodiscard]] bool is_finished() const { return m_state == TransactionState::Finished; }
[[nodiscard]] bool is_active() const { return m_state == TransactionState::Active; }
[[nodiscard]] bool is_inactive() const { return m_state == TransactionState::Inactive; }
[[nodiscard]] bool is_committing() const { return m_state == TransactionState::Committing; }
GC::Ptr<ObjectStore> object_store_named(String const& name) const;
void add_to_scope(GC::Ref<ObjectStore> object_store) { m_scope.append(object_store); }