diff --git a/common/changes/@anticrm/client-resources/server_2021-08-08-21-02.json b/common/changes/@anticrm/client-resources/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..1f11f7286a --- /dev/null +++ b/common/changes/@anticrm/client-resources/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/client-resources", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/client-resources" +} \ No newline at end of file diff --git a/common/changes/@anticrm/client/server_2021-08-08-21-02.json b/common/changes/@anticrm/client/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..57e6a66ce8 --- /dev/null +++ b/common/changes/@anticrm/client/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/client", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/client" +} \ No newline at end of file diff --git a/common/changes/@anticrm/core/server_2021-08-08-21-02.json b/common/changes/@anticrm/core/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..6d760ddb72 --- /dev/null +++ b/common/changes/@anticrm/core/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/core", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/core" +} \ No newline at end of file diff --git a/common/changes/@anticrm/dev-server/server_2021-08-08-21-02.json b/common/changes/@anticrm/dev-server/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..8dfb07c566 --- /dev/null +++ b/common/changes/@anticrm/dev-server/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/dev-server", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/dev-server" +} \ No newline at end of file diff --git a/common/changes/@anticrm/login-resources/server_2021-08-08-21-02.json b/common/changes/@anticrm/login-resources/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..246a5bbe1f --- /dev/null +++ b/common/changes/@anticrm/login-resources/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/login-resources", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/login-resources" +} \ No newline at end of file diff --git a/common/changes/@anticrm/login/server_2021-08-08-21-02.json b/common/changes/@anticrm/login/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..9ae8ac5391 --- /dev/null +++ b/common/changes/@anticrm/login/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/login", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/login" +} \ No newline at end of file diff --git a/common/changes/@anticrm/model-workbench/server_2021-08-08-21-02.json b/common/changes/@anticrm/model-workbench/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..381e06f940 --- /dev/null +++ b/common/changes/@anticrm/model-workbench/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/model-workbench", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/model-workbench" +} \ No newline at end of file diff --git a/common/changes/@anticrm/presentation/server_2021-08-08-21-02.json b/common/changes/@anticrm/presentation/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..ef28c58825 --- /dev/null +++ b/common/changes/@anticrm/presentation/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/presentation", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/presentation" +} \ No newline at end of file diff --git a/common/changes/@anticrm/query/server_2021-08-08-21-02.json b/common/changes/@anticrm/query/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..e4c2263835 --- /dev/null +++ b/common/changes/@anticrm/query/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/query", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/query" +} \ No newline at end of file diff --git a/common/changes/@anticrm/server-ws/server_2021-08-08-21-02.json b/common/changes/@anticrm/server-ws/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..807c2fed03 --- /dev/null +++ b/common/changes/@anticrm/server-ws/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/server-ws", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/server-ws" +} \ No newline at end of file diff --git a/common/changes/@anticrm/workbench-resources/server_2021-08-08-21-02.json b/common/changes/@anticrm/workbench-resources/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..2de8f7a836 --- /dev/null +++ b/common/changes/@anticrm/workbench-resources/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/workbench-resources", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/workbench-resources" +} \ No newline at end of file diff --git a/common/changes/@anticrm/workbench/server_2021-08-08-21-02.json b/common/changes/@anticrm/workbench/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..12a98b796c --- /dev/null +++ b/common/changes/@anticrm/workbench/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@anticrm/workbench", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "@anticrm/workbench" +} \ No newline at end of file diff --git a/common/changes/prod/server_2021-08-08-21-02.json b/common/changes/prod/server_2021-08-08-21-02.json new file mode 100644 index 0000000000..576c4f5ab8 --- /dev/null +++ b/common/changes/prod/server_2021-08-08-21-02.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "prod", + "comment": "Fix server connection", + "type": "patch" + } + ], + "packageName": "prod" +} \ No newline at end of file diff --git a/dev/server/src/session.ts b/dev/server/src/session.ts index a22793f797..10b174dd30 100644 --- a/dev/server/src/session.ts +++ b/dev/server/src/session.ts @@ -30,6 +30,7 @@ export class DevSession implements Storage { async tx (tx: Tx): Promise { const derived = await this.storage.tx(tx) + this.server.broadcast(this, { result: tx }) for (const tx of derived) { this.server.broadcast(this, { result: tx }) } diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index 8a38408e3a..417ec46652 100644 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -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 { + 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, - notify?: (tx: Tx) => void + connect: (txHandler: TxHander) => Promise ): Promise { 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) } } diff --git a/packages/presentation/src/utils.ts b/packages/presentation/src/utils.ts index c06dcf7feb..af4895cf7e 100644 --- a/packages/presentation/src/utils.ts +++ b/packages/presentation/src/utils.ts @@ -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 { + return Promise.all([super.tx(tx), this.liveQuery.tx(tx)]) as unknown as Promise + } } 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 { diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 1249c37db9..fd9d6d1e84 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -108,6 +108,7 @@ export class LiveQuery extends TxProcessor implements Client { } protected async txCreateDoc (tx: TxCreateDoc): Promise { + 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 { - await this.client.tx(tx) + // await this.client.tx(tx) await super.tx(tx) } diff --git a/plugins/client-resources/src/connection.ts b/plugins/client-resources/src/connection.ts index 9c5b0fb415..514d3ccd77 100644 --- a/plugins/client-resources/src/connection.ts +++ b/plugins/client-resources/src/connection.ts @@ -31,12 +31,10 @@ class DeferredPromise { } class Connection implements Storage { - private readonly webSocket: WebSocket private readonly requests = new Map() 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 { - 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')) + }) } diff --git a/server/ws/src/server.ts b/server/ws/src/server.ts index 104c82fd3c..d133c8b0b2 100644 --- a/server/ws/src/server.ts +++ b/server/ws/src/server.ts @@ -29,11 +29,9 @@ export interface _Token { async function handleRequest (service: S, ws: WebSocket, msg: string): Promise { 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) }