From e4db603873a23cb148d23c3ad9b6c43825a91a1b Mon Sep 17 00:00:00 2001 From: Wenzhao Hu <12122021+wzhudev@users.noreply.github.com> Date: Thu, 8 Aug 2024 10:38:16 +0800 Subject: [PATCH] fix(core): fix resource manager get unrelated data --- .../resource-loader.service.ts | 22 ++++++++----------- .../resource-manager.service.ts | 12 +++++++--- .../src/services/resource-manager/type.ts | 13 +++++++---- .../src/types/interfaces/i-workbook-data.ts | 4 +++- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/core/src/services/resource-loader/resource-loader.service.ts b/packages/core/src/services/resource-loader/resource-loader.service.ts index dd9174ddc99..25aa7bd5b72 100644 --- a/packages/core/src/services/resource-loader/resource-loader.service.ts +++ b/packages/core/src/services/resource-loader/resource-loader.service.ts @@ -20,7 +20,7 @@ import type { IWorkbookData } from '../../types/interfaces'; import type { IResourceHook } from '../resource-manager/type'; import { IResourceManagerService } from '../resource-manager/type'; import { IUniverInstanceService } from '../instance/instance.service'; -import { Disposable, toDisposable } from '../../shared/lifecycle'; +import { Disposable } from '../../shared/lifecycle'; import { UniverInstanceType } from '../../common/unit'; import type { DocumentDataModel } from '../../docs'; import type { IResourceLoaderService } from './type'; @@ -84,25 +84,21 @@ export class ResourceLoaderService extends Disposable implements IResourceLoader })); this.disposeWithMe( - toDisposable( - this._univerInstanceService.getTypeOfUnitAdded$(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => { - this._resourceManagerService.loadResources(workbook.getUnitId(), workbook.getSnapshot().resources); - }) - ) + this._univerInstanceService.getTypeOfUnitAdded$(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => { + this._resourceManagerService.loadResources(workbook.getUnitId(), workbook.getSnapshot().resources); + }) ); this.disposeWithMe( - toDisposable( - this._univerInstanceService.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => { - this._resourceManagerService.unloadResources(workbook.getUnitId()); - }) - ) + this._univerInstanceService.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => { + this._resourceManagerService.unloadResources(workbook.getUnitId()); + }) ); } saveWorkbook: (workbook: Workbook) => IWorkbookData = (workbook) => { const unitId = workbook.getUnitId(); - const resources = this._resourceManagerService.getResources(unitId) || []; + const resources = this._resourceManagerService.getResources(unitId, UniverInstanceType.UNIVER_SHEET); const snapshot = workbook.getSnapshot(); snapshot.resources = resources; return snapshot; @@ -110,7 +106,7 @@ export class ResourceLoaderService extends Disposable implements IResourceLoader saveDoc(doc: DocumentDataModel) { const unitId = doc.getUnitId(); - const resources = this._resourceManagerService.getResourcesByType(unitId, UniverInstanceType.UNIVER_DOC) || []; + const resources = this._resourceManagerService.getResources(unitId, UniverInstanceType.UNIVER_DOC); const snapshot = doc.getSnapshot(); snapshot.resources = resources; return snapshot; diff --git a/packages/core/src/services/resource-manager/resource-manager.service.ts b/packages/core/src/services/resource-manager/resource-manager.service.ts index 0ab09b773a5..ff00ed4612e 100644 --- a/packages/core/src/services/resource-manager/resource-manager.service.ts +++ b/packages/core/src/services/resource-manager/resource-manager.service.ts @@ -16,7 +16,7 @@ import { Subject } from 'rxjs'; import { Disposable, toDisposable } from '../../shared/lifecycle'; -import type { IWorkbookData } from '../../types/interfaces/i-workbook-data'; +import type { Resources } from '../../types/interfaces/i-workbook-data'; import type { UniverInstanceType } from '../../common/unit'; import type { IResourceHook, IResourceManagerService, IResourceName } from './type'; @@ -31,7 +31,13 @@ export class ResourceManagerService extends Disposable implements IResourceManag return list; } - public getResources(unitId: string) { + public getResources(unitId: string): Resources; + public getResources(unitId: string, type: UniverInstanceType): Resources; + public getResources(unitId: string, type?: UniverInstanceType): Resources { + if (type) { + return this.getResourcesByType(unitId, type); + } + const resourceHooks = this.getAllResourceHooks(); const resources = resourceHooks.map((resourceHook) => { const data = resourceHook.toJson(unitId); @@ -69,7 +75,7 @@ export class ResourceManagerService extends Disposable implements IResourceManag this._resourceMap.delete(pluginName); } - public loadResources(unitId: string, resources: IWorkbookData['resources']) { + public loadResources(unitId: string, resources?: Resources) { this.getAllResourceHooks().forEach((hook) => { const data = resources?.find((resource) => resource.name === hook.pluginName)?.data; if (data) { diff --git a/packages/core/src/services/resource-manager/type.ts b/packages/core/src/services/resource-manager/type.ts index dbcf85ba431..b645bd28c23 100644 --- a/packages/core/src/services/resource-manager/type.ts +++ b/packages/core/src/services/resource-manager/type.ts @@ -18,7 +18,7 @@ import type { Observable } from 'rxjs'; import type { UniverInstanceType } from '@univerjs/core'; import type { IDisposable } from '../../common/di'; import { createIdentifier } from '../../common/di'; -import type { IWorkbookData } from '../../types/interfaces/i-workbook-data'; +import type { Resources } from '../../types/interfaces/i-workbook-data'; type IBusinessName = 'SHEET' | 'DOC'; export type IResourceName = `${IBusinessName}_${string}_PLUGIN`; @@ -36,9 +36,14 @@ export interface IResourceManagerService { registerPluginResource: (hook: IResourceHook) => IDisposable; disposePluginResource: (pluginName: IResourceName) => void; getAllResourceHooks: () => IResourceHook[]; - getResources: (unitId: string) => IWorkbookData['resources']; - getResourcesByType: (unitId: string, type: UniverInstanceType) => IWorkbookData['resources']; - loadResources: (unitId: string, resources: IWorkbookData['resources']) => void; + + /** + * @deprecated You should get resource with type specified. + */ + getResources(unitId: string): Resources; + getResources(unitId: string, type: UniverInstanceType): Resources; + getResourcesByType: (unitId: string, type: UniverInstanceType) => Resources; + loadResources: (unitId: string, resources?: Resources) => void; unloadResources(unitId: string): void; } diff --git a/packages/core/src/types/interfaces/i-workbook-data.ts b/packages/core/src/types/interfaces/i-workbook-data.ts index 92758099da1..dc2421ab084 100644 --- a/packages/core/src/types/interfaces/i-workbook-data.ts +++ b/packages/core/src/types/interfaces/i-workbook-data.ts @@ -20,6 +20,8 @@ import type { IExtraModelData } from './i-extra-model-data'; import type { IStyleData } from './i-style-data'; import type { IWorksheetData } from './i-worksheet-data'; +export type Resources = Array<{ id?: string; name: string; data: string }>; + /** * Properties of a workbook's configuration */ @@ -53,5 +55,5 @@ export interface IWorkbookData extends IExtraModelData { sheets: { [sheetId: string]: Partial }; // The type of data depends on how the plug-in is defined - resources?: Array<{ id?: string; name: string; data: string }>; + resources?: Resources; }