Skip to content
This repository was archived by the owner on Nov 28, 2018. It is now read-only.

Commit 6a366c3

Browse files
committed
Little bit of refactoring
1 parent 6a81e8d commit 6a366c3

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

lib/index.js

-27
This file was deleted.

lib/stomp.js

+41-14
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ function parse_frame(chunk) {
6666
body = null,
6767
headers_str = null;
6868

69-
if (!utils.really_defined(chunk))
69+
if (!utils.really_defined(chunk)) {
7070
return null;
71+
}
7172

7273
command = parse_command(chunk);
7374
data = chunk.slice(command.length + 1, chunk.length);
@@ -77,8 +78,9 @@ function parse_frame(chunk) {
7778
headers = parse_headers(the_rest[0]);
7879
body = the_rest.slice(1, the_rest.length);
7980

80-
if ('content-length' in headers)
81+
if ('content-length' in headers) {
8182
headers['bytes_message'] = true;
83+
}
8284

8385
args = {
8486
command: command,
@@ -108,7 +110,8 @@ function _connect(stomp) {
108110
}
109111
_setupListeners(stomp);
110112
});
111-
} else {
113+
}
114+
else {
112115
log.debug('Connecting to ' + stomp.host + ':' + stomp.port);
113116
stomp.socket = new net.Socket();
114117
stomp.socket.connect(stomp.port, stomp.host);
@@ -198,15 +201,20 @@ function stomp_connect(stomp, headers) {
198201
function _disconnect(stomp) {
199202
var socket = stomp.socket;
200203
socket.end();
201-
if (socket.readyState == 'readOnly')
204+
205+
if (socket.readyState == 'readOnly') {
202206
socket.destroy();
207+
}
208+
203209
log.debug('disconnect called');
204210
};
205211

206212
function send_command(stomp, command, headers, body, want_receipt) {
207213
var want_receipt = want_receipt || false;
208-
if (!utils.really_defined(headers))
214+
215+
if (!utils.really_defined(headers)) {
209216
headers = {};
217+
}
210218

211219
var args = {
212220
'command': command,
@@ -239,7 +247,7 @@ function send_frame(stomp, _frame) {
239247
//
240248
function Stomp(args) {
241249
this.port = args['port'] || 61613;
242-
this.host = args['host'] || "127.0.0.1";
250+
this.host = args['host'] || '127.0.0.1';
243251
this.debug = args['debug'];
244252
this.login = args['login'] || null;
245253
this.passcode = args['passcode'] || null;
@@ -263,6 +271,31 @@ Stomp.prototype.connect = function() {
263271
_connect(this);
264272
};
265273

274+
// ## Stomp.is_a_message(frame)
275+
//
276+
// **Test that `Frame` is a message**
277+
//
278+
// Takes a `Frame` object
279+
//
280+
Stomp.prototype.is_a_message = function(this_frame) {
281+
return (this_frame.headers !== null && utils.really_defined(this_frame.headers['message-id']))
282+
}
283+
284+
// ## Stomp.should_run_message_callback
285+
//
286+
// **Handle any registered message callbacks**
287+
//
288+
// Takes a `Frame` object
289+
//
290+
Stomp.prototype.should_run_message_callback = function(this_frame) {
291+
var subscription = this._subscribed_to[this_frame.headers.destination];
292+
if (this_frame.headers.destination !== null && subscription !== null) {
293+
if (subscription.enabled && subscription.callback !== null && typeof(subscription.callback) == 'function') {
294+
subscription.callback(this_frame.body, this_frame.headers);
295+
}
296+
}
297+
}
298+
266299
// ## Stomp.handle\_new_frame(frame)
267300
//
268301
// **Handle frame based on type. Emit events when needed.**
@@ -272,16 +305,10 @@ Stomp.prototype.connect = function() {
272305
Stomp.prototype.handle_new_frame = function(this_frame) {
273306
switch (this_frame.command) {
274307
case "MESSAGE":
275-
if (utils.really_defined(this_frame.headers['message-id'])) {
276-
if (this_frame.headers !== null && this_frame.headers.destination !== null && this._subscribed_to[this_frame.headers.destination] !== null) {
277-
var subscription = this._subscribed_to[this_frame.headers.destination];
278-
if (subscription.enabled && subscription.callback !== null && typeof(subscription.callback) == 'function') {
279-
subscription.callback(this_frame.body, this_frame.headers);
280-
}
281-
}
308+
if (this.is_a_message(this_frame)) {
309+
this.should_run_message_callback(this_frame);
282310
this.emit('message', this_frame);
283311
}
284-
285312
break;
286313
case "CONNECTED":
287314
log.debug('Connected to STOMP');

0 commit comments

Comments
 (0)