Skip to content

Commit

Permalink
feat(thread-comment): sync comment after rendered (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 authored Oct 21, 2024
1 parent ebcf3e0 commit 0fec7a9
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions packages/thread-comment/src/models/thread-comment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,16 +63,48 @@ 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();

this.disposeWithMe(() => {
this._commentUpdate$.complete();
this._commentsMap$.complete();
});

this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((stage) => {
const taskMap = new Map<string, Map<string, Set<string>>>();

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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0fec7a9

Please sign in to comment.