Skip to content

Fix matchResponseMapWithRequests to handle prompt response.#505

Open
bglgwyng wants to merge 6 commits into
reflex-frp:developfrom
bglgwyng:fix-matchResponseMapWithRequests-with-prompt-response
Open

Fix matchResponseMapWithRequests to handle prompt response.#505
bglgwyng wants to merge 6 commits into
reflex-frp:developfrom
bglgwyng:fix-matchResponseMapWithRequests-with-prompt-response

Conversation

@bglgwyng

@bglgwyng bglgwyng commented Aug 8, 2024

Copy link
Copy Markdown
app :: forall t m. (MonadHeadlessApp t m) => m (Event t ())
app = mdo
  ePostBuild <- getPostBuild

  rec (_, eRequest) <-
        runRequesterT
          ( do
              eX <-
                requesting (ePostBuild $> Const ())
              performEvent_ $ ffor eX (const $ liftIO $ print "Got Response!")
          )
          eResponse'
      (eRequest', eResponse') <-
        matchResponseMapWithRequests
          (\x -> (mkSome x, \case Some x -> unsafeCoerce x))
          eRequest
          eResponse
      eResponse <- pure $ ffor eRequest' $ fmap (\(Some req) -> Some (Identity 1))
  pure never

This minimal example demonstrates a request/response interaction. When a request is made, the corresponding response is generated immediately, as seen in the eResponse definition. However, the current implementation of matchResponseMapWithRequests doesn't correctly handle prompt responses, resulting in "Got Response!" not being printed from eX. This fix ensures that prompt responses are handled correctly, allowing "Got Response!" to be printed as expected.

@ali-abrar ali-abrar requested a review from ryantrinkle October 22, 2024 21:10

@ryantrinkle ryantrinkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good patch, but I am slightly concerned about this being prompt. What if someone does something like this?

b <- requesting a
c <- requesting b

Will that create a cycle? It seems like it would. It would be great to add a test for this

Even if that does in fact create a cycle, we could still have something prompt here as an option

Comment thread src/Reflex/Requester/Base/Internal.hs Outdated
@bglgwyng

Copy link
Copy Markdown
Author

I tried to add test for new matchResponseMapWithRequests, but after I read https://github.com/reflex-frp/reflex/blob/develop/test/RequesterT.hs#L80, I got confused about why the current matchResponseMapWithRequests has passed this test. testMatchRequestsWithResponses responses promptly also, why does the current test pass the current matchResponseMapWithRequests?

@bglgwyng

Copy link
Copy Markdown
Author

I think we can refactor this code with ??? :: Event a -> Event b -> Event (a, Just b). Do we have something similar?

@bglgwyng

bglgwyng commented Dec 30, 2024

Copy link
Copy Markdown
Author

Looks like a good patch, but I am slightly concerned about this being prompt. What if someone does something like this?

b <- requesting a
c <- requesting b

Will that create a cycle? It seems like it would. It would be great to add a test for this

Even if that does in fact create a cycle, we could still have something prompt here as an option

The example you gave hangs, not even printing 'causality loop detected.

@ali-abrar

Copy link
Copy Markdown
Member

Closing this based on the conversation above. Please reopen if you're still interested in this.

@ali-abrar ali-abrar closed this Oct 20, 2025
@parenthetical parenthetical self-assigned this Jun 27, 2026
@parenthetical parenthetical reopened this Jun 27, 2026
@parenthetical

Copy link
Copy Markdown
Contributor

Reopening because I'd like to investigate this some more since I'm interested in increased laziness.

@bglgwyng

Copy link
Copy Markdown
Author

I used to need this behavior when I was trying to structure my entire Reflex project as a set of request-response relationships. That led me to implement something like this:

newtype Counter t = Counter (Behavior t Int)
data CounterReq a where
  Inc :: CounterReq ()
  Dec :: CounterReq ()

It’s been years since I last wrote Reflex code, so please ignore the details.

This required CounterReq to be handled synchronously, within the same time frame.
I’m not sure whether applying this request-response style across the entire project was a good idea.

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

Successfully merging this pull request may close these issues.

4 participants