Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make the source of univer-instance-type unique #2073

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const SaveSnapshotOptions: ICommand = {
const exportController = accessor.get(ExportController);
const recordController = accessor.get(RecordController);

const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.SHEET)!;
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const worksheet = workbook.getActiveSheet();
const snapshot = resourceLoaderService.saveWorkbook(workbook);
const gitHash = process.env.GIT_COMMIT_HASH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const SetEditable: ICommand = {
} else {
const univerPermissionService = accessor.get(UniverPermissionService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const unitId = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.SHEET)!.getUnitId();
const unitId = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId();
const editable = univerPermissionService.getEditable(unitId);
univerPermissionService.setEditable(unitId, !editable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const SidebarOperation: ICommand = {
const sidebarService = accessor.get(ISidebarService);
const editorService = accessor.get(IEditorService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const unit = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.SHEET)!;
const unit = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
switch (params.value) {
case 'open':
editorService.setOperationSheetUnitId(unit.getUnitId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class E2EMemoryController {

private async _loadAndRelease(id: number): Promise<void> {
const unitId = `e2e${id}`;
this._univerInstanceService.createUnit(UniverInstanceType.SHEET, { ...DEFAULT_WORKBOOK_DATA_DEMO, id: unitId });
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, { ...DEFAULT_WORKBOOK_DATA_DEMO, id: unitId });
await timer(AWAIT_LOADING_TIMEOUT);
this._univerInstanceService.disposeUnit(unitId);
await timer(AWAIT_DISPOSING_TIMEOUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { interval, takeUntil, throttle } from 'rxjs';

@OnLifecycle(LifecycleStages.Rendered, PerformanceMonitorController)
export class PerformanceMonitorController extends RxDisposable {
private _documentType: UniverInstanceType = UniverInstanceType.UNIVER;
private _documentType: UniverInstanceType = UniverInstanceType.UNIVER_UNKNOWN;
private _hasWatched = false;
private _container!: HTMLDivElement;
private _styleElement!: HTMLStyleElement;
Expand Down Expand Up @@ -87,14 +87,14 @@ export class PerformanceMonitorController extends RxDisposable {
this._styleElement = document.createElement('style');
document.head.appendChild(this._styleElement).innerText = style;

if (this._documentType === UniverInstanceType.DOC) {
if (this._documentType === UniverInstanceType.UNIVER_DOC) {
this._docCanvasView.fps$
.pipe(takeUntil(this.dispose$))
.pipe(throttle(() => interval(THROTTLE_TIME)))
.subscribe((fps) => {
container.innerText = `FPS: ${fps}`;
});
} else if (this._documentType === UniverInstanceType.SHEET) {
} else if (this._documentType === UniverInstanceType.UNIVER_SHEET) {
this._injector
.get(SheetCanvasView)
.fps$.pipe(takeUntil(this.dispose$))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const editorStyle: React.CSSProperties = {
*/
export const TestEditorContainer = () => {
const univerInstanceService = useDependency(IUniverInstanceService);
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.SHEET)!;
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
if (workbook == null) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ univer.registerPlugin(UniverSheetsConditionalFormattingUIPlugin);

// create univer sheet instance
if (!IS_E2E) {
univer.createUnit(UniverInstanceType.SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
}

// Uncomment the following lines to test if the document is disposed correctly without memory leaks.
// setTimeout(() => {
// univer.__getInjector().get(IUniverInstanceService).disposeUnit(DEFAULT_WORKBOOK_DATA_DEMO.id);
// }, 5000);
// setTimeout(() => {
// univer.createUnit(UniverInstanceType.SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
// univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
// }, 7000);


Expand Down
15 changes: 3 additions & 12 deletions packages/core/src/common/unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@
* limitations under the License.
*/

import type { UniverType } from '@univerjs/protocol';
import { Disposable } from '../shared';

/**
* Type of built-in univer document instances.
*/
export enum UniverInstanceType {
UNIVER = 0,
DOC = 1,
SHEET = 2,
SLIDE = 3,

UNRECOGNIZED = -1,
}
export { UniverType as UniverInstanceType } from '@univerjs/protocol';

export type UnitType = UniverInstanceType | number;
export type UnitType = UniverType | number;

export abstract class UnitModel<_D = object, T extends UnitType = UnitType> extends Disposable {
abstract readonly type: T;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/docs/data-model/document-data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ interface IDrawingUpdateConfig {
width: number;
}

class DocumentDataModelSimple extends UnitModel<IDocumentData, UniverInstanceType.DOC> {
override type: UniverInstanceType.DOC = UniverInstanceType.DOC;
class DocumentDataModelSimple extends UnitModel<IDocumentData, UniverInstanceType.UNIVER_DOC> {
override type: UniverInstanceType.UNIVER_DOC = UniverInstanceType.UNIVER_DOC;

override getUnitId(): string {
throw new Error('Method not implemented.');
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/services/instance/instance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,23 @@ export class UniverInstanceService extends Disposable implements IUniverInstance
}

getCurrentUniverDocInstance(): Nullable<DocumentDataModel> {
return this.getCurrentUnitForType(UniverInstanceType.DOC) as Nullable<DocumentDataModel>;
return this.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC) as Nullable<DocumentDataModel>;
}

getUniverDocInstance(unitId: string): Nullable<DocumentDataModel> {
return this.getUnit<DocumentDataModel>(unitId, UniverInstanceType.DOC);
return this.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);
}

getUniverSheetInstance(unitId: string): Nullable<Workbook> {
return this.getUnit<Workbook>(unitId, UniverInstanceType.SHEET);
return this.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
}

getAllUnitsForType<T>(type: UnitType): T[] {
return (this._unitsByType.get(type) ?? []) as T[];
}

changeDoc(unitId: string, doc: DocumentDataModel): void {
const allDocs = this.getAllUnitsForType<DocumentDataModel>(UniverInstanceType.DOC);
const allDocs = this.getAllUnitsForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
const oldDoc = allDocs.find((doc) => doc.getUnitId() === unitId);

if (oldDoc != null) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services/permission/permission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class PermissionService extends Disposable implements IPermissionService
// private _init() {
// this.disposeWithMe(
// toDisposable(
// this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.SHEET).subscribe((workbook) => {
// this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
// this._resourceManagerService.registerPluginResource(workbook.getUnitId(), resourceKey, {
// onChange: (unitID, value) => {
// (value as PermissionPoint[]).forEach((permissionPoint) => {
Expand All @@ -72,7 +72,7 @@ export class PermissionService extends Disposable implements IPermissionService
// );
// this.disposeWithMe(
// toDisposable(
// this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.SHEET).subscribe((workbook) => {
// this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
// this._resourceManagerService.disposePluginResource(workbook.getUnitId(), resourceKey);
// })
// )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export class UniverPermissionService extends Disposable {
}

private _init() {
this._univerInstanceService.getTypeOfUnitAdded$(UniverInstanceType.SHEET).subscribe((workbook) => {
this._univerInstanceService.getTypeOfUnitAdded$(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
const univerEditablePermission = new UniverEditablePermission(workbook.getUnitId());
this._permissionService.addPermissionPoint(workbook.getUnitId(), univerEditablePermission);
});
}

getEditable(unitId = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.SHEET)?.getUnitId()) {
getEditable(unitId = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET)?.getUnitId()) {
if (!unitId) {
return;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/services/plugin/plugin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import type { IDisposable } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
import type { UniverType } from '@univerjs/protocol';
import { type UnitType, UniverInstanceType } from '../../common/unit';
import { PluginHolder } from './plugin-holder';
import type { Plugin, PluginCtor } from './plugin';
Expand Down Expand Up @@ -54,7 +53,7 @@ export class PluginService implements IDisposable {
this._scheduleInitPlugin();

const { type } = plugin;
if (type === UniverInstanceType.UNIVER) {
if (type === UniverInstanceType.UNIVER_UNKNOWN) {
this._pluginHolderForUniver.registerPlugin(plugin, config);
this._pluginHolderForUniver.flush();
} else {
Expand All @@ -64,7 +63,7 @@ export class PluginService implements IDisposable {
}
}

startPluginForType(type: UniverType): void {
startPluginForType(type: UniverInstanceType): void {
const holder = this._ensurePluginHolderForType(type);
holder.start();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type PluginCtor<T extends Plugin> = Ctor<T> & { type: UniverInstanceType;
export abstract class Plugin extends Disposable {
static pluginName: string;

static type: UniverInstanceType = UniverInstanceType.UNIVER;
static type: UniverInstanceType = UniverInstanceType.UNIVER_UNKNOWN;

protected abstract _injector: Injector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import { Inject } from '@wendellhu/redi';
import { UniverType } from '@univerjs/protocol';
import type { Workbook } from '../../sheets/workbook';
import type { IWorkbookData } from '../../types/interfaces';
import type { IResourceHook } from '../resource-manager/type';
Expand All @@ -39,15 +38,15 @@ export class ResourceLoaderService extends Disposable implements IResourceLoader
const handleHookAdd = (hook: IResourceHook) => {
hook.businesses.forEach((business) => {
switch (business) {
case UniverType.UNRECOGNIZED:
case UniverType.UNIVER_UNKNOWN:
case UniverType.UNIVER_SLIDE:
case UniverType.UNIVER_DOC: {
case UniverInstanceType.UNRECOGNIZED:
case UniverInstanceType.UNIVER_UNKNOWN:
case UniverInstanceType.UNIVER_SLIDE:
case UniverInstanceType.UNIVER_DOC: {
// TODO@gggpound: wait to support.
break;
}
case UniverType.UNIVER_SHEET: {
this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.SHEET).forEach((workbook) => {
case UniverInstanceType.UNIVER_SHEET: {
this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
const snapshotResource = workbook.getSnapshot().resources || [];
const plugin = snapshotResource.find((r) => r.name === hook.pluginName);
if (plugin) {
Expand Down Expand Up @@ -75,15 +74,15 @@ export class ResourceLoaderService extends Disposable implements IResourceLoader

this.disposeWithMe(
toDisposable(
this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.SHEET).subscribe((workbook) => {
this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._resourceManagerService.loadResources(workbook.getUnitId(), workbook.getSnapshot().resources);
})
)
);

this.disposeWithMe(
toDisposable(
this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.SHEET).subscribe((workbook) => {
this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._resourceManagerService.unloadResources(workbook.getUnitId());
})
)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services/resource-manager/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import type { IDisposable } from '@wendellhu/redi';
import { createIdentifier } from '@wendellhu/redi';
import type { Observable } from 'rxjs';
import type { UniverType } from '@univerjs/protocol';
import type { UniverInstanceType } from '@univerjs/core';
import type { IWorkbookData } from '../../types/interfaces/i-workbook-data';

type IBusinessName = 'SHEET' | 'DOC';
export type IResourceName = `${IBusinessName}_${string}_PLUGIN`;
export interface IResourceHook<T = any> {
pluginName: IResourceName;
businesses: UniverType[];
businesses: UniverInstanceType[];
onLoad: (unitID: string, resource: T) => void;
onUnLoad: (unitID: string) => void;
toJson: (unitID: string) => string;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/sheets/workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export function getWorksheetUID(workbook: Workbook, worksheet: Worksheet): strin
/**
* Access and create Univer Sheets files
*/
export class Workbook extends UnitModel<IWorkbookData, UniverInstanceType.SHEET> {
override type: UniverInstanceType.SHEET = UniverInstanceType.SHEET;
export class Workbook extends UnitModel<IWorkbookData, UniverInstanceType.UNIVER_SHEET> {
override type: UniverInstanceType.UNIVER_SHEET = UniverInstanceType.UNIVER_SHEET;

private readonly _sheetCreated$ = new Subject<Worksheet>();
readonly sheetCreated$ = this._sheetCreated$.asObservable();
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
* limitations under the License.
*/

import { CellValueType } from '@univerjs/protocol';

import type { Nullable } from '../shared';
import { ObjectMatrix, Rectangle, Tools } from '../shared';
import { createRowColIter } from '../shared/row-col-iter';
import type { BooleanNumber } from '../types/enum';
import { type BooleanNumber, CellValueType } from '../types/enum';
import type { ICellData, ICellDataForSheetInterceptor, IFreeze, IRange, IWorksheetData } from '../types/interfaces';
import { ColumnManager } from './column-manager';
import { Range } from './range';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/slides/slide-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { DEFAULT_SLIDE } from '../types/const';
import type { ISlideData, ISlidePage } from '../types/interfaces';
import { PageType } from '../types/interfaces';

export class SlideDataModel extends UnitModel<ISlideData, UniverInstanceType.SLIDE> {
override type: UniverInstanceType.SLIDE = UniverInstanceType.SLIDE;
export class SlideDataModel extends UnitModel<ISlideData, UniverInstanceType.UNIVER_SLIDE> {
override type: UniverInstanceType.UNIVER_SLIDE = UniverInstanceType.UNIVER_SLIDE;

private _snapshot: ISlideData;

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,27 @@ export class Univer {
* @deprecated use `createUnit` instead
*/
createUniverSheet(data: Partial<IWorkbookData>): Workbook {
return this._univerInstanceService.createUnit<IWorkbookData, Workbook>(UniverInstanceType.SHEET, data);
return this._univerInstanceService.createUnit<IWorkbookData, Workbook>(UniverInstanceType.UNIVER_SHEET, data);
}

/**
* @deprecated use `createUnit` instead
*/
createUniverDoc(data: Partial<IDocumentData>): DocumentDataModel {
return this._univerInstanceService.createUnit<IDocumentData, DocumentDataModel>(UniverInstanceType.DOC, data);
return this._univerInstanceService.createUnit<IDocumentData, DocumentDataModel>(UniverInstanceType.UNIVER_DOC, data);
}

/**
* @deprecated use `createUnit` instead
*/
createUniverSlide(data: Partial<ISlideData>): SlideDataModel {
return this._univerInstanceService.createUnit<ISlideData, SlideDataModel>(UniverInstanceType.SLIDE, data);
return this._univerInstanceService.createUnit<ISlideData, SlideDataModel>(UniverInstanceType.UNIVER_SLIDE, data);
}

private _init(injector: Injector): void {
this._univerInstanceService.registerCtorForType(UniverInstanceType.SHEET, Workbook);
this._univerInstanceService.registerCtorForType(UniverInstanceType.DOC, DocumentDataModel);
this._univerInstanceService.registerCtorForType(UniverInstanceType.SLIDE, SlideDataModel);
this._univerInstanceService.registerCtorForType(UniverInstanceType.UNIVER_SHEET, Workbook);
this._univerInstanceService.registerCtorForType(UniverInstanceType.UNIVER_DOC, DocumentDataModel);
this._univerInstanceService.registerCtorForType(UniverInstanceType.UNIVER_SLIDE, SlideDataModel);

const univerInstanceService = injector.get(IUniverInstanceService) as UniverInstanceService;
univerInstanceService.__setCreateHandler(
Expand Down
3 changes: 0 additions & 3 deletions packages/data-validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
"@wendellhu/redi": "^0.13.3",
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/protocol": "^0.1.20"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/shared": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/

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

type DataValidationJSON = Record<string, ISheetDataValidationRule[]>;
Expand Down Expand Up @@ -60,7 +59,7 @@ export class DataValidationResourceController extends Disposable {
this.disposeWithMe(
this._resourceManagerService.registerPluginResource<DataValidationJSON>({
pluginName: DATA_VALIDATION_PLUGIN_NAME,
businesses: [UniverType.UNIVER_SHEET],
businesses: [UniverInstanceType.UNIVER_SHEET],
toJson: (unitID) => toJson(unitID),
parseJson: (json) => parseJson(json),
onUnLoad: (unitID) => {
Expand Down
Loading
Loading