Skip to content

Commit

Permalink
Refresh client hourly when inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
StachuDotNet committed Dec 16, 2022
1 parent a37c15c commit 865ba86
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
37 changes: 34 additions & 3 deletions client/src/app/Main.res
Expand Up @@ -1832,8 +1832,8 @@ let update_ = (msg: msg, m: model): modification => {
| TimerFire(action, _) =>
switch action {
| RefreshAnalysis =>
let getUnlockedDBs = // Small optimization
if Map.length(m.dbs) > 0 {
// Small optimization - only refresh unlocked DBs if there's at least 1 known
let getUnlockedDBs = if !Map.isEmpty(m.dbs) {
GetUnlockedDBsAPICall
} else {
NoChange
Expand All @@ -1853,7 +1853,28 @@ let update_ = (msg: msg, m: model): modification => {
Many(list{GetWorkerStatsAPICall(TL.id(tl)), getUnlockedDBs})
| _ => getUnlockedDBs
}

| RefreshAvatars => ExpireAvatars

| RefreshPageIfUrlIsStable =>
switch m.visibility {
| Visible => NoChange
| Hidden =>
switch m.currentPage {
| FocusedPackageManagerFn(_)
| Architecture =>
Webapi.Dom.location->Webapi.Dom.Location.reload

| FocusedFn(_)
| FocusedDB(_)
| FocusedType(_)
| SettingsModal(_)
| FocusedHandler(_) => ()
}

NoChange
}

| _ => NoChange
}
| IgnoreMsg(_) =>
Expand Down Expand Up @@ -2203,8 +2224,17 @@ let subscriptions = (m: model): Tea.Sub.t<msg> => {
}

let timers = if m.editorSettings.runTimers {
let refreshClientHourly = Tea.Time.every(
~key="refresh_client",
Tea.Time.hour,
f => AppTypes.Msg.TimerFire(RefreshPageIfUrlIsStable, f),
)

// Note: putting a timer in the 'hidden' list doesn't prevent it from being run
// only when the page is invisible. Rather, it only prevents us from _starting_
// the timer until the page is invisible.
switch m.visibility {
| Hidden => list{}
| Hidden => list{refreshClientHourly}
| Visible => list{
Tea.Time.every(~key="refresh_analysis", Tea.Time.second, f => AppTypes.Msg.TimerFire(
RefreshAnalysis,
Expand All @@ -2214,6 +2244,7 @@ let subscriptions = (m: model): Tea.Sub.t<msg> => {
RefreshAvatars,
f,
)),
refreshClientHourly,
}
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions client/src/core/Types.res
Expand Up @@ -138,6 +138,9 @@ and timerAction =
| RefreshAnalysis
| RefreshAvatars
| CheckUrlHashPosition
// TODO: 'stable' is not the right word.
// I really mean "a refresh would leave them in the same place without interruption"
| RefreshPageIfUrlIsStable

and traceID = string

Expand Down

0 comments on commit 865ba86

Please sign in to comment.