Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check if a global store is initialized #6

Open
TristanCacqueray opened this issue May 17, 2024 · 0 comments
Open

How to check if a global store is initialized #6

TristanCacqueray opened this issue May 17, 2024 · 0 comments

Comments

@TristanCacqueray
Copy link

TristanCacqueray commented May 17, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant