Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(user): add user model (#2137)
Gpound.liu49c1a70bb101
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 7335aec1a8..8dbf74e7df 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -114,6 +114,7 @@ export {} from './services/undoredo/undoredo.service';export * from './shared';export { fromCallback } from './shared/rxjs';+export { UserManagerService } from './services/user-manager/user-manager.service';// #region sheetdiff --git a/packages/core/src/services/user-manager/user-manager.service.ts b/packages/core/src/services/user-manager/user-manager.service.tsnew file mode 100644index 0000000000..a8d132a8a7--- /dev/null+++ b/packages/core/src/services/user-manager/user-manager.service.ts@@ -0,0 +1,49 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IUser } from '@univerjs/protocol';+import { Subject } from 'rxjs';+++export class UserManagerService {+ private _model = new Map<string, IUser>();+ private _userChange$ = new Subject<{ type: 'add' | 'delete'; user: IUser } | { type: 'clear' }>();+ public userChange$ = this._userChange$.asObservable();++ addUser(user: IUser) {+ this._model.set(user.userID, user);+ this._userChange$.next({ type: 'add', user });+ }++ getUser(userId: string, callBack?: () => void) {+ const user = this._model.get(userId);+ if (user) {+ return user;+ }+ callBack && callBack();+ }++ delete(userId: string) {+ const user = this.getUser(userId);+ this._model.delete(userId);+ user && this._userChange$.next({ type: 'delete', user });+ }++ clear() {+ this._model.clear();+ this._userChange$.next({ type: 'clear' });+ }+}diff --git a/packages/core/src/univer.ts b/packages/core/src/univer.tsindex a9ce1e2b46..cac072aecc 100644--- a/packages/core/src/univer.ts+++ b/packages/core/src/univer.ts@@ -49,6 +49,7 @@ import { PluginService } from './services/plugin/plugin.service';import type { Plugin, PluginCtor } from './services/plugin/plugin';import type { DependencyOverride } from './services/plugin/plugin-override';import { mergeOverrideWithDependencies } from './services/plugin/plugin-override';+import { UserManagerService } from './services/user-manager/user-manager.service';export class Univer {private _startedTypes = new Set<UnitType>();@@ -170,6 +171,7 @@ function createUniverInjector(parentInjector?: Injector, override?: DependencyOv[LifecycleInitializerService],[UniverPermissionService],[PluginService],+ [UserManagerService],// abstract services[IUniverInstanceService, { useClass: UniverInstanceService }],diff --git a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.tsindex 63e7c0ff75..4c3adcc08a 100644--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts@@ -42,9 +42,8 @@ interface IComputeCache { status: ComputeStatus };const beforeUpdateRuleResult = createInterceptorKey<{ subUnitId: string; unitId: string; cfId: string }, undefined>('conditional-formatting-before-update-rule-result');@OnLifecycle(LifecycleStages.Starting, ConditionalFormattingService)export class ConditionalFormattingService extends Disposable {- // <unitId,<subUnitId,<cfId,IComputeCache>>>-private _afterInitApplyPromise: Promise<void>;+ // <unitId,<subUnitId,<cfId,IComputeCache>>>private _ruleCacheMap: Map<string, Map<string, Map<string, IComputeCache>>> = new Map();private _ruleComputeStatus$: Subject<{ status: ComputeStatus; result?: ObjectMatrix<any>; unitId: string; subUnitId: string; cfId: string }> = new Subject();@@ -372,19 +371,22 @@ export class ConditionalFormattingService extends Disposable {result.forValue((row, col, value) => {this._conditionalFormattingViewModel.setCellCfRuleCache(unitId, subUnitId, row, col, cfId, value);});- this._deleteComputeCache(unitId, subUnitId, cfId);}}));}private async _handleCalculateUnit(unitId: string, subUnitId: string, rule: IConditionFormattingRule) {+ await this._afterInitApplyPromise;+ // We need to perform a secondary verification, as the rule may have been deleted after initApply.+ if (!this._conditionalFormattingRuleModel.getRule(unitId, subUnitId, rule.cfId)) {+ return;+ }const workbook = this._univerInstanceService.getUnit<Workbook>(unitId);const worksheet = workbook?.getSheetBySheetId(subUnitId);let cache = this._getComputedCache(unitId, subUnitId, rule.cfId);- if (cache && cache.status === 'computing') {+ if (cache && ['computing', 'end'].includes(cache.status)) {return;}- await this._afterInitApplyPromise;if (!cache) {cache = { status: 'computing' };this._setComputedCache(unitId, subUnitId, rule.cfId, cache);