1
1
const { PushError, ERROR } = require ( '../../send/data' ) ;
2
+ const { FRAME_NAME } = require ( '../../send/proto' ) ;
2
3
const { SynFlushTransform } = require ( './syn' ) ,
3
4
{ encode, FRAME , pools } = require ( '../../send' ) ;
4
5
@@ -35,6 +36,7 @@ class Batcher extends SynFlushTransform {
35
36
this . ids = { } ; // {aid: {p: {f: id}}}
36
37
this . buffers = { } ; // {id: [push, push, ...]}
37
38
this . listeners = { } ; // {id: function}
39
+ this . flushes = { } ; // {flushid: [pool id, pool id, ...]}
38
40
this . count = 0 ;
39
41
this . size = size ;
40
42
}
@@ -47,22 +49,59 @@ class Batcher extends SynFlushTransform {
47
49
* @param {function } callback callback
48
50
*/
49
51
_transform ( push , encoding , callback ) {
52
+ this . log . d ( 'in batcher _transform' , FRAME_NAME [ push . frame ] , push . _id ) ;
50
53
if ( push . frame & FRAME . CMD ) {
51
54
if ( push . frame & ( FRAME . FLUSH | FRAME . SYN ) ) {
52
- if ( this . do_flush ( ( ) => { } ) === true ) {
53
- this . push ( push ) ;
54
- }
55
+ this . do_flush ( ( ) => {
56
+ this . flushes [ push . payload ] = [ ] ;
57
+ this . log . d ( 'in batcher _transform' , FRAME_NAME [ push . frame ] , push . _id , 'sending to' , Object . keys ( this . listeners ) ) ;
58
+ for ( let id in this . listeners ) {
59
+ pools . pools [ id ] . write ( encode ( push . frame , push . payload ) ) ;
60
+ this . flushes [ push . payload ] . push ( id ) ;
61
+ }
62
+ callback ( ) ;
63
+ } ) ;
55
64
}
56
- for ( let id in this . listeners ) {
57
- pools . pools [ id ] . write ( encode ( push . frame , push . payload ) ) ;
65
+ else {
66
+ for ( let id in this . listeners ) {
67
+ pools . pools [ id ] . write ( encode ( push . frame , push . payload ) ) ;
68
+ }
69
+ callback ( ) ;
58
70
}
71
+ return ;
72
+ }
73
+ else if ( push . frame & FRAME . RESULTS ) {
74
+ this . push ( push ) ;
59
75
callback ( ) ;
60
76
return ;
61
77
}
62
78
63
79
let id = this . ids [ push . a ] [ push . p ] [ push . f ] ;
64
80
this . buffers [ id ] . push ( push ) ;
65
81
this . count ++ ;
82
+ this . state . incSending ( push . m ) ;
83
+
84
+ if ( ! this . listeners [ id ] ) {
85
+ // this.listeners[id] = true;
86
+ // pools.pools[id].pipe(this);
87
+ this . listeners [ id ] = result => {
88
+ if ( result . frame & FRAME . FLUSH ) {
89
+ let pls = this . flushes [ result . payload ] ;
90
+ if ( pls && pls . indexOf ( id ) !== - 1 ) {
91
+ pls . splice ( pls . indexOf ( id ) , 1 ) ;
92
+ if ( ! pls . length ) {
93
+ this . log . d ( 'flush %d is done' , result . payload ) ;
94
+ this . push ( result ) ;
95
+ delete this . flushes [ result . payload ] ;
96
+ }
97
+ }
98
+ }
99
+ else {
100
+ this . push ( result ) ;
101
+ }
102
+ } ;
103
+ pools . pools [ id ] . on ( 'data' , this . listeners [ id ] ) ;
104
+ }
66
105
67
106
if ( this . count >= this . size ) {
68
107
this . log . d ( 'flushing' ) ;
@@ -98,12 +137,15 @@ class Batcher extends SynFlushTransform {
98
137
* @param {function } callback callback
99
138
*/
100
139
_flush ( callback ) {
140
+ this . log . d ( 'in batcher _flush' ) ;
101
141
// this.do_flush(callback);
102
142
this . do_flush ( err => {
103
143
if ( err ) {
144
+ this . log . d ( '_flush err, callback' , err ) ;
104
145
callback ( err ) ;
105
146
}
106
147
else {
148
+ this . log . d ( '_flush ok, synIt' ) ;
107
149
this . synIt ( callback ) ;
108
150
}
109
151
} ) ;
@@ -116,6 +158,7 @@ class Batcher extends SynFlushTransform {
116
158
* @returns {boolean|undefined } true in case nothing has been written
117
159
*/
118
160
do_flush ( callback ) {
161
+ this . log . d ( 'in batcher do_flush' ) ;
119
162
let count = 0 ,
120
163
anything = false ,
121
164
/**
@@ -140,17 +183,7 @@ class Batcher extends SynFlushTransform {
140
183
anything = true ;
141
184
count ++ ;
142
185
this . count -= this . buffers [ id ] . length ;
143
-
144
- if ( ! this . listeners [ id ] ) {
145
- // this.listeners[id] = true;
146
- // pools.pools[id].pipe(this);
147
- this . listeners [ id ] = result => {
148
- this . push ( result ) ;
149
- } ;
150
- pools . pools [ id ] . on ( 'data' , this . listeners [ id ] ) ;
151
- }
152
186
pools . pools [ id ] . write ( encode ( FRAME . SEND , this . buffers [ id ] ) , cb ) ;
153
-
154
187
this . buffers [ id ] = [ ] ;
155
188
}
156
189
}
@@ -170,6 +203,7 @@ class Batcher extends SynFlushTransform {
170
203
* @param {function } callback callback
171
204
*/
172
205
_final ( callback ) {
206
+ this . log . d ( 'in batcher _final' ) ;
173
207
if ( this . count ) {
174
208
callback ( new PushError ( 'final with data left' , ERROR . EXCEPTION ) ) ;
175
209
// this.log.d('final: flushing');
0 commit comments