Re: [elm-discuss] How do I subscribe to an event at a fixed point in time?

2016-11-09 Thread 'Rupert Smith' via Elm Discuss
On Wednesday, November 9, 2016 at 10:22:05 AM UTC, Rupert Smith wrote:
>
> Or can I chain Time.now andThen Process.sleep andThen invokeRefresh 
> together without needing to issue a Cmd and have and update handler for the 
> intermediate steps? I'll look into that. 
>

Chaining the tasks together turned out to be fairly easy once I got my head 
around it:

tokenExpiryTask : Date -> Task.Task Http.Error Model.AuthResponse
tokenExpiryTask refreshDate =
let
delay expiryDate now =
max 0 ((Date.toTime expiryDate) - now - Time.second * 30)
in
Time.now
`andThen` (\now -> Process.sleep <| delay refreshDate now)
`andThen` (\_ -> Auth.Service.refreshTask)
 
Thanks for the pointers in the right direction.

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] How do I subscribe to an event at a fixed point in time?

2016-11-09 Thread 'Rupert Smith' via Elm Discuss
On Wednesday, November 9, 2016 at 10:18:39 AM UTC, Rupert Smith wrote:
>
> On Tuesday, November 8, 2016 at 8:09:25 PM UTC, Max Goldstein wrote:
>>
>> Could you run Time.now andThen calculate the amount of time left until 
>> expiry, then Process.sleep until then?
>
>
> Yes, I think this is how it needs to be done:
>
> Run Time.now to produce a Msg, say 'SetupRefresh'
>
> In the update handler for 'SetupRefresh' calculate the delay needed as 
> (expiryTime - now), then use Process.sleep andThen invokeRefresh, where 
> invokeRefresh is my Task for invoking the refresh call.
>

Or can I chain Time.now andThen Process.sleep andThen invokeRefresh 
together without needing to issue a Cmd and have and update handler for the 
intermediate steps? I'll look into that. 

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] How do I subscribe to an event at a fixed point in time?

2016-11-09 Thread 'Rupert Smith' via Elm Discuss
On Tuesday, November 8, 2016 at 5:28:26 PM UTC, Witold Szczerba wrote:
>
> You could propably "tick" (as in example from docs) every few seconds and 
> return Cmd.none if you're before expiration, and some other Cmd otherwise. 
> Does it make sense?
>

I could, but there is still the inconvenience of having to make a request 
to get Time.now and receive the result back as a Msg to the update 
function. So it would be tick -> get now -> compare now to expiry time -> 
invoke refresh.

Also at the moment I am logging all events to my update functions, it would 
be kind of annoying to have a tick event every few seconds, when it should 
not really be necessary. But thanks for the suggestion. 

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] How do I subscribe to an event at a fixed point in time?

2016-11-09 Thread 'Rupert Smith' via Elm Discuss
On Tuesday, November 8, 2016 at 8:09:25 PM UTC, Max Goldstein wrote:
>
> Could you run Time.now andThen calculate the amount of time left until 
> expiry, then Process.sleep until then?


Yes, I think this is how it needs to be done:

Run Time.now to produce a Msg, say 'SetupRefresh'

In the update handler for 'SetupRefresh' calculate the delay needed as 
(expiryTime - now), then use Process.sleep andThen invokeRefresh, where 
invokeRefresh is my Task for invoking the refresh call.

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] How do I subscribe to an event at a fixed point in time?

2016-11-08 Thread Max Goldstein
Could you run Time.now andThen calculate the amount of time left until expiry, 
then Process.sleep until then?

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] How do I subscribe to an event at a fixed point in time?

2016-11-08 Thread Witold Szczerba
You could propably "tick" (as in example from docs) every few seconds and
return Cmd.none if you're before expiration, and some other Cmd otherwise.
Does it make sense?

Regards,
Witold Szczerba

08.11.2016 5:22 PM "'Rupert Smith' via Elm Discuss" <
elm-discuss@googlegroups.com> napisaƂ(a):

> I have an authentication token that I know expires at a particular moment
> in time:
>
> type alias AuthState =
> { loggedIn : Bool
> , permissions : List String
> , expiresAt : Maybe Date
> }
>
> I would like to fire an event once it gets close to 'expiresAt'.
>
> What happens if I update the subscription
>
> subscriptions : Model -> Sub Msg
> subscriptions model =
>   Time.every ((Date.toTime model.authState.expiresAt) - now)
> RefreshTokenMsg
>
> I'm worried that as this subscription will be re-evaluated every time the
> model changes, that Time.every may somehow miss this expiry time. Say there
> are some other events causing model changes very close to the expiry time,
> can re-evaluation this subscription cause a one off firing of it to be
> missed?
>
> Is there some better way to subscribe to a one-off event?
>
> Or perhaps I should change the logic so once a point in time is passed
> that is close to the expiry time, I start regularly firing RefreshTokenMsg,
> perhaps every couple of secods. That way any glitches will be recovered
> from:
>
> subscriptions : Model -> Sub Msg
> subscriptions model =
>   if afterCloseToExpiryTime then
>Time.every Time.second RefreshTokenMsg
>   else
>Sub.none
>
> --
> 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 elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.