@@ -182,51 +182,27 @@ public void run() {
182182 return ;
183183 }
184184
185- List <Object > _args = new ArrayList <Object >(args .length + 1 );
186- _args .add (event );
187- _args .addAll (Arrays .asList (args ));
188-
189- JSONArray jsonArgs = new JSONArray ();
190- for (Object arg : _args ) {
191- jsonArgs .put (arg );
192- }
193- int parserType = HasBinary .hasBinary (jsonArgs ) ? Parser .BINARY_EVENT : Parser .EVENT ;
194- Packet <JSONArray > packet = new Packet <JSONArray >(parserType , jsonArgs );
195-
196- if (_args .get (_args .size () - 1 ) instanceof Ack ) {
197- logger .fine (String .format ("emitting packet with ack id %d" , Socket .this .ids ));
198- Socket .this .acks .put (Socket .this .ids , (Ack )_args .remove (_args .size () - 1 ));
199- jsonArgs = remove (jsonArgs , jsonArgs .length () - 1 );
200- packet .data = jsonArgs ;
201- packet .id = Socket .this .ids ++;
202- }
203-
204- if (Socket .this .connected ) {
205- Socket .this .packet (packet );
185+ Ack ack ;
186+ Object [] _args ;
187+ int lastIndex = args .length - 1 ;
188+
189+ if (args .length > 0 && args [lastIndex ] instanceof Ack ) {
190+ _args = new Object [lastIndex ];
191+ for (int i = 0 ; i < lastIndex ; i ++) {
192+ _args [i ] = args [i ];
193+ }
194+ ack = (Ack ) args [lastIndex ];
206195 } else {
207- Socket .this .sendBuffer .add (packet );
196+ _args = args ;
197+ ack = null ;
208198 }
199+
200+ emit (event , _args , ack );
209201 }
210202 });
211203 return this ;
212204 }
213205
214- private static JSONArray remove (JSONArray a , int pos ) {
215- JSONArray na = new JSONArray ();
216- for (int i = 0 ; i < a .length (); i ++){
217- if (i != pos ) {
218- Object v ;
219- try {
220- v = a .get (i );
221- } catch (JSONException e ) {
222- v = null ;
223- }
224- na .put (v );
225- }
226- }
227- return na ;
228- }
229-
230206 /**
231207 * Emits an event with an acknowledge.
232208 *
@@ -239,25 +215,28 @@ public Emitter emit(final String event, final Object[] args, final Ack ack) {
239215 EventThread .exec (new Runnable () {
240216 @ Override
241217 public void run () {
242- List <Object > _args = new ArrayList <Object >() {{
243- add (event );
244- if (args != null ) {
245- addAll (Arrays .asList (args ));
246- }
247- }};
248-
249218 JSONArray jsonArgs = new JSONArray ();
250- for (Object _arg : _args ) {
251- jsonArgs .put (_arg );
219+ jsonArgs .put (event );
220+
221+ if (args != null ) {
222+ for (Object arg : args ) {
223+ jsonArgs .put (arg );
224+ }
252225 }
253- int parserType = HasBinary .hasBinary (jsonArgs ) ? Parser .BINARY_EVENT : Parser .EVENT ;
254- Packet <JSONArray > packet = new Packet <JSONArray >(parserType , jsonArgs );
255226
256- logger .fine (String .format ("emitting packet with ack id %d" , ids ));
257- Socket .this .acks .put (ids , ack );
258- packet .id = ids ++;
227+ Packet <JSONArray > packet = new Packet <JSONArray >(Parser .EVENT , jsonArgs );
228+
229+ if (ack != null ) {
230+ logger .fine (String .format ("emitting packet with ack id %d" , ids ));
231+ Socket .this .acks .put (ids , ack );
232+ packet .id = ids ++;
233+ }
259234
260- Socket .this .packet (packet );
235+ if (Socket .this .connected ) {
236+ Socket .this .packet (packet );
237+ } else {
238+ Socket .this .sendBuffer .add (packet );
239+ }
261240 }
262241 });
263242 return this ;
@@ -371,9 +350,7 @@ public void run() {
371350 jsonArgs .put (arg );
372351 }
373352
374- int type = HasBinary .hasBinary (jsonArgs )
375- ? Parser .BINARY_ACK : Parser .ACK ;
376- Packet <JSONArray > packet = new Packet <JSONArray >(type , jsonArgs );
353+ Packet <JSONArray > packet = new Packet <JSONArray >(Parser .ACK , jsonArgs );
377354 packet .id = id ;
378355 self .packet (packet );
379356 }
0 commit comments