add stGen which gets bumped for every worker thread spawned by loadBook every thread captures stGen when it spawns and before actually loading the book in the main thread, and in the worker thread it checks if myGen == stGen this throws away stale requests, meaning if you try and load another book while one is still loading, it will ignore the one still loading and start loading the latest request
23 lines
668 B
Haskell
23 lines
668 B
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
-- | The mutable application state's initial value. Navigation logic lives in
|
|
-- the UI, which renders chapters lazily into 'stCache'.
|
|
module State (initialState) where
|
|
|
|
import qualified Data.Map.Strict as M
|
|
import qualified Data.Text as T
|
|
import Types (AppState (..), ChapterIndex (..))
|
|
|
|
-- | Empty state shown while the book's structure loads in the background.
|
|
initialState :: T.Text -> FilePath -> AppState
|
|
initialState title path =
|
|
AppState
|
|
{ stRefs = [],
|
|
stToc = [],
|
|
stCache = M.empty,
|
|
stIndex = ChapterIndex 0,
|
|
stZoom = 1.0,
|
|
stTitle = title,
|
|
stBookPath = path,
|
|
stGen = 0
|
|
}
|