sdk: add license-based initialization and awaitInitialization
- core/sdk: add initializeFromLicense(), awaitInitialization(), isInitialized() - index: export new functions Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
这个提交包含在:
父节点
b25ae21760
当前提交
d6f69f3723
@ -1,15 +1,61 @@
|
||||
import type { SDKConfig } from '../types'
|
||||
import { configureHttp } from './http'
|
||||
import { DEFAULT_API_BASE_URL } from './endpoints'
|
||||
import { DEFAULT_API_BASE_URL, DEFAULT_IM_WS_URL } from './endpoints'
|
||||
|
||||
let _config: SDKConfig | null = null
|
||||
let _token: string | null = null
|
||||
let _userId: string | null = null
|
||||
let _initialized = false
|
||||
let _initPromise: Promise<void> | null = null
|
||||
let _initResolve: (() => void) | null = null
|
||||
|
||||
function ensureInitPromise(): Promise<void> {
|
||||
if (!_initPromise) {
|
||||
_initPromise = new Promise((resolve) => { _initResolve = resolve })
|
||||
}
|
||||
return _initPromise
|
||||
}
|
||||
|
||||
function markInitialized(): void {
|
||||
_initialized = true
|
||||
if (_initResolve) {
|
||||
_initResolve()
|
||||
_initResolve = null
|
||||
}
|
||||
}
|
||||
|
||||
export function isInitialized(): boolean {
|
||||
return _initialized
|
||||
}
|
||||
|
||||
export function init(config: SDKConfig) {
|
||||
if (_initialized) return
|
||||
_config = config
|
||||
configureHttp(config.baseUrl || DEFAULT_API_BASE_URL, () => _token)
|
||||
if (config.debug) console.log('[XuqmSDK] initialized', config.appKey, 'baseUrl=', config.baseUrl || DEFAULT_API_BASE_URL)
|
||||
markInitialized()
|
||||
}
|
||||
|
||||
export function initializeFromLicense(
|
||||
file: { appKey: string; baseUrl?: string; serverUrl?: string },
|
||||
options?: { debug?: boolean },
|
||||
): void {
|
||||
if (_initialized) return
|
||||
const serverUrl = file.serverUrl || file.baseUrl || DEFAULT_API_BASE_URL
|
||||
_config = {
|
||||
appKey: file.appKey,
|
||||
debug: options?.debug,
|
||||
baseUrl: serverUrl,
|
||||
wsUrl: serverUrl.replace(/^https/, 'wss') + '/ws/im',
|
||||
}
|
||||
configureHttp(serverUrl, () => _token)
|
||||
if (options?.debug) console.log('[XuqmSDK] initialized from license', file.appKey, 'baseUrl=', serverUrl)
|
||||
markInitialized()
|
||||
}
|
||||
|
||||
export async function awaitInitialization(): Promise<void> {
|
||||
if (_initialized) return
|
||||
await ensureInitPromise()
|
||||
}
|
||||
|
||||
export function setToken(token: string | null) {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export { init, login, logout, setToken, setUserId, getToken, getUserId, getConfig } from './core/sdk'
|
||||
export { init, login, logout, setToken, setUserId, getToken, getUserId, getConfig, isInitialized, awaitInitialization, initializeFromLicense } from './core/sdk'
|
||||
export { http, uploadFile } from './core/http'
|
||||
export { ImClient } from './im/ImClient'
|
||||
export { ImManager } from './im/ImManager'
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户