Conditions | 2 |
Paths | 2 |
Total Lines | 52 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 'use strict' |
||
3 | module.exports = (input, callback) => { |
||
4 | const mqtt = require('mqtt') |
||
5 | const configuration = require('./configuration') |
||
6 | const validate = require('./validate') |
||
7 | let error = null |
||
8 | let output = null |
||
9 | let receivedMessage |
||
|
|||
10 | |||
11 | validate.sourceConfiguration(input, (validatedInput, thrownError) => { |
||
12 | input = validatedInput |
||
13 | error = thrownError |
||
14 | }) |
||
15 | validate.paramConfiguration(input, (validatedInput, thrownError) => { |
||
16 | input = validatedInput |
||
17 | error = thrownError |
||
18 | }) |
||
19 | |||
20 | // Send MQTT |
||
21 | if ( !error ) { |
||
22 | let configurationMqtt = configuration.mqtt(input) |
||
23 | let client = mqtt.connect(input.source.url, configurationMqtt) |
||
24 | |||
25 | client.on('connect', () => { |
||
26 | client.subscribe(input.source.topic, (errorConnection) => { |
||
27 | if ( !errorConnection ) { |
||
28 | client.publish(input.source.topic, input.params.payload, { |
||
29 | qos: input.params.qos, |
||
30 | retain: false |
||
31 | }) |
||
32 | } else { |
||
33 | error = errorConnection |
||
34 | } |
||
35 | }) |
||
36 | }) |
||
37 | |||
38 | client.on('message', (topic, message) => { |
||
39 | output = { |
||
40 | 'version': {'ref': 'output'}, |
||
41 | 'metadata': [ |
||
42 | {'name': 'message', 'value': input.params.payload.toString()}, |
||
43 | {'name': 'receivedMessage', 'value': message.toString()}, |
||
44 | {'name': 'qos', 'value': input.params.qos.toString()}, |
||
45 | {'name': 'timestamp', 'value': Date.now().toString()}, |
||
46 | {'name': 'topic', 'value': topic} |
||
47 | ] |
||
48 | } |
||
49 | client.end() |
||
50 | callback(error, output) |
||
51 | }) |
||
52 | |||
53 | } |
||
54 | } |
||
55 |