5
5
*/
6
6
7
7
import { SessionConfig } from '../config' ;
8
- import { LocoSession , PacketResData , SessionFactory } from '../network/request-session' ;
9
- import { DefaultRes , DefaultReq , AsyncCommandResult } from '../request' ;
10
- import { LocoPacket } from '../packet ' ;
8
+ import { ConnectionSession , PacketResData , SessionFactory } from '../network/request-session' ;
9
+ import { DefaultReq , AsyncCommandResult , DefaultRes } from '../request' ;
10
+ import { BiStream } from '../stream ' ;
11
11
12
12
/**
13
13
* Hook incoming datas
@@ -24,11 +24,6 @@ export interface SessionHook {
24
24
*/
25
25
onRequest : ( method : string , data : DefaultReq ) => void ;
26
26
27
- /**
28
- * Hook loco packet
29
- */
30
- onSendPacket : ( packet : LocoPacket ) => void ;
31
-
32
27
onClose ( ) : ( ) => void ;
33
28
34
29
}
@@ -41,60 +36,51 @@ export class HookedSessionFactory implements SessionFactory {
41
36
42
37
}
43
38
44
- async createSession ( config : SessionConfig ) : AsyncCommandResult < LocoSession > {
45
- const sessionRes = await this . _factory . createSession ( config ) ;
39
+ async connect ( config : SessionConfig ) : AsyncCommandResult < ConnectionSession > {
40
+ const sessionRes = await this . _factory . connect ( config ) ;
46
41
if ( ! sessionRes . success ) return sessionRes ;
47
42
48
- return { status : sessionRes . status , success : true , result : new HookedLocoSession ( sessionRes . result , this . _hook ) } ;
43
+ return { status : sessionRes . status , success : true , result : new InspectSession ( sessionRes . result , this . _hook ) } ;
49
44
}
50
45
}
51
46
52
- /**
53
- * Hook loco session
54
- */
55
- export class HookedLocoSession implements LocoSession {
56
- constructor ( private _session : LocoSession , public hook : Partial < SessionHook > = { } ) {
47
+ export class InspectSession implements ConnectionSession {
48
+
49
+ constructor (
50
+ private _session : ConnectionSession ,
51
+ private _hook : Partial < SessionHook > = { }
52
+ ) {
57
53
58
54
}
59
55
56
+ get stream ( ) : BiStream {
57
+ return this . _session . stream ;
58
+ }
59
+
60
+ request < T = DefaultRes > ( method : string , data : DefaultReq ) : Promise < DefaultRes & T > {
61
+ if ( this . _hook . onRequest ) this . _hook . onRequest ( method , data ) ;
62
+
63
+ return this . _session . request ( method , data ) ;
64
+ }
65
+
60
66
listen ( ) : AsyncIterableIterator < PacketResData > {
61
- const hook = this . hook ;
62
67
const iterator = this . _session . listen ( ) ;
63
68
64
69
return {
65
- [ Symbol . asyncIterator ] ( ) {
70
+ [ Symbol . asyncIterator ] ( ) : AsyncIterableIterator < PacketResData > {
66
71
return this ;
67
72
} ,
68
73
69
- async next ( ) : Promise < IteratorResult < PacketResData > > {
74
+ next : async ( ) : Promise < IteratorResult < PacketResData > > = > {
70
75
const next = await iterator . next ( ) ;
71
76
72
- if ( ! next . done && hook . onData ) {
73
- const { method, data, push } = next . value ;
74
-
75
- hook . onData ( method , data , push ) ;
77
+ if ( ! next . done && this . _hook . onData ) {
78
+ this . _hook . onData ( next . value . method , next . value . data , next . value . push ) ;
76
79
}
77
80
78
81
return next ;
79
- } ,
82
+ }
80
83
} ;
81
84
}
82
85
83
- request < T = DefaultRes > ( method : string , data : DefaultReq ) : Promise < DefaultRes & T > {
84
- if ( this . hook . onRequest ) this . hook . onRequest ( method , data ) ;
85
-
86
- return this . _session . request ( method , data ) ;
87
- }
88
-
89
- sendPacket ( packet : LocoPacket ) : Promise < LocoPacket > {
90
- if ( this . hook . onSendPacket ) this . hook . onSendPacket ( packet ) ;
91
-
92
- return this . _session . sendPacket ( packet ) ;
93
- }
94
-
95
- close ( ) : void {
96
- if ( this . hook . onClose ) this . hook . onClose ( ) ;
97
-
98
- this . _session . close ( ) ;
99
- }
100
86
}
0 commit comments