Skip to content

Commit

Permalink
feat(resources): support unload resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Apr 12, 2024
1 parent c595319 commit 64d4561
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ export class ResourceManagerService extends Disposable implements IResourceManag
if (data) {
try {
const model = hook.parseJson(data);
hook.onChange(unitId, model);
hook.onLoad(unitId, model);
} catch (err) {
console.error('LoadResources Error!');
}
}
});
}

public unloadResources(unitId: string) {
this.getAllResourceHooks().forEach((hook) => {
hook.onUnLoad(unitId);
});
}

override dispose(): void {
this._register$.complete();
this._resourceMap.clear();
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/services/resource-manager/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export type IResourceName = `${IBusinessName}_${string}_PLUGIN`;
export interface IResourceHook<T = any> {
pluginName: IResourceName;
businesses: IBusinessName[];
onChange: (unitID: string, resource: T) => void;
onLoad: (unitID: string, resource: T) => void;
onUnLoad: (unitID: string) => void;
toJson: (unitID: string) => string;
parseJson: (bytes: string) => T;
}
Expand All @@ -38,6 +39,8 @@ export interface IResourceManagerService {
getAllResourceHooks: () => IResourceHook[];
getResources: (unitId: string) => IWorkbookData['resources'];
loadResources: (unitId: string, resources: IWorkbookData['resources']) => void;

unloadResources(unitId: string): void;
}

export const IResourceManagerService = createIdentifier<IResourceManagerService>('resource-manager-service');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ISheetDataValidationRule } from '@univerjs/core';
import { Disposable, IResourceManagerService, IUniverInstanceService, LifecycleStages, OnLifecycle, toDisposable } from '@univerjs/core';
import { Disposable, IResourceManagerService, IUniverInstanceService, LifecycleStages, OnLifecycle } from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
import { DataValidationModel } from '../models/data-validation-model';

Expand Down Expand Up @@ -62,7 +62,10 @@ export class DataValidationResourceController extends Disposable {
businesses: ['SHEET'],
toJson: (unitID) => toJson(unitID),
parseJson: (json) => parseJson(json),
onChange: (unitID, value) => {
onUnLoad: (unitID) => {
this._dataValidationModel.deleteUnitRules(unitID);
},
onLoad: (unitID, value) => {
Object.keys(value).forEach((subunitId) => {
const ruleList = value[subunitId];
ruleList.forEach((rule) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/data-validation/src/models/data-validation-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ export class DataValidationModel<T extends IDataValidationRule = IDataValidation

return res;
}

deleteUnitRules(unitId: string) {
this._model.delete(unitId);
}
}
21 changes: 15 additions & 6 deletions packages/engine-formula/src/services/defined-names.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface IDefinedNamesService {

removeDefinedName(unitId: string, name: string): void;

removeUnitDefinedName(unitId: string): void;

hasDefinedName(unitId: string): boolean;

setCurrentRange(range: IUnitRange): void;
Expand All @@ -80,12 +82,14 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ
private readonly _update$ = new Subject();
readonly update$ = this._update$.asObservable();

private _currentRange: IUnitRange = { unitId: '', sheetId: '', range: {
startRow: 0,
endRow: 0,
startColumn: 0,
endColumn: 0,
} };
private _currentRange: IUnitRange = {
unitId: '', sheetId: '', range: {
startRow: 0,
endRow: 0,
startColumn: 0,
endColumn: 0,
},
};

private readonly _currentRange$ = new Subject<IUnitRange>();
readonly currentRange$ = this._currentRange$.asObservable();
Expand Down Expand Up @@ -144,6 +148,11 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ
this._update();
}

removeUnitDefinedName(unitId: string) {
delete this._definedNameMap[unitId];
this._update();
}

getDefinedNameMap(unitId: string) {
return this._definedNameMap[unitId];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,8 @@ export class ConditionalFormattingRuleModel {
createCfId(_unitId: string, _subUnitId: string) {
return createCfId();
}

deleteUnitId(unitId: string) {
this._model.delete(unitId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ export class ConditionalFormattingService extends Disposable {
businesses: ['SHEET'],
toJson: (unitID) => toJson(unitID),
parseJson: (json) => parseJson(json),
onChange: (unitID, value) => {
onUnLoad: (unitID) => {
this._conditionalFormattingRuleModel.deleteUnitId(unitID);
},
onLoad: (unitID, value) => {
Object.keys(value).forEach((subunitId) => {
const ruleList = value[subunitId];
ruleList.forEach((rule) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export class DefinedNameDataController extends Disposable {
businesses: ['SHEET'],
toJson: (unitID) => toJson(unitID),
parseJson: (json) => parseJson(json),
onChange: (unitID, value) => {
onUnLoad: (unitID) => {
this._definedNamesService.removeUnitDefinedName(unitID);
},
onLoad: (unitID, value) => {
this._definedNamesService.registerDefinedNames(unitID, value);
},
})
Expand Down
21 changes: 5 additions & 16 deletions packages/sheets/src/services/numfmt/numfmt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ export class NumfmtService extends Disposable implements INumfmtService {
businesses: ['SHEET'],
toJson: (unitID) => this._toJson(unitID),
parseJson: (json) => this._parseJson(json),
onChange: (unitID, value) => {
onUnLoad: (unitID) => {
this._numfmtModel.delete(unitID);
this._refAliasModel.delete(unitID);
},
onLoad: (unitID, value) => {
const { model, refModel } = value;
if (model) {
const parseModel = Object.keys(model).reduce((result, sheetId) => {
Expand All @@ -90,21 +94,6 @@ export class NumfmtService extends Disposable implements INumfmtService {
},
})
);
this.disposeWithMe(
toDisposable(
this._univerInstanceService.sheetDisposed$.subscribe((workbook) => {
const unitID = workbook.getUnitId();
const model = this._numfmtModel.get(unitID);
if (model) {
this._numfmtModel.delete(unitID);
}
const refModel = this._refAliasModel.get(unitID);
if (refModel) {
this._refAliasModel.delete(unitID);
}
})
)
);
}

private _toJson(unitID: string) {
Expand Down

0 comments on commit 64d4561

Please sign in to comment.