mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-23 18:15:03 +02:00
fix pulse issues (#10144)
Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user