Skip to content

Commit

Permalink
delay phx-disconnected to prevent reconnect flash
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Feb 18, 2025
1 parent e73735c commit 33c4239
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions assets/js/phoenix_live_view/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const PHX_RELOAD_STATUS = "__phoenix_reload_status__"
export const LOADER_TIMEOUT = 1
export const MAX_CHILD_JOIN_ATTEMPTS = 3
export const BEFORE_UNLOAD_LOADER_TIMEOUT = 200
export const DISCONNECTED_TIMEOUT = 500
export const BINDING_PREFIX = "phx-"
export const PUSH_TIMEOUT = 30000
export const LINK_HEADER = "x-requested-with"
Expand Down
11 changes: 10 additions & 1 deletion assets/js/phoenix_live_view/view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BEFORE_UNLOAD_LOADER_TIMEOUT,
DISCONNECTED_TIMEOUT,
CHECKABLE_INPUTS,
CONSECUTIVE_RELOADS,
PHX_AUTO_RECOVER,
Expand Down Expand Up @@ -143,6 +144,7 @@ export default class View {
this.lastAckRef = null
this.childJoins = 0
this.loaderTimer = null
this.disconnectedTimer = null
this.pendingDiffs = []
this.pendingForms = new Set()
this.redirect = false
Expand Down Expand Up @@ -254,6 +256,7 @@ export default class View {

hideLoader(){
clearTimeout(this.loaderTimer)
clearTimeout(this.disconnectedTimer)
this.setContainerClasses(PHX_CONNECTED_CLASS)
this.execAll(this.binding("connected"))
}
Expand Down Expand Up @@ -891,7 +894,13 @@ export default class View {
if(this.isMain()){ DOM.dispatchEvent(window, "phx:page-loading-start", {detail: {to: this.href, kind: "error"}}) }
this.showLoader()
this.setContainerClasses(...classes)
this.execAll(this.binding("disconnected"))
this.delayedDisconnected()
}

delayedDisconnected(){
this.disconnectedTimer = setTimeout(() => {
this.execAll(this.binding("disconnected"))
}, DISCONNECTED_TIMEOUT)
}

wrapPush(callerPush, receives){
Expand Down

0 comments on commit 33c4239

Please sign in to comment.