Skip to content

Commit 20c5dbe

Browse files
committed
feat: extract GlobalEvent abstract class
1 parent 0bcbbe4 commit 20c5dbe

11 files changed

+36
-31
lines changed

src/common/eventBus.ts src/common/event/decorator.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { EventEmitter } from 'mo/common/event';
2-
3-
export const EventBus = new EventEmitter();
1+
import { EventBus } from './eventBus';
42

53
/**
64
* Emit decorator, when the function be called,
@@ -29,7 +27,6 @@ export function emit(name: string) {
2927
};
3028
}
3129

32-
3330
/**
3431
* When the event emitted, it's going to call target function
3532
* @param name Event name

src/common/event/eventBus.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { EventEmitter } from 'mo/common/event/eventEmitter';
2+
3+
export const EventBus = new EventEmitter();
4+
5+
export abstract class GlobalEvent {
6+
/**
7+
* Subscribe the service event
8+
* @param name Event name
9+
* @param callback Callback function
10+
*/
11+
public subscribe(name: string | string [], callback: Function) {
12+
EventBus.subscribe(name, callback);
13+
}
14+
15+
/**
16+
* Emit the service event
17+
* @param name Event name
18+
* @param args Arguments
19+
*/
20+
public emit(name: string, ...args: any) {
21+
EventBus.emit(name, args);
22+
}
23+
}
24+
File renamed without changes.

src/common/event/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './decorator';
2+
export * from './eventBus';
3+
export * from './eventEmitter';

src/services/baseService.ts

-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
import { EventBus } from '../common/eventBus';
2-
31
export abstract class BaseService<S = any> {
4-
/**
5-
* Subscribe the service event
6-
* @param name Event name
7-
* @param callback Callback function
8-
*/
9-
public subscribe(name: string | string [], callback: Function) {
10-
EventBus.subscribe(name, callback);
11-
}
12-
13-
/**
14-
* Emit the service event
15-
* @param name Event name
16-
* @param args Arguments
17-
*/
18-
public emit(name: string, ...args: any) {
19-
EventBus.emit(name, args);
20-
}
212
}

src/services/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'reflect-metadata';
2-
export * from '../common/eventBus';
2+
export * from '../common/event/eventBus';
33
export * from './workbench/activityBarService';
4-
export * from '../common/eventBus';
4+
export * from '../common/event/eventBus';
55
export * from './extensionService';
66
export * from './workbench/menuBarService';
77
export * from './workbench/sidebarService';

src/services/react/component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BaseService } from 'mo/services/baseService';
1+
import { GlobalEvent } from 'mo/common/event';
22

3-
export abstract class Component<S> extends BaseService {
3+
export abstract class Component<S> extends GlobalEvent {
44
protected abstract state: S;
55

66
public updateState(nextState: S) {

src/services/workbench/editorService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component } from 'mo/services/react';
22
import { ITab } from 'mo/components/tabs';
3-
import { emit } from 'mo/common/eventBus';
3+
import { emit } from 'mo/common/event';
44
import { singleton, container } from 'tsyringe';
55
import { EditorModel, EditorGroupModel, IEditor, IEditorGroup } from 'mo/model/editor';
66

src/services/workbench/menuBarService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IMenuBar, IMenuBarItem, MenuBarModel } from 'mo/model/menuBar';
22
import { Component } from 'mo/services/react';
33
import { singleton, container } from 'tsyringe';
4-
import { emit } from '../../common/eventBus';
4+
import { emit } from '../../common/event';
55

66
/**
77
* The activity bar event definition

src/services/workbench/sidebarService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component } from 'mo/services/react';
22
import { singleton, container } from 'tsyringe';
3-
import { emit } from '../../common/eventBus';
3+
import { emit } from '../../common/event';
44
import { ISidebar, ISidebarPane, SidebarModel } from 'mo/model/sidebar';
55

66
/**

src/services/workbench/statusBarService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IStatusBar, IStatusBarItem, StatusBarModel } from 'mo/model/statusBar';
22
import { Component } from 'mo/services/react';
3-
import { emit } from 'mo/common/eventBus';
3+
import { emit } from 'mo/common/event';
44
import { container, singleton } from 'tsyringe';
55

66
/**

0 commit comments

Comments
 (0)