Skip to content

Commit

Permalink
Merge pull request #50 from nightly-labs/fix-WS-disconnect-in-background
Browse files Browse the repository at this point in the history
fix WS disconnect in background
  • Loading branch information
NorbertBodziony authored Jul 29, 2023
2 parents 1ce2934 + 78efa54 commit 171df00
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
41 changes: 34 additions & 7 deletions sdk/apps/solana/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ interface SolanaAppEvents {
export class AppSolana extends EventEmitter<SolanaAppEvents> {
sessionId: string
base: BaseApp

constructor(base: BaseApp) {
initData: AppSolanaInitialize
constructor(base: BaseApp, initData: AppSolanaInitialize) {
super()

this.initData = initData
this.base = base
this.sessionId = base.sessionId
this.base.on('userConnected', (e) => {
Expand All @@ -34,10 +34,38 @@ export class AppSolana extends EventEmitter<SolanaAppEvents> {
this.base.on('userDisconnected', (e) => {
this.emit('userDisconnected', e)
})
this.base.on('serverDisconnected', () => {
this.emit('serverDisconnected')
this.base.on('serverDisconnected', async () => {
// We need this because of power saving mode on mobile
await this.tryReconnect()
})
}
private tryReconnect = async () => {
try {
const base = await BaseApp.build({ ...this.initData, network: SOLANA_NETWORK })
// On reconnect, if the base has not been restored, emit serverDisconnected
if (!base.hasBeenRestored) {
this.emit('serverDisconnected')
return
}
base.on('userConnected', (e) => {
this.emit('userConnected', e)
})
base.on('userDisconnected', (e) => {
this.emit('userDisconnected', e)
})
base.on('serverDisconnected', async () => {
await this.tryReconnect()
})
// If there is a deeplink, reconnect to it
if (this.base.deeplink) {
base.connectDeeplink(this.base.deeplink)
}
this.base = base
return
} catch (_) {
this.emit('serverDisconnected')
}
}
public hasBeenRestored = () => {
return this.base.hasBeenRestored
}
Expand All @@ -49,8 +77,7 @@ export class AppSolana extends EventEmitter<SolanaAppEvents> {
}
public static build = async (initData: AppSolanaInitialize): Promise<AppSolana> => {
const base = await BaseApp.build({ ...initData, network: SOLANA_NETWORK })
base.connectDeeplink
return new AppSolana(base)
return new AppSolana(base, initData)
}
connectDeeplink = async (data: DeeplinkConnect) => {
this.base.connectDeeplink(data)
Expand Down
40 changes: 34 additions & 6 deletions sdk/apps/sui/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ interface SuiAppEvents {
export class AppSui extends EventEmitter<SuiAppEvents> {
sessionId: string
base: BaseApp

constructor(base: BaseApp) {
initData: AppSuiInitialize
constructor(base: BaseApp, initData: AppSuiInitialize) {
super()

this.initData = initData
this.base = base
this.sessionId = base.sessionId
this.base.on('userConnected', (e) => {
Expand All @@ -39,10 +39,38 @@ export class AppSui extends EventEmitter<SuiAppEvents> {
this.base.on('userDisconnected', (e) => {
this.emit('userDisconnected', e)
})
this.base.on('serverDisconnected', () => {
this.emit('serverDisconnected')
this.base.on('serverDisconnected', async () => {
// We need this because of power saving mode on mobile
await this.tryReconnect()
})
}
private tryReconnect = async () => {
try {
const base = await BaseApp.build({ ...this.initData, network: SUI_NETWORK })
// On reconnect, if the base has not been restored, emit serverDisconnected
if (!base.hasBeenRestored) {
this.emit('serverDisconnected')
return
}
base.on('userConnected', (e) => {
this.emit('userConnected', e)
})
base.on('userDisconnected', (e) => {
this.emit('userDisconnected', e)
})
base.on('serverDisconnected', async () => {
await this.tryReconnect()
})
// If there is a deeplink, reconnect to it
if (this.base.deeplink) {
base.connectDeeplink(this.base.deeplink)
}
this.base = base
return
} catch (_) {
this.emit('serverDisconnected')
}
}
public hasBeenRestored = () => {
return this.base.hasBeenRestored
}
Expand All @@ -54,7 +82,7 @@ export class AppSui extends EventEmitter<SuiAppEvents> {
}
public static build = async (initData: AppSuiInitialize): Promise<AppSui> => {
const base = await BaseApp.build({ ...initData, network: SUI_NETWORK })
return new AppSui(base)
return new AppSui(base, initData)
}
connectDeeplink = async (data: DeeplinkConnect) => {
this.base.connectDeeplink(data)
Expand Down

0 comments on commit 171df00

Please sign in to comment.