When you look at this excerpt of "An Elm introduction":
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every 1000 Tick
The model
parameter passed to the function subscriptions
must correspond to the current model of the app, i.e. if the model has changed in less that 1 second, the argument model
passed to the function subscriptions
must have taken into account this modification.
As in JS, there are 3 different ways to make a variable calculated in one function update msg model
visible in another subscriptions model
:
- make it a global,
- make it an object property,
- pass it as a parameter when calling B from A.
But only 2 if you have async code as in the function subscriptions
to keep your model 'sync':
1- make it a global variable, then by reassignment, update the value of the current model:
function app() {
const currentModel = initModel
function update(msg, model) {
const newModel = doSomething(msg, model)
// REASSIGNMENT
currentModel = newModel
}
function subscriptions() {
doSomethingEveryOneSecond(currentModel)
}
}
2- make it an object property
function app() {
const model = initModel
function update(msg, model) {
// MUTATION
model.value = doSomething(msg, model)
}
// model ALWAYS represents the current model
function subscriptions(model) {
doSomethingEveryOneSecond(model)
}
}
My question is how does it work “under the hood” in The Elm Architecture to keep the model sync between the update
and the subscriptions
functions?
Thanks a LOT!