2024-11-07 18:49:31 +01:00
/*
2025-04-11 11:21:05 +02:00
* Copyright ( c ) 2024 - 2025 , stelar7 < dudedbz @ gmail . com >
2024-11-07 18:49:31 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
# include <LibGC/Heap.h>
2025-04-11 11:21:05 +02:00
# include <LibWeb/Bindings/IDBCursorPrototype.h>
2025-05-13 11:04:55 +02:00
# include <LibWeb/Bindings/IDBCursorWithValuePrototype.h>
2024-11-07 18:49:31 +01:00
# include <LibWeb/Bindings/PlatformObject.h>
2025-04-11 11:21:05 +02:00
# include <LibWeb/IndexedDB/IDBIndex.h>
# include <LibWeb/IndexedDB/IDBKeyRange.h>
# include <LibWeb/IndexedDB/IDBObjectStore.h>
# include <LibWeb/IndexedDB/IDBTransaction.h>
# include <LibWeb/IndexedDB/Internal/Key.h>
2024-11-07 18:49:31 +01:00
namespace Web : : IndexedDB {
2025-05-06 11:36:11 +02:00
using CursorSourceHandle = Variant < GC : : Ref < IDBObjectStore > , GC : : Ref < IDBIndex > > ;
using CursorSource = Variant < GC : : Ref < ObjectStore > , GC : : Ref < Index > > ;
2025-04-11 11:21:05 +02:00
2024-11-07 18:49:31 +01:00
// https://w3c.github.io/IndexedDB/#cursor-interface
class IDBCursor : public Bindings : : PlatformObject {
WEB_PLATFORM_OBJECT ( IDBCursor , Bindings : : PlatformObject ) ;
GC_DECLARE_ALLOCATOR ( IDBCursor ) ;
2025-05-13 09:09:34 +02:00
enum class GotValue {
No ,
Yes ,
} ;
enum class KeyOnly {
No ,
Yes ,
} ;
2024-11-07 18:49:31 +01:00
public :
virtual ~ IDBCursor ( ) override ;
2025-05-13 09:09:34 +02:00
[ [ nodiscard ] ] static GC : : Ref < IDBCursor > create ( JS : : Realm & , CursorSourceHandle , GC : : Ptr < Key > , Bindings : : IDBCursorDirection , GotValue , GC : : Ptr < Key > , JS : : Value , GC : : Ref < IDBKeyRange > , KeyOnly ) ;
2025-04-11 11:21:05 +02:00
2025-05-06 11:36:11 +02:00
[ [ nodiscard ] ] CursorSourceHandle source_handle ( ) { return m_source_handle ; }
2025-04-11 11:21:05 +02:00
[ [ nodiscard ] ] Bindings : : IDBCursorDirection direction ( ) { return m_direction ; }
[ [ nodiscard ] ] JS : : Value key ( ) ;
2025-05-13 09:32:11 +02:00
[ [ nodiscard ] ] JS : : Value primary_key ( ) const ;
2025-04-11 11:21:05 +02:00
[ [ nodiscard ] ] GC : : Ptr < IDBRequest > request ( ) { return m_request ; }
2025-05-13 09:32:11 +02:00
2025-05-13 09:54:00 +02:00
WebIDL : : ExceptionOr < void > advance ( WebIDL : : UnsignedLong ) ;
2025-05-13 09:32:11 +02:00
WebIDL : : ExceptionOr < void > continue_ ( JS : : Value ) ;
2025-05-13 09:54:13 +02:00
WebIDL : : ExceptionOr < void > continue_primary_key ( JS : : Value , JS : : Value ) ;
2025-05-13 09:32:11 +02:00
2025-05-13 10:09:48 +02:00
WebIDL : : ExceptionOr < GC : : Ref < IDBRequest > > update ( JS : : Value ) ;
2025-05-13 10:23:52 +02:00
WebIDL : : ExceptionOr < GC : : Ref < IDBRequest > > delete_ ( ) ;
2025-05-13 10:09:48 +02:00
2025-04-11 11:21:05 +02:00
[ [ nodiscard ] ] GC : : Ref < IDBKeyRange > range ( ) { return m_range ; }
[ [ nodiscard ] ] GC : : Ptr < Key > position ( ) { return m_position ; }
[ [ nodiscard ] ] GC : : Ptr < Key > object_store_position ( ) { return m_object_store_position ; }
[ [ nodiscard ] ] bool key_only ( ) const { return m_key_only ; }
[ [ nodiscard ] ] bool got_value ( ) const { return m_got_value ; }
2025-05-06 11:36:11 +02:00
[ [ nodiscard ] ] GC : : Ref < IDBTransaction > transaction ( ) ;
[ [ nodiscard ] ] CursorSource internal_source ( ) ;
2025-05-08 09:42:39 +02:00
[ [ nodiscard ] ] GC : : Ref < Key > effective_key ( ) const ;
2025-05-13 10:09:48 +02:00
[ [ nodiscard ] ] GC : : Ref < ObjectStore > effective_object_store ( ) const ;
2025-05-06 11:36:11 +02:00
2025-04-29 17:37:16 +02:00
void set_request ( GC : : Ptr < IDBRequest > request ) { m_request = request ; }
void set_position ( GC : : Ptr < Key > position ) { m_position = position ; }
void set_got_value ( bool got_value ) { m_got_value = got_value ; }
void set_key ( GC : : Ptr < Key > key ) { m_key = key ; }
void set_value ( JS : : Value value ) { m_value = value ; }
void set_object_store_position ( GC : : Ptr < Key > object_store_position ) { m_object_store_position = object_store_position ; }
2024-11-07 18:49:31 +01:00
protected :
2025-05-13 09:09:34 +02:00
explicit IDBCursor ( JS : : Realm & , CursorSourceHandle , GC : : Ptr < Key > , Bindings : : IDBCursorDirection , GotValue , GC : : Ptr < Key > , JS : : Value , GC : : Ref < IDBKeyRange > , KeyOnly ) ;
2024-11-07 18:49:31 +01:00
virtual void initialize ( JS : : Realm & ) override ;
2025-04-11 11:21:05 +02:00
virtual void visit_edges ( Visitor & visitor ) override ;
2025-05-13 11:04:55 +02:00
// A cursor has a value which represent the value of the last iterated record.
Optional < JS : : Value > m_value ;
2025-04-11 11:21:05 +02:00
private :
// A cursor has a position within its range.
GC : : Ptr < Key > m_position ;
// When iterating indexes the cursor also has an object store position
GC : : Ptr < Key > m_object_store_position ;
// A cursor has a direction that determines whether it moves in monotonically increasing or decreasing order of the record keys when iterated, and if it skips duplicated values when iterating indexes.
Bindings : : IDBCursorDirection m_direction ;
// A cursor has a got value flag.
bool m_got_value { false } ;
2025-05-13 11:04:55 +02:00
// A cursor has a key which represent the key the last iterated record.
2025-04-11 11:21:05 +02:00
GC : : Ptr < Key > m_key ;
2025-05-06 11:36:11 +02:00
// A cursor has a source handle, which is the index handle or the object store handle that opened the cursor.
CursorSourceHandle m_source_handle ;
2025-04-11 11:21:05 +02:00
// A cursor has a range of records in either an index or an object store.
GC : : Ref < IDBKeyRange > m_range ;
// A cursor has a request, which is the request used to open the cursor.
GC : : Ptr < IDBRequest > m_request ;
// A cursor also has a key only flag, that indicates whether the cursor’ s value is exposed via the API.
bool m_key_only { false } ;
2024-11-07 18:49:31 +01:00
} ;
2025-05-13 11:04:55 +02:00
2024-11-07 18:49:31 +01:00
}