1- /**
2- * Base class for storage managers that provides common functionality
3- * for tracking pending operations and dispatching events.
4- */
51class BaseStorageManager {
62 constructor ( ) {
73 this . dbName = "todoDB" ;
@@ -12,115 +8,58 @@ class BaseStorageManager {
128 this . pendingDeletions = 0 ;
139 }
1410
15- /**
16- * Initialize the database. This method should be implemented by subclasses.
17- * @returns {Promise } Promise that resolves when the database is initialized
18- */
1911 async initDB ( ) {
2012 throw new Error ( "initDB method must be implemented by subclass" ) ;
2113 }
2214
23- /**
24- * Check if the database connection is established
25- * @throws {Error } If database connection is not established
26- */
2715 _ensureDbConnection ( ) {
2816 if ( ! this . db )
2917 throw new Error ( "Database connection is not established" ) ;
3018 }
3119
32- /**
33- * Handle completion of add operations
34- * @protected
35- */
3620 _handleAddComplete ( ) {
3721 if ( -- this . pendingAdditions === 0 )
3822 window . dispatchEvent ( new CustomEvent ( "db-add-completed" , { } ) ) ;
3923 }
4024
41- /**
42- * Handle completion of toggle operations
43- * @protected
44- */
4525 _handleToggleComplete ( ) {
4626 if ( -- this . pendingToggles === 0 )
4727 window . dispatchEvent ( new CustomEvent ( "db-toggle-completed" , { } ) ) ;
4828 }
4929
50- /**
51- * Handle completion of remove operations
52- * @protected
53- */
5430 _handleRemoveComplete ( ) {
5531 if ( -- this . pendingDeletions === 0 )
5632 window . dispatchEvent ( new CustomEvent ( "db-remove-completed" , { } ) ) ;
5733 }
5834
59- /**
60- * Dispatch the db-ready event when initialization is complete
61- * @protected
62- */
6335 _dispatchReadyEvent ( ) {
6436 window . dispatchEvent ( new CustomEvent ( "db-ready" , { } ) ) ;
6537 }
6638
67- /**
68- * Increment the pending additions counter
69- * @protected
70- */
7139 _incrementPendingAdditions ( ) {
7240 this . pendingAdditions ++ ;
7341 }
7442
75- /**
76- * Increment the pending toggles counter
77- * @protected
78- */
7943 _incrementPendingToggles ( ) {
8044 this . pendingToggles ++ ;
8145 }
8246
83- /**
84- * Increment the pending deletions counter
85- * @protected
86- */
8747 _incrementPendingDeletions ( ) {
8848 this . pendingDeletions ++ ;
8949 }
9050
91- // Abstract methods that must be implemented by subclasses
92-
93- /**
94- * Add a todo item to the database
95- * @param {Object } todo - The todo item to add
96- */
9751 addTodo ( todo ) {
9852 throw new Error ( "addTodo method must be implemented by subclass" ) ;
9953 }
10054
101- /**
102- * Get todos from the database
103- * @param {number } upperItemNumber - Upper bound for item numbers (exclusive)
104- * @param {number } count - Maximum number of items to retrieve
105- * @returns {Promise<Array> } Promise that resolves to an array of todo items
106- */
10755 async getTodos ( upperItemNumber , count ) {
10856 throw new Error ( "getTodos method must be implemented by subclass" ) ;
10957 }
11058
111- /**
112- * Toggle the completed status of a todo item
113- * @param {number } itemNumber - The item number to toggle
114- * @param {boolean } completed - The new completed status
115- */
11659 toggleTodo ( itemNumber , completed ) {
11760 throw new Error ( "toggleTodo method must be implemented by subclass" ) ;
11861 }
11962
120- /**
121- * Remove a todo item from the database
122- * @param {number } itemNumber - The item number to remove
123- */
12463 removeTodo ( itemNumber ) {
12564 throw new Error ( "removeTodo method must be implemented by subclass" ) ;
12665 }
0 commit comments