From 0fec7a9fa4f8952e9f133d7c905ac2481556c639 Mon Sep 17 00:00:00 2001 From: zhangw Date: Mon, 21 Oct 2024 11:13:51 +0800 Subject: [PATCH] feat(thread-comment): sync comment after rendered (#3810) --- .../src/models/thread-comment.model.ts | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/thread-comment/src/models/thread-comment.model.ts b/packages/thread-comment/src/models/thread-comment.model.ts index 83f1c5302d9..a3ad205145e 100644 --- a/packages/thread-comment/src/models/thread-comment.model.ts +++ b/packages/thread-comment/src/models/thread-comment.model.ts @@ -14,11 +14,11 @@ * limitations under the License. */ -import { CustomRangeType, Disposable, ICommandService, Inject } from '@univerjs/core'; -import { BehaviorSubject, map, Subject } from 'rxjs'; -import { IThreadCommentDataSourceService } from '../services/tc-datasource.service'; import type { IUpdateCommentPayload, IUpdateCommentRefPayload } from '../commands/mutations/comment.mutation'; import type { IBaseComment, IThreadComment } from '../types/interfaces/i-thread-comment'; +import { CustomRangeType, Disposable, ICommandService, Inject, LifecycleService, LifecycleStages } from '@univerjs/core'; +import { BehaviorSubject, map, Subject } from 'rxjs'; +import { IThreadCommentDataSourceService } from '../services/tc-datasource.service'; export type CommentUpdate = { unitId: string; @@ -63,9 +63,12 @@ export class ThreadCommentModel extends Disposable { commentUpdate$ = this._commentUpdate$.asObservable(); commentMap$ = this._commentsMap$.asObservable(); + private _tasks: { unitId: string; subUnitId: string; threadIds: string[] }[] = []; + constructor( @Inject(IThreadCommentDataSourceService) private readonly _dataSourceService: IThreadCommentDataSourceService, - @ICommandService private readonly _commandService: ICommandService + @ICommandService private readonly _commandService: ICommandService, + @Inject(LifecycleService) private readonly _lifecycleService: LifecycleService ) { super(); @@ -73,6 +76,35 @@ export class ThreadCommentModel extends Disposable { this._commentUpdate$.complete(); this._commentsMap$.complete(); }); + + this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((stage) => { + const taskMap = new Map>>(); + + if (stage === LifecycleStages.Rendered) { + this._tasks.forEach(({ unitId, subUnitId, threadIds }) => { + let unitMap = taskMap.get(unitId); + if (!unitMap) { + unitMap = new Map(); + taskMap.set(unitId, unitMap); + } + let subUnitMap = unitMap.get(subUnitId); + if (!subUnitMap) { + subUnitMap = new Set(); + unitMap.set(subUnitId, subUnitMap); + } + for (const threadId of threadIds) { + subUnitMap.add(threadId); + } + }); + + this._tasks = []; + taskMap.forEach((subUnitMap, unitId) => { + subUnitMap.forEach((threadIds, subUnitId) => { + this.syncThreadComments(unitId, subUnitId, Array.from(threadIds)); + }); + }); + } + })); } private _ensureCommentMap(unitId: string, subUnitId: string) { @@ -175,6 +207,11 @@ export class ThreadCommentModel extends Disposable { } async syncThreadComments(unitId: string, subUnitId: string, threadIds: string[]) { + if (this._lifecycleService.stage < LifecycleStages.Rendered) { + this._tasks.push({ unitId, subUnitId, threadIds }); + return; + } + const comments = await this._dataSourceService.listThreadComments(unitId, subUnitId, threadIds); if (!comments.length) { return;