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: refactor core package to DI pattern and remove Context #45

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
refactor: remove context completely from UniverSheet
  • Loading branch information
Wenzhao Hu committed Aug 10, 2023
commit 1fe23ea95c4bc9c28a7519aef5991c7f5b35bcd1
32 changes: 16 additions & 16 deletions packages/base-sheets/src/SheetPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Engine, IRenderingEngine } from '@univerjs/base-render';
import { Plugin, PLUGIN_NAMES, DEFAULT_SELECTION, UniverSheet, PluginType, CommandManager, ICurrentUniverService, LocaleService } from '@univerjs/core';
import { Plugin, PLUGIN_NAMES, DEFAULT_SELECTION, UniverSheet, PluginType, CommandManager, ICurrentUniverService, LocaleService, ObserverManager } from '@univerjs/core';
import { Dependency, Inject, Injector } from '@wendellhu/redi';

import { SheetPluginObserve, uninstall } from './Basics/Observer';
Expand Down Expand Up @@ -66,14 +66,14 @@ export class SheetPlugin extends Plugin<SheetPluginObserve> {
config: Partial<ISheetPluginConfig>,
@ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,
@Inject(LocaleService) private readonly _localeService: LocaleService,
@Inject(Injector) private readonly _sheetInjector: Injector,
@Inject(Injector) override readonly _injector: Injector,
@Inject(CommandManager) private readonly _commandManager: CommandManager
) {
super(PLUGIN_NAMES.SPREADSHEET);

this._config = Object.assign(DEFAULT_SPREADSHEET_PLUGIN_DATA, config);

this.initializeDependencies(_sheetInjector);
this.initializeDependencies(_injector);
}

