LibWeb/IDB: Implement IDBObjectStore::clear

This commit is contained in:
stelar7 2025-05-08 10:04:53 +02:00 committed by Sam Atkins
parent 637f35c0eb
commit fa207c8fc6
Notes: github-actions[bot] 2025-05-08 13:14:20 +00:00
3 changed files with 34 additions and 1 deletions

View file

@ -474,4 +474,36 @@ WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::delete_(JS::Value query
return result; return result;
} }
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-clear
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> IDBObjectStore::clear()
{
auto& realm = this->realm();
// 1. Let transaction be thiss transaction.
auto transaction = this->transaction();
// 2. Let store be thiss object store.
auto store = this->store();
// 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)
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.
if (transaction->is_readonly())
return WebIDL::ReadOnlyError::create(realm, "Transaction is read-only while clearing object store"_string);
// 6. Let operation be an algorithm to run clear an object store with store.
auto operation = GC::Function<WebIDL::ExceptionOr<JS::Value>()>::create(realm.heap(), [store] -> WebIDL::ExceptionOr<JS::Value> {
return clear_an_object_store(store);
});
// 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 clear with uuid {}", result->uuid());
return result;
}
} }

View file

@ -53,6 +53,7 @@ public:
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> delete_(JS::Value); [[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> delete_(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> clear();
protected: protected:
explicit IDBObjectStore(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>); explicit IDBObjectStore(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>);

View file

@ -13,7 +13,7 @@ interface IDBObjectStore {
[NewObject] IDBRequest put(any value, optional any key); [NewObject] IDBRequest put(any value, optional any key);
[NewObject] IDBRequest add(any value, optional any key); [NewObject] IDBRequest add(any value, optional any key);
[NewObject] IDBRequest delete(any query); [NewObject] IDBRequest delete(any query);
[FIXME, NewObject] IDBRequest clear(); [NewObject] IDBRequest clear();
[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);