import { _clearToken, setUserId as setCommonUserId, getUserId as getCommonUserId, XuqmSDK as CommonXuqmSDK } from '@xuqm/rn-common' import { ImSDK } from '@xuqm/rn-im' import { PushSDK } from '@xuqm/rn-push' export interface UnifiedLoginOptions { userId: string userSig: string } let currentSession: UnifiedLoginOptions | null = null async function applyLoginSession(session: UnifiedLoginOptions): Promise { setCommonUserId(session.userId) try { await ImSDK.login(session.userId, session.userSig) } catch (error) { setCommonUserId(null) currentSession = null throw error } currentSession = session try { await PushSDK.initialize(session.userId) } catch (error) { void error } } async function login(options: UnifiedLoginOptions): Promise { if (currentSession) { await logout() } await applyLoginSession(options) } async function logout(): Promise { const userId = currentSession?.userId ?? getCommonUserId() currentSession = null setCommonUserId(null) if (userId) { try { await PushSDK.logout(userId) } catch (error) { void error } } ImSDK.disconnect() await _clearToken() } export const XuqmSDK = { ...CommonXuqmSDK, login, logout, }