installTo(universheetInstance: UniverSheet) {
Expand Down Expand Up @@ -115,19 +115,19 @@ export class SheetPlugin extends Plugin<SheetPluginObserve> {

// TODO@huwenzhao: We don't need to init controllers manually
initController() {
this._sheetContainerController = this._sheetInjector.get(SheetContainerController);
this._cellEditorController = this._sheetInjector.get(CellEditorController);
this._formulaBarController = this._sheetInjector.get(FormulaBarController);
this._editTooltipsController = this._sheetInjector.get(EditTooltipsController);
this._sheetBarController = this._sheetInjector.get(SheetBarController);
this._toolbarController = this._sheetInjector.get(ToolbarController);
this._rightMenuController = this._sheetInjector.get(RightMenuController);
this._countBarController = this._sheetInjector.get(CountBarController);
this._hideColumnController = this._sheetInjector.get(HideColumnController);
this._sheetContainerController = this._injector.get(SheetContainerController);
this._cellEditorController = this._injector.get(CellEditorController);
this._formulaBarController = this._injector.get(FormulaBarController);
this._editTooltipsController = this._injector.get(EditTooltipsController);
this._sheetBarController = this._injector.get(SheetBarController);
this._toolbarController = this._injector.get(ToolbarController);
this._rightMenuController = this._injector.get(RightMenuController);
this._countBarController = this._injector.get(CountBarController);
this._hideColumnController = this._injector.get(HideColumnController);
}

initCanvasView() {
this._canvasEngine = this._sheetInjector.get(IRenderingEngine);
this._canvasEngine = this._injector.get(IRenderingEngine);
}

override onMounted(): void {
Expand All @@ -148,10 +148,10 @@ export class SheetPlugin extends Plugin<SheetPluginObserve> {

registerExtension() {
const actionRegister = this._commandManager.getActionExtensionManager().getRegister();
this._namedRangeActionExtensionFactory = new NamedRangeActionExtensionFactory(this, this._sheetInjector);
this._namedRangeActionExtensionFactory = new NamedRangeActionExtensionFactory(this, this._injector);
actionRegister.add(this._namedRangeActionExtensionFactory); // TODO: this should return a disposable function

this._columnRulerManager = this._sheetInjector.get(ColumnRulerManager);
this._columnRulerManager = this._injector.get(ColumnRulerManager);
const rulerRegister = this._columnRulerManager.getRegister();
this._hideColumnRulerFactory = new HideColumnRulerFactory(this);
rulerRegister.add(this._hideColumnRulerFactory);
Expand All @@ -167,7 +167,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve> {

/** @deprecated move to DI system */
getSelectionManager() {
return this._sheetInjector.get(ISelectionManager);
return this._injector.get(ISelectionManager);
}

private initializeDependencies(sheetInjector: Injector) {
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/Basics/Univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export class Univer {
/** Create a univer sheet instance with internal dependency injection. */
createUniverSheet(config: Partial<IWorkbookConfig>): UniverSheet {
const sheet = this._univerInjector.createInstance(UniverSheet, config);
sheet.context.onUniver(this);

// TODO@wzhudev: clean this
sheet.getWorkBook().onUniver();

this.initializePluginsForSheet(sheet);
this._currentUniverService.addSheet(sheet);
Expand All @@ -71,8 +73,6 @@ export class Univer {
}

addUniverSheet(univerSheet: UniverSheet): void {
univerSheet.context.onUniver(this);

this._currentUniverService.addSheet(univerSheet);
}

Expand Down Expand Up @@ -190,10 +190,8 @@ export class Univer {

private initializePluginsForSheet(sheet: UniverSheet): void {
const plugins = this._univerPluginRegistry.getRegisterPlugins(PluginType.Sheet);
const sheetInjector = sheet._sheetInjector;
plugins.forEach((p) => {
const pluginInstance: Plugin = sheetInjector.createInstance(p.plugin as unknown as Ctor<any>, p.options);
sheet.installPlugin(pluginInstance);
sheet.addPlugin(p.plugin as unknown as PluginCtor<any>, p.options);
});
}
}
152 changes: 23 additions & 129 deletions packages/core/src/Basics/UniverSheet.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
import { Ctor, Injector, Optional, Disposable, Dependency } from '@wendellhu/redi';
import { ObserverManager } from 'src/Observer';
import { Workbook, ColorBuilder } from '../Sheets/Domain';

import { ObserverManager } from '../Observer';
import { Workbook } from '../Sheets/Domain';
import { IWorkbookConfig } from '../Types/Interfaces';
import { BasePlugin, Plugin, PluginCtor, PluginStore } from '../Plugin';
import { GenName, IOHttp, IOHttpConfig, Logger } from '../Shared';
import { SheetContext } from './SheetContext';
import { Plugin, PluginCtor, PluginStore } from '../Plugin';
import { GenName, Logger } from '../Shared';
import { VersionCode, VersionEnv } from './Version';

interface IComposedConfig {
[key: string]: any;

workbookConfig: IWorkbookConfig;
}
import { WorkBookObserverImpl } from './WorkBookObserverImpl';

/**
* Externally provided UniverSheet root instance
*/
export class UniverSheet implements Disposable {
univerSheetConfig: Partial<IWorkbookConfig>;

readonly _sheetInjector: Injector;
private readonly _sheetInjector: Injector;

private readonly _pluginStore = new PluginStore();
private readonly _workbook: Workbook;

/**
* @deprecated this is a temporary solution, migrate modules to `sheetInjector`
*/
private _context: SheetContext;
private readonly _pluginStore = new PluginStore();

constructor(univerSheetData: Partial<IWorkbookConfig> = {}, @Optional(Injector) parentInjector?: Injector) {
this.univerSheetConfig = univerSheetData;

// Initialize injector after constructoring context
this._sheetInjector = this.initializeInjector(parentInjector);
this._context = new SheetContext(univerSheetData, this._sheetInjector);
this._context.UNSAFE_setObserverManager(this._sheetInjector.get(ObserverManager));
this._context.UNSAFE_setGenName(this._sheetInjector.get(GenName));
this._context.TEMP_setObserver();
}

/**
* get SheetContext
*/
get context() {
return this._context;
this.setObserver();
this._workbook = this._sheetInjector.createInstance(Workbook, univerSheetData);
}

static newInstance(univerSheetData: Partial<IWorkbookConfig> = {}): UniverSheet {
Expand All @@ -52,79 +34,18 @@ export class UniverSheet implements Disposable {
}

/**
*
* Request data
*
* @example
* Get data for all tables, including core and plugin data
*
* @param config
*/
static get<T = void>(config: Omit<IOHttpConfig, 'type'>): Promise<T> {
return IOHttp({ ...config, type: 'GET' });
}

/**
* Submit data
* @param config
*/
static post<T = void>(config: Omit<IOHttpConfig, 'type'>): Promise<T> {
return IOHttp({ ...config, type: 'POST' });
}

/**
* Load data
*
* @example
* UniverSheet.get gets all the core and plug-in data, UniverSheet.load(univerSheetInstance,data) internally calls the load API of each plug-in to centrally load the core and plug-in data
*
* @param sheet
* @param data
* get unit id
*/
static load<T extends IComposedConfig>(sheet: UniverSheet, data: T) {
sheet.getWorkBook().load(data.workbookConfig);
sheet.context
.getPluginManager()
.getPlugins()
.forEach((plugin: BasePlugin) => {
plugin.load(data[`${plugin.getPluginName()}Config`]);
});
}

static newColor(): ColorBuilder {
return new ColorBuilder();
getUnitId(): string {
return this.getWorkBook().getUnitId();
}

/**
* Save data
*
* @example
* get all the core and plug-in data
*
* @param univerSheet
*/
static toJson(univerSheet: UniverSheet): IComposedConfig {
const workbookConfig = univerSheet.getWorkBook().save();
const pluginConfig: Partial<IComposedConfig> = {};
univerSheet.context
.getPluginManager()
.getPlugins()
.forEach((plugin: BasePlugin) => {
pluginConfig[`${plugin.getPluginName()}Config`] = plugin.save();
});

return { workbookConfig, ...pluginConfig };
getWorkBook(): Workbook {
return this._workbook;
}

dispose(): void {}

/**
* get unit id
*/
getUnitId(): string {
return this.getWorkBook().getUnitId();
}

/**
* Add a plugin into UniverSheet. UniverSheet should add dependencies exposed from this plugin to its DI system.
*
Expand All @@ -137,44 +58,17 @@ export class UniverSheet implements Disposable {
const pluginInstance: Plugin = this._sheetInjector.createInstance(plugin as unknown as Ctor<any>, options);

// TODO: remove context passed in here
pluginInstance.onCreate(this._context);

pluginInstance.onCreate({} as any);
pluginInstance.onMounted({} as any);
this._pluginStore.addPlugin(pluginInstance);

// FIXME: this is temporary. Will be removed in the future.
this._context.getPluginManager().install(pluginInstance);
}

/**
* install plugin
*
* @param plugin - install plugin
* @deprecated Use addPlugin instead
*/
installPlugin(plugin: Plugin): void {
this._context.getPluginManager().install(plugin);
}

/**
* uninstall plugin
*
* @param name - plugin name
*/
uninstallPlugin(name: string): void {
this._context.getPluginManager().uninstall(name);
}

/**
* get WorkBook
*
* @returns Workbook
*/
getWorkBook(): Workbook {
return this._context.getWorkBook();
}

private initializeInjector(parentInjector?: Injector): Injector {
const dependencies: Dependency[] = [[ObserverManager], [GenName]];
return parentInjector ? parentInjector.createChild(dependencies) : new Injector(dependencies);
}
}

private setObserver(): void {
new WorkBookObserverImpl().install(this._sheetInjector.get(ObserverManager));
}
}
2 changes: 1 addition & 1 deletion packages/core/src/Basics/WorkBookObserverImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ export class WorkBookObserverImpl {
manager.addObserver('onSheetOrderObservable', 'core', new Observable());
manager.addObserver('onZoomRatioSheetObservable', 'core', new Observable());
}
}
}
Loading