Skip to content

Commit

Permalink
Use browser event visibilitychange to check if need to reconnect
Browse files Browse the repository at this point in the history
I got clarification that the renewal stuff doesn't exist anymore and
I just need to keep checking if disconnected and reconnect if so.
  • Loading branch information
KyleAMathews committed Mar 5, 2024
1 parent 43c1936 commit 02f9884
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export async function initElectric(params: InitElectricParams) {
await electric.connect(params.token)
console.log(`connected`, { token: params.token })

if (params.getToken && params.renewInterval) {
// let stopRenewing = renewPeriodically(electric, params.renewInterval)

if (params.getToken) {
// Subscribe to connectivity changes to detect JWT expiration
electric.notifier.subscribeToConnectivityStateChanges(async (event) => {
console.log({ event })
Expand All @@ -55,27 +53,23 @@ export async function initElectric(params: InitElectricParams) {
event.connectivityState.reason?.code === SatelliteErrorCode.AUTH_EXPIRED
) {
console.log(`JWT expired, reconnecting...`)
// stopRenewing() // NOTE: the connectivity state change event is async and is fired after the socket to Electric is closed. Between the socket closing and this event firing, we may have tried to renew the token which will fail
const newToken = await params.getToken()
await electric.connect(newToken)
console.log(`connection restored`)
// stopRenewing = renewPeriodically(electric, oneMinute)
}
})

// Renews the JWT periodically
// and returns a function that can be called to stop renewing
// function renewPeriodically(electric: Client, ms: number) {
// const id = setInterval(async () => {
// // Renew the JWT
// const renewedToken = await params.getToken()
// await electric.authenticate(renewedToken)
// console.log(`Renewed JWT`)
// }, ms)
// return () => {
// clearInterval(id)
// }
// }
document.addEventListener(`visibilitychange`, async function () {
if (document.visibilityState === `visible`) {
if (!electric.isConnected) {
if (params.getToken) {
const newToken = await params.getToken()
await electric.connect(newToken)
console.log(`reconnected`)
}
}
}
})
}

electricResolve(electric)
Expand Down

0 comments on commit 02f9884

Please sign in to comment.