diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp index 1ab67d6e17e..bb590f0c0ba 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp @@ -331,4 +331,35 @@ WebIDL::ExceptionOr> IDBObjectStore::put(JS::Value value, Op return add_or_put(*this, value, key, false); } +// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-count +WebIDL::ExceptionOr> IDBObjectStore::count(Optional query) +{ + auto& realm = this->realm(); + + // 1. Let transaction be this's transaction. + auto transaction = this->transaction(); + + // 2. Let store be this's object store. + auto store = this->store(); + + // FIXME: 3. If 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 doing count"_string); + + // 5. Let range be the result of converting a value to a key range with query. Rethrow any exceptions. + auto range = TRY(convert_a_value_to_a_key_range(realm, move(query))); + + // 6. Let operation be an algorithm to run count the records in a range with store and range. + auto operation = GC::Function()>::create(realm.heap(), [store, range] -> WebIDL::ExceptionOr { + return count_the_records_in_a_range(store, 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 count with uuid {}", result->uuid()); + return result; +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h index f4b147ace70..3c236333aaa 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h @@ -47,6 +47,7 @@ public: [[nodiscard]] WebIDL::ExceptionOr> add_or_put(GC::Ref, JS::Value, Optional const&, bool); [[nodiscard]] WebIDL::ExceptionOr> add(JS::Value value, Optional const& key); [[nodiscard]] WebIDL::ExceptionOr> put(JS::Value value, Optional const& key); + [[nodiscard]] WebIDL::ExceptionOr> count(Optional); protected: explicit IDBObjectStore(JS::Realm&, GC::Ref, GC::Ref); diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl index 46a43c2ab29..4d3296626d0 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl @@ -18,7 +18,7 @@ interface IDBObjectStore { [FIXME, NewObject] IDBRequest getKey(any query); [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 count(optional any query); + [NewObject] IDBRequest count(optional any query); [FIXME, NewObject] IDBRequest openCursor(optional any query, optional IDBCursorDirection direction = "next"); [FIXME, NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next"); IDBIndex index(DOMString name);