Looking at purescript-signal examples, I have seen that a common pattern is to use a pure update function and an effectful render function this way:
runSignal $ map render (foldp update initialState input)
But what to do if rendering needs to keep a state, e.g. because effectful rendering operations inside render return values that are needed by following calls to render?
The unwrap function looked to me like the way to go, with something like this:
unwrap $ foldp render (pure initialRenderState) (foldp update initialState input)
I made the following code to try out unwrap, but it did not work as I expected. I was expecting it to print subsequent integers starting from 0, with a delay of one second between them. Instead, every second it prints not only the new number, but also all the previous ones.
updateEff :: forall eff. Number -> Eff (console :: CONSOLE | eff) Int -> Eff (console :: CONSOLE | eff) Int
updateEff _ stateEff = do
state <- stateEff
log $ show state
pure $ state + 1
main = do
unwrap $ foldp updateEff (pure 0) (every 1000.0)
Again, this code is just an example to try out unwrap, I know that these particular operations could be rewritten e.g. using a pure update function and a "render" function: runSignal $ map render (foldp update 0 (every 1000.0))
Why is the whole sequence of numbers getting printed again every time?