fix pulse issues (#10144)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov
2025-10-24 18:47:37 +07:00
committed by GitHub
parent f2e0ca1453
commit c6706903f7
4 changed files with 81 additions and 53 deletions
+37 -11
View File
@@ -15,24 +15,50 @@ import { HulypulseClient } from '@hcengineering/hulypulse-client'
import { getMetadata } from '@hcengineering/platform'
import presentation from './plugin'
let pulseClient: HulypulseClient | undefined
let currentWorkspaceUuid: string | undefined
let currentToken: string | undefined
let promise: Promise<HulypulseClient | undefined> | undefined
export async function createPulseClient (): Promise<HulypulseClient | undefined> {
const token = getMetadata(presentation.metadata.Token)
if (token !== currentToken) {
const pulseUrl = getMetadata(presentation.metadata.PulseUrl) ?? ''
const token = getMetadata(presentation.metadata.Token) ?? ''
const workspaceUuid = getMetadata(presentation.metadata.WorkspaceUuid) ?? ''
if (pulseUrl === '' || token === '' || workspaceUuid === '') {
return undefined
}
// Token or workspace changed, need to reconnect
if (token !== currentToken || workspaceUuid !== currentWorkspaceUuid) {
closePulseClient()
}
if (pulseClient === undefined) {
const wsPulseUrl = getMetadata(presentation.metadata.PulseUrl)
if (wsPulseUrl == null || wsPulseUrl.trim().length === 0) return undefined
pulseClient = await HulypulseClient.connect(`${wsPulseUrl}?token=${token}`)
currentToken = token
if (promise !== undefined) {
// eslint-disable-next-line @typescript-eslint/return-await
return promise
}
return pulseClient
promise = new Promise((resolve) => {
HulypulseClient.connect(`${pulseUrl}?token=${token}`)
.then(resolve)
.catch(() => {
resolve(undefined)
})
})
currentToken = token
currentWorkspaceUuid = workspaceUuid
// eslint-disable-next-line @typescript-eslint/return-await
return promise
}
export function closePulseClient (): void {
pulseClient?.close()
pulseClient = undefined
if (promise !== undefined) {
void promise.then((client) => {
client?.close()
})
}
promise = undefined
currentToken = undefined
currentWorkspaceUuid = undefined
}