1
+ 'use strict' ;
2
+
3
+ var _dec , _class ;
4
+
5
+ var _flowRuntime = require ( 'flow-runtime' ) ;
6
+
7
+ var _flowRuntime2 = _interopRequireDefault ( _flowRuntime ) ;
8
+
9
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
10
+
11
+ function _asyncToGenerator ( fn ) { return function ( ) { var gen = fn . apply ( this , arguments ) ; return new Promise ( function ( resolve , reject ) { function step ( key , arg ) { try { var info = gen [ key ] ( arg ) ; var value = info . value ; } catch ( error ) { reject ( error ) ; return ; } if ( info . done ) { resolve ( value ) ; } else { return Promise . resolve ( value ) . then ( function ( value ) { step ( "next" , value ) ; } , function ( err ) { step ( "throw" , err ) ; } ) ; } } return step ( "next" ) ; } ) ; } ; }
12
+
13
+ let Command = ( _dec = _flowRuntime2 . default . annotate ( _flowRuntime2 . default . class ( 'Command' , Command => {
14
+ return [ _flowRuntime2 . default . method ( 'constructor' , _flowRuntime2 . default . param ( 'command' , _flowRuntime2 . default . any ( ) ) , _flowRuntime2 . default . param ( 'args' , _flowRuntime2 . default . any ( ) ) , _flowRuntime2 . default . param ( 'middlewares' , _flowRuntime2 . default . any ( ) ) , _flowRuntime2 . default . param ( 'handler' , _flowRuntime2 . default . any ( ) ) ) , _flowRuntime2 . default . method ( 'call' , _flowRuntime2 . default . param ( 'parser' , _flowRuntime2 . default . any ( ) ) , _flowRuntime2 . default . param ( 'message' , _flowRuntime2 . default . any ( ) ) , _flowRuntime2 . default . param ( 'args' , _flowRuntime2 . default . any ( ) ) ) , _flowRuntime2 . default . method ( 'callMiddlewares' , _flowRuntime2 . default . param ( 'message' , _flowRuntime2 . default . any ( ) ) ) , _flowRuntime2 . default . method ( 'sub' , _flowRuntime2 . default . param ( 'cmd' , _flowRuntime2 . default . union ( _flowRuntime2 . default . string ( ) , _flowRuntime2 . default . ref ( Command ) ) ) , _flowRuntime2 . default . param ( 'handler' , _flowRuntime2 . default . function ( ) ) ) , _flowRuntime2 . default . method ( 'command' ) , _flowRuntime2 . default . method ( 'args' ) , _flowRuntime2 . default . method ( 'middlewares' ) , _flowRuntime2 . default . method ( 'handler' ) , _flowRuntime2 . default . method ( 'subs' ) ] ;
15
+ } ) ) , _dec ( _class = class Command {
16
+
17
+ constructor ( command , args , middlewares , handler ) {
18
+ this . _command = command ;
19
+ this . _args = args ;
20
+ this . _middlewares = middlewares ;
21
+ this . _handler = handler ;
22
+ this . _subs = [ ] ;
23
+ }
24
+
25
+ call ( parser , message , args ) {
26
+ var _this = this ;
27
+
28
+ return _asyncToGenerator ( function * ( ) {
29
+ let subCalled = false ;
30
+ if ( args . length > 0 ) {
31
+ _this . _subs . forEach ( function ( sub ) {
32
+ if ( sub . command === args [ 0 ] ) {
33
+ if ( _this . callMiddlewares ( message ) ) {
34
+ sub . call ( message , args . slice ( 1 ) ) ;
35
+ subCalled = true ;
36
+ }
37
+ return ;
38
+ }
39
+ } ) ;
40
+ }
41
+ if ( ! subCalled ) {
42
+ const map = new Map ( ) ;
43
+ for ( let i = 0 ; i < _this . _args . length ; i ++ ) {
44
+ let argument = args [ i ] ;
45
+
46
+ if ( ! _this . _args [ i ] . optional && ! argument ) return ;
47
+
48
+ if ( _this . _args [ i ] . list ) {
49
+ argument = args . splice ( i ) ;
50
+ let match = true ;
51
+ argument . every ( function ( val ) {
52
+ if ( _this . _args [ i ] . regex . test ( val ) ) return true ; else return match = false ;
53
+ } ) ;
54
+ if ( ! match ) return ;
55
+ } else {
56
+ const test = _this . _args [ i ] . regex . test ( argument ) ;
57
+ if ( ! test ) return ;
58
+ if ( i + 1 === _this . _args . length && i + 1 < args . length ) return ;
59
+ }
60
+
61
+ if ( parser && parser instanceof Array ) for ( let i = 0 ; i < parser . length ; i ++ ) {
62
+ const parse = parser [ i ] ;
63
+ let cond = false ;
64
+ if ( parse . match instanceof RegExp ) cond = parse . match . test ( argument ) ; else if ( typeof parse . match === 'function' ) cond = parse . match ( message , argument ) ;
65
+ if ( cond && typeof parse . perform === 'function' ) {
66
+ let res = parse . perform ( message , argument ) ;
67
+ if ( res instanceof Promise ) res = yield res ;
68
+ argument = res ;
69
+ }
70
+ } ;
71
+
72
+ map . set ( _this . _args [ i ] . key , argument ) ;
73
+ }
74
+
75
+ if ( _this . callMiddlewares ( message , map ) ) _this . _handler ( message , map ) ;
76
+ }
77
+ } ) ( ) ;
78
+ }
79
+
80
+ callMiddlewares ( message ) {
81
+ let ret = true ;
82
+ this . _middlewares . every ( middleware => {
83
+ return ! middleware ( this , message ) ? ret = false : true ;
84
+ } ) ;
85
+ return ret ;
86
+ }
87
+
88
+ sub ( cmd , handler = ( ) => null ) {
89
+ let _cmdType = _flowRuntime2 . default . union ( _flowRuntime2 . default . string ( ) , _flowRuntime2 . default . ref ( Command ) ) ;
90
+
91
+ let _handlerType = _flowRuntime2 . default . function ( ) ;
92
+
93
+ _flowRuntime2 . default . param ( 'cmd' , _cmdType ) . assert ( cmd ) ;
94
+
95
+ _flowRuntime2 . default . param ( 'handler' , _handlerType ) . assert ( handler ) ;
96
+
97
+ const CommandBuilder = require ( './Builders/CommandBuilder' ) ;
98
+ if ( cmd instanceof Command ) this . _subs . push ( cmd ) ; else if ( typeof cmd === 'string' && typeof handler === 'function' ) return new CommandBuilder ( null ) . parent ( this ) . command ( cmd ) . handler ( handler ) ;
99
+ }
100
+
101
+ get command ( ) {
102
+ return this . _command ;
103
+ }
104
+ get args ( ) {
105
+ return this . _args ;
106
+ }
107
+ get middlewares ( ) {
108
+ return this . _middlewares ;
109
+ }
110
+ get handler ( ) {
111
+ return this . _handler ;
112
+ }
113
+ get subs ( ) {
114
+ return this . _subs ;
115
+ }
116
+
117
+ } ) || _class ) ;
118
+
119
+
120
+ module . exports = Command ;
0 commit comments