# All files / src/store/middleware persistenceMiddleware.ts

68.35% Statements 54/79
66.66% Branches 8/12
100% Functions 2/2
68.35% Lines 54/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    811x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    106x
    106x
    106x

    106x
    106x
    106x
    106x

    106x
    106x
    106x
    106x
    3x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    3x
    106x
    106x
    106x

     import { Middleware } from '@reduxjs/toolkit'
    import { saveDB } from '@/lib/db'
    import { getPersistenceConfig } from './persistenceConfig'

    let persistenceQueue: Promise<void> = Promise.resolve()
    let pendingSync = false
    let retryCount = 0

    async function saveWithRetry(): Promise<void> {
      const config = getPersistenceConfig()

      try {
        await saveDB()

        if (config.logging) {
          console.log('[Redux Persistence] State synced to database')
        }

        retryCount = 0
      } catch (error) {
        if (config.logging) {
          console.error('[Redux Persistence] Error syncing to database:', error)
        }

        if (config.retryOnFailure && retryCount < config.maxRetries) {
          retryCount++

          if (config.logging) {
            console.warn(`[Redux Persistence] Retrying save (${retryCount}/${config.maxRetries})...`)
          }

          await new Promise(resolve => setTimeout(resolve, config.retryDelayMs))
          await saveWithRetry()
        } else {
          retryCount = 0
          throw error
        }
      }
    }

    export const persistenceMiddleware: Middleware = () => (next) => (action: unknown) => {
      const result = next(action)

      if (typeof action !== 'object' || !action || !('type' in action)) {
        return result
      }

      const config = getPersistenceConfig()

      if (!config.enabled) {
        return result
      }

      const actionType = (action as { type: string }).type

      if (config.actions.includes(actionType)) {
        if (!pendingSync) {
          pendingSync = true

          persistenceQueue = persistenceQueue.then(async () => {
            try {
              if (config.debounceMs > 0) {
                await new Promise(resolve => setTimeout(resolve, config.debounceMs))
              }

              await saveWithRetry()
            } catch (error) {
              if (config.logging) {
                console.error('[Redux Persistence] Failed to save after retries:', error)
              }
            } finally {
              pendingSync = false
            }
          })
        }
      }

      return result
    }