Skip to content

Commit b676e55

Browse files
committed
feat: add singleton decorator
1 parent 3f7b0bd commit b676e55

7 files changed

+67
-48
lines changed

src/services/activityBarService.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
import { ActivityBarEvent, IActivityBar, IActivityBarItem } from 'mo/core/activityBar';
2-
import { emit } from 'mo/common/eventEmitter';
3-
4-
export class ActivityBarService implements IActivityBar {
5-
data: IActivityBarItem[];
1+
import { ActivityBarEvent, IActivityBar, IActivityBarItem } from 'mo/core/workbench/activityBar';
2+
import { emit, EventService } from 'mo/services/eventService';
3+
import { injectable, inject } from 'tsyringe';
4+
import { BaseService } from './baseService';
5+
6+
@injectable()
7+
export class ActivityBarService extends BaseService implements IActivityBar {
8+
public data: IActivityBarItem[];
69
public selected: string;
710

8-
constructor(data: IActivityBarItem[] = [], selected: string = '') {
11+
constructor(
12+
@inject('IActivityBarItem') data: IActivityBarItem[] = [],
13+
selected: string = '',
14+
) {
15+
super();
916
this.data = data;
1017
this.selected = selected;
1118
}
1219

20+
public subscribe(name: ActivityBarEvent, callback: Function) {
21+
EventService.subscribe(name, callback);
22+
}
23+
1324
@emit(ActivityBarEvent.Selected)
14-
public onSelect(key: string, item?: IActivityBarItem) {
25+
public onSelect(key: string, item?: IActivityBarItem | undefined) {
1526
this.selected = key;
1627
}
1728

29+
@emit(ActivityBarEvent.OnClick)
1830
public onClick(event: React.MouseEvent, item: IActivityBarItem) {
1931

2032
}
@@ -39,4 +51,8 @@ export class ActivityBarService implements IActivityBar {
3951
public get(id: string) {
4052

4153
}
54+
55+
// public subscribe(name: ActivityBarEvent | ActivityBarEvent[], callback: CallbackEvent) {
56+
// EventService.subscribe(name, callback);
57+
// }
4258
}

src/services/editor/editorService.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { ITab } from 'mo/components/tabs';
2-
import { EditorEvent, IEditor, IEditorGroup } from 'mo/core/editor';
3-
import { emit } from 'mo/common/eventEmitter';
2+
import { EditorEvent, IEditor, IEditorGroup } from 'mo/core/workbench/editor';
3+
import { emit } from 'mo/services/eventService';
4+
import { EditorGroupService } from './groupService';
45

56
export class EditorService<T = any> implements IEditor<T> {
6-
public current: IEditorGroup;
7-
public groups: IEditorGroup[];
7+
public current: IEditorGroup | undefined;
8+
public groups!: IEditorGroup[];
89

9-
constructor(current: IEditorGroup, groups: IEditorGroup[] = []) {
10+
constructor(current?: IEditorGroup, groups: IEditorGroup[] = []) {
1011
this.current = current;
1112
this.groups = groups;
1213
}
@@ -20,6 +21,14 @@ export class EditorService<T = any> implements IEditor<T> {
2021
if (group) {
2122
group.tabs.push(tab);
2223
group.activeTab = tab;
24+
} else {
25+
group = new EditorGroupService(
26+
this.groups.length + 1,
27+
tab,
28+
[tab],
29+
);
30+
this.current = group;
31+
this.groups.push(group);
2332
}
2433
}
2534

src/services/editor/groupService.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import { IEditorGroup } from 'mo/core/editor';
1+
import { IEditorGroup } from 'mo/core/workbench/editor';
22
import { ITab } from 'mo/components/tabs';
33

4-
export class EditorGroupService<E = any> implements IEditorGroup {
4+
export class EditorGroupService implements IEditorGroup {
55
id: number;
66
activeTab: ITab;
77
tabs: ITab[];
88
breadcrumb: any[];
99
actions: any[];
1010
menu: any[];
11-
editorInstance: E;
11+
editorInstance: any;
1212

1313
constructor(
1414
id: number,
1515
activeTab: ITab,
1616
tabs: ITab[],
17-
breadcrumb: any[],
18-
actions: any[],
19-
menu: any[],
20-
editorInstance: E,
17+
breadcrumb: any[] = [],
18+
actions: any[] = [],
19+
menu: any[] = [],
20+
editorInstance?: any,
2121
) {
2222
this.id = id;
2323
this.tabs = tabs;

src/services/extensionService.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
import { ErrorMsg } from 'mo/common/error';
22
import { IContribute, IContributeType, IExtension, IExtensionEntry } from 'mo/core/extension';
33
import { IMolecule } from 'mo/core/molecule';
4-
import { defaultExtensions } from 'mo/extensions';
54

65
export class ExtensionService {
76
public extensions: IExtension[] = [];
87
public moleculeCtx: IMolecule;
98

109
constructor(extensionEntry: IExtensionEntry = {}, moleculeCtx: IMolecule) {
1110
this.moleculeCtx = moleculeCtx;
12-
this.load(defaultExtensions, moleculeCtx);
13-
this.load(extensionEntry, moleculeCtx);
11+
this.load(extensionEntry);
1412
}
1513

1614
/**
1715
* TODO: Current extension service can't parses VSCode theme, so needs to refactor
1816
* @param param0 extensionEntry object
1917
* @param moleculeCtx the context object of molecule
2018
*/
21-
public load({ location, extensions = [] }: IExtensionEntry, moleculeCtx: IMolecule) {
19+
public load({ location, extensions = [] }: IExtensionEntry) {
2220
try {
2321
if (extensions?.length === 0) return;
2422
this.extensions = this.extensions.concat(extensions || []);
2523

2624
extensions?.forEach((extension: IExtension, index: number) => {
2725
if (extension.main) {
2826
if (extension.activate) {
29-
extension.activate(moleculeCtx);
27+
extension.activate(this.moleculeCtx);
3028
} else {
3129
throw new Error(ErrorMsg.NotFoundActivate);
3230
}
@@ -51,7 +49,7 @@ export class ExtensionService {
5149
});
5250
}
5351

54-
unload(id: string) {
55-
console.log('unload extension:', id);
52+
unload(extension: IExtension) {
53+
console.log('unload extension:', extension.name);
5654
}
5755
}

src/services/moleculeService.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { IEditor } from 'mo/core/editor';
2-
import { IActivityBar } from 'mo/core/activityBar';
1+
import { IEditor } from 'mo/core/workbench/editor';
2+
import { IActivityBar } from 'mo/core/workbench/activityBar';
33
import { ITheme } from 'mo/core/theme';
4-
import { ISidebar } from 'mo/core/sidebar';
5-
6-
7-
export class MoleculeService {
8-
// public menuBar: MenuBar;
4+
import { ISidebar } from 'mo/core/workbench/sidebar';
5+
import { IMenuBar } from 'mo/core/workbench/menuBar';
6+
import { IMolecule } from 'mo/core/molecule';
7+
import { singleton } from 'tsyringe';
8+
@singleton()
9+
export class MoleculeService implements IMolecule {
10+
public menuBar: IMenuBar;
911
// public statusBar: StatusBar;
1012
public activityBar: IActivityBar;
1113
// public panel: Panel;
@@ -19,20 +21,20 @@ export class MoleculeService {
1921
// public shortcutKeys: ShortcutKeys;
2022

2123
constructor(
22-
// menuBar: MenuBar,
24+
menuBar: IMenuBar,
2325
// statusBar: StatusBar,
2426
activityBar: IActivityBar,
2527
editor: IEditor,
28+
sidebar: ISidebar,
2629
// panel: Panel,
2730
// layout: Layout,
2831
theme: ITheme,
29-
sidebar: ISidebar,
3032
// iconTheme: IconTheme,
3133
// settings: Settings,
3234
// local: Local,
3335
// shortcutKeys: ShortcutKeys,
3436
) {
35-
// this.menuBar = menuBar;
37+
this.menuBar = menuBar;
3638
// this.statusBar = statusBar;
3739
this.activityBar = activityBar;
3840
// this.panel = panel;
@@ -46,5 +48,3 @@ export class MoleculeService {
4648
// this.shortcutKeys = shortcutKeys;
4749
}
4850
};
49-
50-
// // TODO

src/services/sidebarService.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { ISidebar, ISidebarPane } from 'mo/core/sidebar';
1+
import { ISidebar, ISidebarPane } from 'mo/core/workbench/sidebar';
2+
import { singleton } from 'tsyringe';
23

3-
export class SidebarBarService implements ISidebar {
4+
@singleton()
5+
export class SidebarService implements ISidebar {
46
selected: string;
57
panes: ISidebarPane[];
68

@@ -12,12 +14,4 @@ export class SidebarBarService implements ISidebar {
1214
onSelect(key: string) {
1315
this.selected = key;
1416
}
15-
16-
// getSelected() {
17-
// return this.selected;
18-
// }
19-
20-
// getPanes() {
21-
// return this.panes;
22-
// }
2317
}

src/services/themeServices.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { IExtension } from 'mo/core/extension';
77
import { ITheme, ThemeColor, TokenColor } from 'mo/core/theme';
8+
import { singleton } from 'tsyringe';
89

910
/**
1011
* Apply css content to workbench
@@ -24,6 +25,7 @@ function _applyRules(styleSheetContent: string, rulesClassName: string) {
2425
}
2526
}
2627

28+
@singleton()
2729
export class ThemeService implements ITheme {
2830
id: string;
2931
name: string;

0 commit comments

Comments
 (0)