@@ -10,7 +10,7 @@ import {
10
10
SentActivity ,
11
11
TypingActivity ,
12
12
} from '@microsoft/teams.api' ;
13
- import { EventEmitter } from '@microsoft/teams.common' ;
13
+ import { ConsoleLogger , EventEmitter , ILogger } from '@microsoft/teams.common' ;
14
14
15
15
import { IStreamer , IStreamerEvents } from '../../types' ;
16
16
import { promises } from '../../utils' ;
@@ -30,10 +30,12 @@ export class HttpStream implements IStreamer {
30
30
31
31
private _result ?: SentActivity ;
32
32
private _timeout ?: NodeJS . Timeout ;
33
+ private _logger : ILogger ;
33
34
34
- constructor ( client : Client , ref : ConversationReference ) {
35
+ constructor ( client : Client , ref : ConversationReference , logger ?: ILogger ) {
35
36
this . client = client ;
36
37
this . ref = ref ;
38
+ this . _logger = logger ?. child ( 'stream' ) || new ConsoleLogger ( '@teams/http/stream' ) ;
37
39
}
38
40
39
41
emit ( activity : Partial < IMessageActivity > | string ) {
@@ -54,8 +56,15 @@ export class HttpStream implements IStreamer {
54
56
}
55
57
56
58
async close ( ) {
57
- if ( ! this . index && ! this . queue . length ) return ;
58
- if ( this . _result ) return this . _result ;
59
+ if ( ! this . index && ! this . queue . length ) {
60
+ this . _logger . debug ( 'closed with no content' ) ;
61
+ return ;
62
+ }
63
+
64
+ if ( this . _result ) {
65
+ this . _logger . debug ( 'already closed' ) ;
66
+ return this . _result ;
67
+ }
59
68
60
69
while ( ! this . id || this . queue . length ) {
61
70
await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
@@ -68,7 +77,10 @@ export class HttpStream implements IStreamer {
68
77
. addStreamFinal ( )
69
78
. withChannelData ( this . channelData ) ;
70
79
71
- const res = await promises . retry ( this . send ( activity ) ) ;
80
+ const res = await promises . retry ( ( ) => this . send ( activity ) , {
81
+ logger : this . _logger
82
+ } ) ;
83
+
72
84
this . events . emit ( 'close' , res ) ;
73
85
74
86
this . index = 0 ;
@@ -78,6 +90,7 @@ export class HttpStream implements IStreamer {
78
90
this . channelData = { } ;
79
91
this . entities = [ ] ;
80
92
this . _result = res ;
93
+ this . _logger . debug ( res ) ;
81
94
return res ;
82
95
}
83
96
@@ -88,10 +101,9 @@ export class HttpStream implements IStreamer {
88
101
this . _timeout = undefined ;
89
102
}
90
103
91
- const size = Math . round ( this . queue . length / 10 ) ;
92
104
let i = 0 ;
93
105
94
- while ( this . queue . length && i <= size ) {
106
+ while ( this . queue . length && i < 10 ) {
95
107
const activity = this . queue . shift ( ) ;
96
108
97
109
if ( ! activity ) continue ;
@@ -118,13 +130,18 @@ export class HttpStream implements IStreamer {
118
130
i ++ ;
119
131
}
120
132
121
- this . index ++ ;
133
+ if ( i === 0 ) return ;
134
+
122
135
const activity = new TypingActivity ( { id : this . id } )
123
136
. withText ( this . text )
124
- . addStreamUpdate ( this . index ) ;
137
+ . addStreamUpdate ( this . index + 1 ) ;
138
+
139
+ const res = await promises . retry ( ( ) => this . send ( activity ) , {
140
+ logger : this . _logger
141
+ } ) ;
125
142
126
- const res = await promises . retry ( this . send ( activity ) ) ;
127
143
this . events . emit ( 'chunk' , res ) ;
144
+ this . index ++ ;
128
145
129
146
if ( ! this . id ) {
130
147
this . id = res . id ;
0 commit comments