|
15 | 15 |
|
16 | 16 | import { Analytics } from '@hcengineering/analytics' |
17 | 17 | import { BackupClient, DocChunk } from './backup' |
18 | | -import { Class, DOMAIN_MODEL, Doc, Domain, Ref, Timestamp } from './classes' |
| 18 | +import { Class, DOMAIN_MODEL, Doc, Domain, Ref, Timestamp, type Account } from './classes' |
19 | 19 | import core from './component' |
20 | 20 | import { Hierarchy } from './hierarchy' |
21 | 21 | import { MeasureContext, MeasureMetricsContext } from './measurements' |
22 | 22 | import { ModelDb } from './memdb' |
23 | 23 | import type { DocumentQuery, FindOptions, FindResult, FulltextStorage, Storage, TxResult, WithLookup } from './storage' |
24 | 24 | import { SearchOptions, SearchQuery, SearchResult } from './storage' |
25 | 25 | import { Tx, TxCUD, WorkspaceEvent, type TxWorkspaceEvent } from './tx' |
26 | | -import { platformNow, platformNowDiff, toFindResult } from './utils' |
| 26 | +import { platformNow, platformNowDiff, toFindResult, type WorkspaceUuid } from './utils' |
| 27 | +import { deepEqual } from 'fast-equals' |
27 | 28 |
|
28 | 29 | /** |
29 | 30 | * @public |
@@ -80,13 +81,15 @@ export interface ClientConnection extends Storage, FulltextStorage, BackupClient |
80 | 81 | isConnected: () => boolean |
81 | 82 |
|
82 | 83 | close: () => Promise<void> |
83 | | - onConnect?: (event: ClientConnectEvent, lastTx: string | undefined, data: any) => Promise<void> |
| 84 | + onConnect?: (event: ClientConnectEvent, lastTx: Record<WorkspaceUuid, string | undefined> | undefined, data: any) => Promise<void> |
84 | 85 |
|
85 | 86 | // If hash is passed, will return LoadModelResponse |
86 | 87 | loadModel: (last: Timestamp, hash?: string) => Promise<Tx[] | LoadModelResponse> |
87 | 88 |
|
88 | | - getLastHash?: (ctx: MeasureContext) => Promise<string | undefined> |
| 89 | + getLastHash?: (ctx: MeasureContext) => Promise<Record<WorkspaceUuid, string | undefined>> |
89 | 90 | pushHandler: (handler: Handler) => void |
| 91 | + |
| 92 | + getAccount: () => Promise<Account> |
90 | 93 | } |
91 | 94 |
|
92 | 95 | class ClientImpl implements Client, BackupClient { |
@@ -237,7 +240,7 @@ export async function createClient ( |
237 | 240 | let hierarchy = new Hierarchy() |
238 | 241 | let model = new ModelDb(hierarchy) |
239 | 242 |
|
240 | | - let lastTx: string | undefined |
| 243 | + let lastTx: Record<WorkspaceUuid, string | undefined> | undefined |
241 | 244 |
|
242 | 245 | function txHandler (...tx: Tx[]): void { |
243 | 246 | if (tx == null || tx.length === 0) { |
@@ -282,7 +285,7 @@ export async function createClient ( |
282 | 285 | txBuffer = undefined |
283 | 286 |
|
284 | 287 | const oldOnConnect: |
285 | | - | ((event: ClientConnectEvent, lastTx: string | undefined, data: any) => Promise<void>) |
| 288 | + | ((event: ClientConnectEvent, lastTx: Record<WorkspaceUuid, string | undefined> | undefined, data: any) => Promise<void>) |
286 | 289 | | undefined = conn.onConnect |
287 | 290 | conn.onConnect = async (event, _lastTx, data) => { |
288 | 291 | console.log('Client: onConnect', event) |
@@ -324,7 +327,7 @@ export async function createClient ( |
324 | 327 | return |
325 | 328 | } |
326 | 329 |
|
327 | | - if (lastTx === _lastTx) { |
| 330 | + if (deepEqual(lastTx, _lastTx)) { |
328 | 331 | // Same lastTx, no need to refresh |
329 | 332 | await oldOnConnect?.(ClientConnectEvent.Reconnected, _lastTx, data) |
330 | 333 | return |
@@ -362,9 +365,13 @@ async function loadModel ( |
362 | 365 | hash: '' |
363 | 366 | } |
364 | 367 |
|
365 | | - if (conn.getLastHash !== undefined && (await conn.getLastHash(ctx)) === current.hash) { |
366 | | - // We have same model hash. |
367 | | - return { mode: 'same', current: current.transactions, addition: [] } |
| 368 | + if (conn.getLastHash !== undefined) { |
| 369 | + const lastHash = await conn.getLastHash(ctx) |
| 370 | + const account = await conn.getAccount() |
| 371 | + if (lastHash[account.targetWorkspace] === current.hash) { |
| 372 | + // We have same model hash. |
| 373 | + return { mode: 'same', current: current.transactions, addition: [] } |
| 374 | + } |
368 | 375 | } |
369 | 376 | const lastTxTime = getLastTxTime(current.transactions) |
370 | 377 | const result = await ctx.with('connection-load-model', { hash: current.hash !== '' }, (ctx) => |
|
0 commit comments