Hi, I’m trying to figure out how to choreograph the initialisation of my
app. When I load the page I need to take into account routing and the need
to look in local storage for a Jwt token. I need the token first as if the
token is not there or expired, I can ignore the routing and send the user
to the login page.
At present I get the token using a port, with this framework for javascript
calls
jsport : Signal.Mailbox JSMsg
jsport =
Signal.mailbox <| JSMsg "First message" Nothing
jsTask : JSMsg -> Task x ()
jsTask action msg =
Signal.send jsport.address msg
sendViaJSPort : (() -> a) -> JSMsg -> Effects a
sendViaJSPort action msg =
jsTask msg
|> Task.map action
|> Effects.task
And on the JS side:
elm.ports.jsOutgoing.subscribe( msg => {
switch (msg.msgType) {
case "saveLocal":
localStorage.setItem('token', msg.payload);
break;
case "getToken":
const token = localStorage.getItem('token');
const t = getTime();
elm.ports.token.send([t, token]);
break;
Meanwhile I am using elm-route-hash and have this in my Main.elm
app : StartApp.App Model
app =
StartApp.start
{ init = init
, update = update
, view = view
, inputs =
[ Signal.map LocalToken token
, messages.signal -- Routing
]
}
How can I ensure I get the token first, or do I need to store the route
temporarily in my model if it arrives before the token?
The problem as I see it is that jsTask returns Task x () so I cannot use
Task.andThen for sequencing, even if I could squeeze elm-route-hash into a
Task too, which I’m not sure about.
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.