XuqmGroup-RNSDK/src/sdk.ts

64 行
1.4 KiB
TypeScript

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<void> {
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<void> {
2026-05-07 19:39:41 +08:00
if (
currentSession &&
currentSession.userId === options.userId &&
currentSession.userSig === options.userSig
) {
setCommonUserId(options.userId)
return
}
if (currentSession) {
await logout()
}
await applyLoginSession(options)
}
async function logout(): Promise<void> {
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,
}