Skip to content

Commit

Permalink
feat(user): add user model (#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound authored Apr 30, 2024
1 parent ad30012 commit 49c1a70
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 sheet

Expand Down
49 changes: 49 additions & 0 deletions packages/core/src/services/user-manager/user-manager.service.ts
Original file line number Diff line number Diff line change
@@ -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' });
}
}
2 changes: 2 additions & 0 deletions packages/core/src/univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down Expand Up @@ -170,6 +171,7 @@ function createUniverInjector(parentInjector?: Injector, override?: DependencyOv
[LifecycleInitializerService],
[UniverPermissionService],
[PluginService],
[UserManagerService],

// abstract services
[IUniverInstanceService, { useClass: UniverInstanceService }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 49c1a70

Please sign in to comment.