Skip to content

How to check if a global store is initialized #6

Description

@TristanCacqueray

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions