Merge branch 'main' of github.com:hcengineering/anticrm into main

This commit is contained in:
Andrey Platov 2021-08-08 23:04:58 +02:00
commit 46de7cd706
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
19 changed files with 168 additions and 24 deletions

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/client-resources",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/client-resources"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/client",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/client"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/core",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/core"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/dev-server",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/dev-server"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/login-resources",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/login-resources"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/login",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/login"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/model-workbench",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/model-workbench"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/presentation",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/presentation"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/query",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/query"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/server-ws",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/server-ws"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/workbench-resources",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/workbench-resources"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@anticrm/workbench",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "@anticrm/workbench"
}

View File

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "prod",
"comment": "Fix server connection",
"type": "patch"
}
],
"packageName": "prod"
}

View File

@ -30,6 +30,7 @@ export class DevSession implements Storage {
async tx (tx: Tx): Promise<void> {
const derived = await this.storage.tx(tx)
this.server.broadcast(this, { result: tx })
for (const tx of derived) {
this.server.broadcast(this, { result: tx })
}

View File

@ -32,10 +32,13 @@ export type TxHander = (tx: Tx) => void
* @public
*/
export interface Client extends Storage {
notify?: (tx: Tx) => void
getHierarchy: () => Hierarchy
}
class ClientImpl implements Storage {
class ClientImpl implements Client {
notify?: (tx: Tx) => void
constructor (private readonly hierarchy: Hierarchy, private readonly model: ModelDb, private readonly conn: Storage) {
}
@ -54,7 +57,11 @@ class ClientImpl implements Storage {
}
async tx (tx: Tx): Promise<void> {
if (tx.objectSpace === core.space.Model) {
this.hierarchy.tx(tx)
}
await Promise.all([this.conn.tx(tx), this.model.tx(tx)])
this.notify?.(tx)
}
}
@ -62,8 +69,7 @@ class ClientImpl implements Storage {
* @public
*/
export async function createClient (
connect: (txHandler: TxHander) => Promise<Storage>,
notify?: (tx: Tx) => void
connect: (txHandler: TxHander) => Promise<Storage>
): Promise<Client> {
let client: Client | null = null
let txBuffer: Tx[] | undefined = []
@ -75,13 +81,8 @@ export async function createClient (
if (client === null) {
txBuffer?.push(tx)
} else {
if (tx.objectSpace === core.space.Model) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
hierarchy.tx(tx)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
model.tx(tx)
}
notify?.(tx)
console.log('handler got ', tx)
client.notify?.(tx)
}
}

View File

@ -16,7 +16,7 @@
import { onDestroy } from 'svelte'
import { Doc, Ref, Class, DocumentQuery, FindOptions, Client, Hierarchy } from '@anticrm/core'
import { Doc, Ref, Class, DocumentQuery, FindOptions, Client, Hierarchy, Tx } from '@anticrm/core'
import { TxOperations } from '@anticrm/core'
import { LiveQuery as LQ } from '@anticrm/query'
import core from '@anticrm/core'
@ -26,13 +26,17 @@ let client: Client & TxOperations
class UIClient extends TxOperations implements Client {
constructor (private readonly client: Client) {
constructor (private readonly client: Client, private readonly liveQuery: LQ) {
super(client, core.account.System)
}
getHierarchy (): Hierarchy {
return this.client.getHierarchy()
}
}
tx(tx: Tx): Promise<void> {
return Promise.all([super.tx(tx), this.liveQuery.tx(tx)]) as unknown as Promise<void>
}
}
export function getClient(): Client & TxOperations {
@ -41,7 +45,10 @@ export function getClient(): Client & TxOperations {
export function setClient(_client: Client) {
liveQuery = new LQ(_client)
client = new UIClient(liveQuery)
client = new UIClient(_client, liveQuery)
_client.notify = (tx: Tx) => {
liveQuery.tx(tx)
}
}
class LiveQuery {

View File

@ -108,6 +108,7 @@ export class LiveQuery extends TxProcessor implements Client {
}
protected async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
console.log('query tx', tx)
for (const q of this.queries) {
const doc = TxProcessor.createDoc2Doc(tx)
if (this.match(q, doc)) {
@ -146,7 +147,7 @@ export class LiveQuery extends TxProcessor implements Client {
}
async tx (tx: Tx): Promise<void> {
await this.client.tx(tx)
// await this.client.tx(tx)
await super.tx(tx)
}

View File

@ -31,12 +31,10 @@ class DeferredPromise {
}
class Connection implements Storage {
private readonly webSocket: WebSocket
private readonly requests = new Map<ReqId, DeferredPromise>()
private lastId = 0
constructor (url: string, private readonly handler: TxHander) {
this.webSocket = new WebSocket(url)
constructor (private readonly webSocket: WebSocket, private readonly handler: TxHander) {
this.webSocket.onmessage = (event: MessageEvent) => {
const resp = readResponse(event.data)
if (resp.id !== undefined) {
@ -49,6 +47,7 @@ class Connection implements Storage {
promise.resolve(resp.result)
}
} else {
console.log('handle', resp)
this.handler(resp.result as Tx)
}
}
@ -76,5 +75,11 @@ class Connection implements Storage {
}
export async function connect (url: string, handler: TxHander): Promise<Storage> {
return new Connection(url, handler)
return await new Promise((resolve, reject) => {
const webSocket = new WebSocket(url)
webSocket.onopen = () => {
resolve(new Connection(webSocket, handler))
}
webSocket.onerror = () => reject(new Error('Could not connect'))
})
}

View File

@ -29,11 +29,9 @@ export interface _Token {
async function handleRequest<S> (service: S, ws: WebSocket, msg: string): Promise<void> {
const request = readRequest(msg)
const f = (service as any)[request.method]
const result = await f.apply(null, request.params)
ws.send(serialize({
id: request.id,
result
}))
const result = await f.apply(service, request.params)
const resp = { id: request.id, result }
ws.send(serialize(resp))
}
/**
@ -62,6 +60,7 @@ export function start (sessionFactory: (server: JsonRpcServer) => Session, port:
const jsonServer: JsonRpcServer = {
broadcast (from: Session, resp: Response<[]>) {
console.log('server broadcasting', resp)
const msg = serialize(resp)
for (const session of sessions) {
if (session[0] !== from) { session[1].send(msg) }