Skip to content

Commit ac33332

Browse files
committed
Move reset_stale_fetching_states to ListMetadataRepository
Move `reset_stale_fetching_states_internal` from `WpApiCache` to `ListMetadataRepository` where it belongs. The repository owns the state table and should provide all operations on it. `WpApiCache::perform_migrations` still calls this method after migrations complete, but now delegates to the repository.
1 parent 41f9ba5 commit ac33332

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

wp_mobile_cache/src/lib.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ impl WpApiCache {
277277
// Errors are logged but not propagated: this is a best-effort cleanup,
278278
// and failure doesn't affect core functionality (worst case: UI shows
279279
// stale loading state).
280-
if let Err(e) = Self::reset_stale_fetching_states_internal(connection) {
280+
if let Err(e) =
281+
repository::list_metadata::ListMetadataRepository::reset_stale_fetching_states(
282+
connection,
283+
)
284+
{
281285
log::warn!("Failed to reset stale fetching states: {}", e);
282286
}
283287

@@ -320,31 +324,6 @@ impl WpApiCache {
320324
}
321325

322326
impl WpApiCache {
323-
/// Resets stale fetching states (`FetchingFirstPage`, `FetchingNextPage`) to `Idle`.
324-
///
325-
/// If the app terminates while a fetch is in progress, these transient states persist
326-
/// in the database. On next launch, this could cause perpetual loading indicators or
327-
/// blocked fetches. We reset them during `WpApiCache` initialization since it's
328-
/// typically created once at app startup.
329-
///
330-
/// Note: `Error` states are intentionally preserved for UI feedback and debugging.
331-
fn reset_stale_fetching_states_internal(
332-
connection: &mut Connection,
333-
) -> Result<usize, SqliteDbError> {
334-
use crate::list_metadata::ListState;
335-
336-
connection
337-
.execute(
338-
"UPDATE list_metadata_state SET state = ?1 WHERE state IN (?2, ?3)",
339-
params![
340-
ListState::Idle as i32,
341-
ListState::FetchingFirstPage as i32,
342-
ListState::FetchingNextPage as i32,
343-
],
344-
)
345-
.map_err(SqliteDbError::from)
346-
}
347-
348327
/// Execute a database operation with scoped access to the connection.
349328
///
350329
/// This is the **only** way to access the database. The provided closure

wp_mobile_cache/src/repository/list_metadata.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,33 @@ impl ListMetadataRepository {
486486
})
487487
.map_err(SqliteDbError::from)
488488
}
489+
490+
/// Reset stale fetching states to Idle.
491+
///
492+
/// If the app terminates while a fetch is in progress, `FetchingFirstPage` and
493+
/// `FetchingNextPage` states persist in the database. On next launch, this could
494+
/// cause perpetual loading indicators or blocked fetches.
495+
///
496+
/// This resets those transient states to `Idle`. Error states are intentionally
497+
/// preserved for UI feedback and debugging.
498+
///
499+
/// Returns the number of rows updated.
500+
pub fn reset_stale_fetching_states(
501+
executor: &impl QueryExecutor,
502+
) -> Result<usize, SqliteDbError> {
503+
let sql = format!(
504+
"UPDATE {} SET state = ?1 WHERE state IN (?2, ?3)",
505+
Self::state_table().table_name()
506+
);
507+
executor.execute(
508+
&sql,
509+
rusqlite::params![
510+
ListState::Idle as i32,
511+
ListState::FetchingFirstPage as i32,
512+
ListState::FetchingNextPage as i32,
513+
],
514+
)
515+
}
489516
}
490517

491518
/// Header info returned from `get_or_create_and_increment_version`.

0 commit comments

Comments
 (0)