Skip to content

Commit

Permalink
backend: auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Oct 7, 2024
1 parent 7364eb4 commit f178f18
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export async function disconnect() {
return
}

connection.face?.removeEventListener('down', reconnect)

connState.value = 'DISCONNECTING'
if (connection.config.kind !== 'peerJs') {
await checkPrefixRegistration(true)
Expand Down Expand Up @@ -88,16 +90,36 @@ export async function connect(config: connections.Config) {
connState.value = 'DISCONNECTED'
return
}
connection.face!.addEventListener('down', () => {
disconnect()
})

// Reconnect and initialize when ws breaks, but keep the
// face around. This means the application does not see any change.
connection.face?.addEventListener('down', reconnect)

workspace?.fireUpdate()
connState.value = 'CONNECTED'

toast.success('Connected to forwarder successfully!')
}

async function reconnect() {
toast.promise(
new Promise<void>((resolve, reject) => {
connection?.face?.addEventListener('up', async () => {

Check failure on line 107 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L107

Replace `'up',` with `⏎········'up',⏎·······` (prettier/prettier)
resolve()

Check failure on line 108 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L108

Insert `··` (prettier/prettier)

// The sleep here is needed otherwise prefix registration fails

Check failure on line 110 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L110

Insert `··` (prettier/prettier)
setTimeout(() => checkPrefixRegistration(false), 500)

Check failure on line 111 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L111

Insert `··` (prettier/prettier)
}, { once: true })

Check failure on line 112 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L112

Replace `},·{·once:·true·}` with `··},⏎········{·once:·true·},⏎······` (prettier/prettier)
connection?.face?.addEventListener('close', () => reject(), { once: true })
}),
{
loading: 'Disconnected from forwarding, attempting to reconnect ...',
success: (val) => 'Reconnected to forwarder!',

Check warning on line 117 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L117

'val' is defined but never used (@typescript-eslint/no-unused-vars)
error: 'Failed to reconnect to forwarder',
}

Check failure on line 119 in src/backend/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/backend/main.ts#L119

Insert `,` (prettier/prettier)
)
}

// ============= Bootstrapping =============

export async function bootstrapWorkspace(opts: {
Expand Down

0 comments on commit f178f18

Please sign in to comment.