Comments (6)
from purescript-httpure.
I thought about that @JordanMartinez. But it seemed like such a common use case, I wondered if I was missing a more "pure" way to do it.
from purescript-httpure.
Since the state monad is just a boilerplate-free way to thread immutable pure state through a computation...
this :: MonadState Int m => m String
this = do
original <- get
put (original + 1)
pure "done"
sameThing :: Monad m => Int -> m (Tuple Int String)
sameThing initialValue = do
let original = initialValue
let new = original + 1
pure (Tuple new "done")
So, if you wrote a handler that uses the state monad, the problem is with the initial state value. Each time a request is made, the same initial state would be used in the resulting state monad computation. Using a counter as an example, the initial count would always start at 0 and be 1 at the end of the response. The next time a request comes in, it's back to 0, not 1.
If you wanted to track state across requests, you need to use mutable state. Your options are either ST (local) or Ref (global). Since ST doesn't work in this situation, you're only real option is Ref.
from purescript-httpure.
Currently, I don't think there would be any way to use something like a StateT
monad to thread mutable server state into your router. But as @JordanMartinez mentions, you can certainly use a Ref.
I'm not opposed to thinking about ways to make one of those other monad transformerss usable for this kind of situation (which would probably match the solution for #134). That said, I typically structure my apps following something very close to https://www.fpcomplete.com/blog/2017/06/readert-design-pattern. That article makes very compelling arguments why Ref is actually more appropriate for global app state. Presumably your app is your server, so server state is a synonym for global app state.
from purescript-httpure.
@cprussin would you say the remarks about exceptions apply to PureScript as well?
from purescript-httpure.
wow this is a very late reply but somehow I missed your message @easafe . Yes I think the same logic for exceptions applies to purescript since the Effect
monad in purescript has the same exception contract as IO
(that is, anything running in it has an implicit assumption that it could throw an exception).
from purescript-httpure.
Related Issues (20)
- How to handle `Expect: 100-continue`? HOT 2
- Upgrade dependencies
- Expose request path and query to enable custom route parsing? HOT 7
- Should functions on `Response` return `MonadAff m => m Response` HOT 1
- default `serve` binds to `localhost` (resulting in "connection refused" errors) HOT 6
- Allow multiple SetCookie headers in response HOT 22
- Make `Status` a bit more safe and signature friendly HOT 1
- Change `Method` representation to something more like `Status` HOT 1
- Pursuit is not up to date with 0.10.0 HOT 2
- Utils.replacePlus Only Replaces the First Occurrence HOT 1
- Body write doesn't wait for async finish? HOT 4
- Add support for multiple request body formats
- Simplify & update project setup
- No 'Body' instance for 'Duplex' streams
- Calling `HTTPure.Body.toString` multiple times on the same Readable stream blocks forever HOT 6
- Is HTTPure production ready? HOT 14
- PureScript 0.15-compatible release. HOT 3
- Parse cookies
- build errors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from purescript-httpure.