Skip to content

Async calls in initializer in the latest version #21

Description

@clayrat

Is it possible now to do an async call to initialize a component? I have something like this, where I want an image loaded on startup and a button to reload it:

imageLoader = withAsync $ with \st h -> let
  setUrl u = updatePure h $ case u of
    Just a  -> st # imgUrl .~ Success a
    Nothing -> st # imgUrl .~ Failed unit
  reload = update h $ do
    async (JS.decode <<< _.response <$> (AJ.get someUrl)) (const Nothing)
    pure st
  in withView (H.div $ [ H.onInitialized "foo" $ const reload ]) $ mconcat
  [ imgUrl $ mconcat
    [ _Failed $ ui $ H.p_ $ text "error"
    , _Success $ with \s _ -> ui $ H.div_ $ H.img [ H.srcA $ extractUrl s ] mempty
    ]
  , ui $ H.button [ H.onClick $ const reload ] $ text "reload"
  , listen (const true) setUrl
  ]

But it doesn't seem to fire. Maybe it's possible to hook it up via the _Init prism somehow?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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