From 78efa54777361a171502861c647d071ba1ca143e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?NB=F0=9F=98=88?= Date: Sat, 29 Jul 2023 17:29:03 +0200 Subject: [PATCH] fix WS disconnect in background --- sdk/apps/solana/src/app.ts | 41 +++++++++++++++++++++++++++++++------- sdk/apps/sui/src/app.ts | 40 +++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/sdk/apps/solana/src/app.ts b/sdk/apps/solana/src/app.ts index 2a06e4a6..a5ca2b58 100644 --- a/sdk/apps/solana/src/app.ts +++ b/sdk/apps/solana/src/app.ts @@ -22,10 +22,10 @@ interface SolanaAppEvents { export class AppSolana extends EventEmitter { 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) => { @@ -34,10 +34,38 @@ export class AppSolana extends EventEmitter { 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 } @@ -49,8 +77,7 @@ export class AppSolana extends EventEmitter { } public static build = async (initData: AppSolanaInitialize): Promise => { 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) diff --git a/sdk/apps/sui/src/app.ts b/sdk/apps/sui/src/app.ts index 9302ad07..21a99f4a 100644 --- a/sdk/apps/sui/src/app.ts +++ b/sdk/apps/sui/src/app.ts @@ -27,10 +27,10 @@ interface SuiAppEvents { export class AppSui extends EventEmitter { 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) => { @@ -39,10 +39,38 @@ export class AppSui extends EventEmitter { 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 } @@ -54,7 +82,7 @@ export class AppSui extends EventEmitter { } public static build = async (initData: AppSuiInitialize): Promise => { 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)