mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-13 13:17:45 +02:00
Backup restore support (#1878)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
@@ -29,6 +29,7 @@ import type {
|
||||
TxHander,
|
||||
TxResult
|
||||
} from '@anticrm/core'
|
||||
import core from '@anticrm/core'
|
||||
import { getMetadata, PlatformError, readResponse, ReqId, serialize } from '@anticrm/platform'
|
||||
|
||||
class DeferredPromise {
|
||||
@@ -49,7 +50,11 @@ class Connection implements ClientConnection {
|
||||
private lastId = 0
|
||||
private readonly interval: number
|
||||
|
||||
constructor (private readonly url: string, private readonly handler: TxHander) {
|
||||
constructor (
|
||||
private readonly url: string,
|
||||
private readonly handler: TxHander,
|
||||
private readonly onUpgrade?: () => void
|
||||
) {
|
||||
console.log('connection created')
|
||||
this.interval = setInterval(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
@@ -62,37 +67,69 @@ class Connection implements ClientConnection {
|
||||
this.websocket?.close()
|
||||
}
|
||||
|
||||
private openConnection (): Promise<ClientSocket> {
|
||||
// Use defined factory or browser default one.
|
||||
const clientSocketFactory =
|
||||
getMetadata(client.metadata.ClientSocketFactory) ?? ((url: string) => new WebSocket(url) as ClientSocket)
|
||||
private async waitOpenConnection (): Promise<ClientSocket> {
|
||||
while (true) {
|
||||
try {
|
||||
return await this.openConnection()
|
||||
} catch (err: any) {
|
||||
console.log('failed to connect')
|
||||
|
||||
const websocket = clientSocketFactory(this.url)
|
||||
websocket.onmessage = (event: MessageEvent) => {
|
||||
const resp = readResponse(event.data)
|
||||
if (resp.id !== undefined) {
|
||||
const promise = this.requests.get(resp.id)
|
||||
if (promise === undefined) {
|
||||
throw new Error(`unknown response id: ${resp.id}`)
|
||||
}
|
||||
this.requests.delete(resp.id)
|
||||
if (resp.error !== undefined) {
|
||||
promise.reject(new PlatformError(resp.error))
|
||||
} else {
|
||||
promise.resolve(resp.result)
|
||||
}
|
||||
} else {
|
||||
this.handler(resp.result as Tx)
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(null)
|
||||
}, 1000)
|
||||
})
|
||||
}
|
||||
}
|
||||
websocket.onclose = () => {
|
||||
console.log('client websocket closed')
|
||||
// clearInterval(interval)
|
||||
this.websocket = null
|
||||
}
|
||||
}
|
||||
|
||||
private openConnection (): Promise<ClientSocket> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Use defined factory or browser default one.
|
||||
const clientSocketFactory =
|
||||
getMetadata(client.metadata.ClientSocketFactory) ?? ((url: string) => new WebSocket(url) as ClientSocket)
|
||||
|
||||
const websocket = clientSocketFactory(this.url)
|
||||
websocket.onmessage = (event: MessageEvent) => {
|
||||
const resp = readResponse(event.data)
|
||||
if (resp.id === -1 && resp.result === 'hello') {
|
||||
resolve(websocket)
|
||||
return
|
||||
}
|
||||
if (resp.id !== undefined) {
|
||||
const promise = this.requests.get(resp.id)
|
||||
if (promise === undefined) {
|
||||
throw new Error(`unknown response id: ${resp.id}`)
|
||||
}
|
||||
this.requests.delete(resp.id)
|
||||
if (resp.error !== undefined) {
|
||||
promise.reject(new PlatformError(resp.error))
|
||||
} else {
|
||||
promise.resolve(resp.result)
|
||||
}
|
||||
} else {
|
||||
const tx = resp.result as Tx
|
||||
if (tx._class === core.class.TxModelUpgrade) {
|
||||
this.onUpgrade?.()
|
||||
}
|
||||
this.handler(tx)
|
||||
}
|
||||
}
|
||||
websocket.onclose = () => {
|
||||
console.log('client websocket closed')
|
||||
// clearInterval(interval)
|
||||
this.websocket = null
|
||||
reject(new Error('websocket error'))
|
||||
}
|
||||
websocket.onopen = () => {
|
||||
resolve(websocket)
|
||||
console.log('connection opened...')
|
||||
websocket.send(
|
||||
serialize({
|
||||
method: 'hello',
|
||||
params: [],
|
||||
id: -1
|
||||
})
|
||||
)
|
||||
}
|
||||
websocket.onerror = (event: any) => {
|
||||
console.log('client websocket error', event)
|
||||
@@ -103,7 +140,8 @@ class Connection implements ClientConnection {
|
||||
|
||||
private async sendRequest (method: string, ...params: any[]): Promise<any> {
|
||||
if (this.websocket === null) {
|
||||
this.websocket = await this.openConnection()
|
||||
console.log('open connection from', method, params)
|
||||
this.websocket = await this.waitOpenConnection()
|
||||
}
|
||||
const id = this.lastId++
|
||||
this.websocket.send(
|
||||
@@ -141,11 +179,19 @@ class Connection implements ClientConnection {
|
||||
loadDocs (domain: Domain, docs: Ref<Doc>[]): Promise<Doc[]> {
|
||||
return this.sendRequest('loadDocs', domain, docs)
|
||||
}
|
||||
|
||||
upload (domain: Domain, docs: Doc[]): Promise<void> {
|
||||
return this.sendRequest('upload', domain, docs)
|
||||
}
|
||||
|
||||
clean (domain: Domain, docs: Ref<Doc>[]): Promise<void> {
|
||||
return this.sendRequest('clean', domain, docs)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function connect (url: string, handler: TxHander): Promise<ClientConnection> {
|
||||
return new Connection(url, handler)
|
||||
export async function connect (url: string, handler: TxHander, onUpgrade?: () => void): Promise<ClientConnection> {
|
||||
return new Connection(url, handler, onUpgrade)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ export default async () => {
|
||||
|
||||
return {
|
||||
function: {
|
||||
GetClient: async (token: string, endpoint: string): Promise<Client> => {
|
||||
GetClient: async (token: string, endpoint: string, onUpgrade?: () => void): Promise<Client> => {
|
||||
if (token !== _token && client !== undefined) {
|
||||
await client.close()
|
||||
client = undefined
|
||||
@@ -43,10 +43,11 @@ export default async () => {
|
||||
(handler: TxHander) => {
|
||||
const url = new URL(`/${token}`, endpoint)
|
||||
console.log('connecting to', url.href)
|
||||
return connect(url.href, handler)
|
||||
return connect(url.href, handler, onUpgrade)
|
||||
},
|
||||
filterModel ? getPlugins() : undefined
|
||||
)
|
||||
console.log('client connection created')
|
||||
_token = token
|
||||
|
||||
// Check if we had dev hook for client.
|
||||
|
||||
Reference in New Issue
Block a user