foreign-store icon indicating copy to clipboard operation
foreign-store copied to clipboard

How to check if a global store is initialized

Open TristanCacqueray opened this issue 9 months ago • 0 comments

Hello, I'm using foreign-store with ghcid to implement a hot reload feedback loop as suggested in https://discourse.haskell.org/t/live-reloading-gui-from-scratch/9569/2.

On first run, the window needs to be created, then on reload only the render function is updated. The setup presently looks like this with lookupStore (full demo here):

-- Store the render function behind a IORef for hot reload
renderFuncStore :: FS.Store (IORef (Window -> IO ()))
renderFuncStore = FS.Store 0

-- The ghcid entry point
mainGHCID :: IO ()
mainGHCID = FS.lookupStore renderFuncStoreID >>= \case
  -- This is a fresh reload, initialize the store and fork the main thread
  Nothing -> initWindow
  -- This is a hot reload, just update the stored render func IORef
  Just store -> updateStore
 where
  -- FS.lookupStore needs the newtyped Store value
  FS.Store renderFuncStoreID = renderFuncStore

The return value from lookupStore is not really useful since the store is global. I think this could be more ergonomic if foreign-store provided a isEmptyStore helper so that the entry point could be rewritten as follow:

isEmptyStore :: Store a -> IO Bool

mainGHCID = isEmptyStore renderFuncStore >>= \case
  True -> initWindow
  False -> updateStore

What do you think, may I propose a PR with this isEmptyStore helper? Thanks!

TristanCacqueray avatar May 17 '24 12:05 TristanCacqueray