File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { EventEmitter } from 'mo/common/event' ;
2
+
3
+ export const EventService = new EventEmitter ( ) ;
4
+
5
+ /**
6
+ * Emit decorator, when the function be called,
7
+ * it's going to notify the listener
8
+ * @param name Event name
9
+ */
10
+ export function emit ( name : string ) {
11
+ return function (
12
+ target ,
13
+ property : string ,
14
+ descriptor : PropertyDescriptor ,
15
+ ) {
16
+ const original = descriptor . value ;
17
+ if ( typeof original === 'function' ) {
18
+ descriptor . value = function ( ...args ) {
19
+ try {
20
+ const result = original . apply ( this , args ) ;
21
+ EventService . emit ( name , args ) ;
22
+ return result ;
23
+ } catch ( e ) {
24
+ throw e ;
25
+ }
26
+ } ;
27
+ }
28
+ return descriptor ;
29
+ } ;
30
+ }
31
+
32
+
33
+ /**
34
+ * When the event emitted, it's going to call target function
35
+ * @param name Event name
36
+ */
37
+ export function subscribe ( name : string | string [ ] ) {
38
+ return function (
39
+ target ,
40
+ property : string ,
41
+ descriptor : PropertyDescriptor ,
42
+ ) {
43
+ const original = descriptor . value ;
44
+ if ( typeof original === 'function' ) {
45
+ EventService . subscribe ( name , original ) ;
46
+ }
47
+ return descriptor ;
48
+ } ;
49
+ }
You can’t perform that action at this time.
0 commit comments