Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared data in Yesod

Tags:

haskell

yesod

I want to share some data across requests in Yesod. In my case that data is a MVar (Data.Map Text ReadWriteLock), but I don't think the format of the data being shared matters too much here.

In Foundation.hs, there is a comment that says I can add fields to App, and every handler will have access to the data there. This seems like an approach I could use to share data between different handlers. I have been looking through the Yesod book, but I could not find any examples of getting data from App.

  • How would I access the newly created field from within a handler?

I think this might be a good use case for STM. I could share a TVar (Data.Map Text ReadWriteLock). But creating a TVar wraps the TVar in the STM monad. I might be mistaken, but to me that seems like the entire Yesod "main loop" would need to be run in the STM monad.

  • Is using STM a viable option here? Could anyone elaborate on how this might be achieved?
like image 757
Kevin Avatar asked Dec 06 '25 06:12

Kevin


1 Answers

This tutorial for building a file server with Yesod shows quite nicely how you can use STM to access shared data. The relevant part starts from part 2.

like image 100
Sam Avatar answered Dec 08 '25 19:12

Sam