Skip to content

Commit 739e559

Browse files
author
RyLee Harrison
committed
updated: newest widely supported javascript spec
1 parent 98cb2e1 commit 739e559

File tree

3 files changed

+132
-122
lines changed

3 files changed

+132
-122
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ logs
33
*.log
44
npm-debug.log*
55

6+
*.lock
7+
68
# Runtime data
79
pids
810
*.pid

index.js

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,103 @@
1-
'use strict';
2-
var mqtt = require('mqtt');
3-
var inArray = require('in-array');
4-
5-
var RegularClientPrototype = mqtt.MqttClient.prototype;
6-
7-
var ASYNC_METHODS = ['publish',
8-
'subscribe',
9-
'unsubscribe',
10-
'end'
11-
];
12-
13-
var SYNC_METHODS = [
14-
'addListener',
15-
'emit',
16-
'eventNames',
17-
'getMaxListeners',
18-
'listenerCount',
19-
'listeners',
20-
'off',
21-
'on',
22-
'once',
23-
'prependListener',
24-
'prependOnceListener',
25-
'removeAllListeners',
26-
'removeListener',
27-
'setMaxListeners',
28-
'rawListeners'
29-
];
1+
const mqtt = require('mqtt');
2+
const RegularClientPrototype = mqtt.MqttClient.prototype;
303

31-
module.exports = {
32-
connect: connect,
33-
AsyncClient: AsyncClient
34-
};
4+
class AsyncClient {
5+
constructor(client) {
6+
this.client = client
7+
}
358

36-
function connect (brokerURL, opts) {
37-
var client = mqtt.connect(brokerURL, opts);
9+
set handleMessage(newHandler) {
10+
this.client.handleMessage = newHandler;
11+
}
3812

39-
var asyncClient = new AsyncClient(client);
13+
get handleMessage() {
14+
return this.client.handleMessage;
15+
}
4016

41-
return asyncClient;
42-
}
17+
publish(...args) {
18+
return Promise.resolve(this.client.publish(...args))
19+
}
20+
21+
subscribe(...args) {
22+
return Promise.resolve(this.client.subscribe(...args))
23+
}
24+
25+
unsubscribe(...args) {
26+
return Promise.resolve(this.client.unsubscribe(...args))
27+
}
4328

44-
function AsyncClient (client) {
45-
this._client = client;
46-
}
29+
end(...args) {
30+
return Promise.resolve(this.client.end(...args))
31+
}
4732

48-
AsyncClient.prototype = {
49-
set handleMessage (newHandler) {
50-
this._client.handleMessage = newHandler;
51-
},
52-
get handleMessage () {
53-
return this._client.handleMessage;
33+
addListener(...args) {
34+
this.client.addListener(...args);
5435
}
55-
};
5636

57-
ASYNC_METHODS.forEach(defineAsync);
58-
SYNC_METHODS.forEach(definePassthrough);
37+
emit(...args) {
38+
this.client.emit(...args);
39+
}
5940

60-
function definePassthrough (name) {
61-
AsyncClient.prototype[name] = function () {
62-
var client = this._client;
63-
return client[name].apply(client, arguments);
64-
};
65-
}
41+
eventNames(...args) {
42+
this.client.eventNames(...args);
43+
}
44+
45+
getMaxListeners(...args) {
46+
this.client.getMaxListeners(...args);
47+
}
48+
49+
listenerCount(...args) {
50+
this.client.listenerCount(...args);
51+
}
52+
53+
listeners(...args) {
54+
this.client.listeners(...args);
55+
}
6656

67-
function defineAsync (name) {
68-
AsyncClient.prototype[name] = function asyncMethod () {
69-
var client = this._client;
70-
var args = [];
71-
var length = arguments.length;
72-
var i = 0;
73-
for (i; i < length; i++)
74-
args.push(arguments[i]);
75-
76-
return new Promise(function (resolve, reject) {
77-
args.push(makeCallback(resolve, reject));
78-
client[name].apply(client, args);
79-
});
80-
};
57+
off(...args) {
58+
this.client.off(...args);
59+
}
60+
61+
on(...args) {
62+
this.client.on(...args);
63+
}
64+
65+
once(...args) {
66+
this.client.once(...args);
67+
}
68+
69+
prependListener(...args) {
70+
this.client.prependListener(...args);
71+
}
72+
73+
prependOnceListener(...args) {
74+
this.client.prependOnceListener(...args);
75+
}
76+
77+
removeAllListeners(...args) {
78+
this.client.removeAllListeners(...args);
79+
}
80+
81+
removeListener(...args) {
82+
this.client.removeListener(...args);
83+
}
84+
85+
setMaxListeners(...args) {
86+
this.client.setMaxListeners(...args);
87+
}
88+
89+
rawListeners(...args) {
90+
this.client.rawListeners(...args);
91+
}
92+
};
93+
94+
const connect = (brokerUrl, opts = {}) => {
95+
const client = mqtt.connect(brokerUrl, opts);
96+
const asyncClient = new AsyncClient(client);
97+
return asyncClient
8198
}
8299

83-
function makeCallback (resolve, reject) {
84-
return function (err, data) {
85-
if (err)
86-
reject(err);
87-
else resolve(data);
88-
};
100+
module.exports = {
101+
connect,
102+
AsyncClient
89103
}

test.js

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,89 @@
1-
'use strict';
1+
const Server = require('mqtt/test/server');
22

3-
var Server = require('mqtt/test/server');
3+
const AsyncMQTT = require('./');
4+
const AsyncClient = AsyncMQTT.AsyncClient;
45

5-
var AsyncMQTT = require('./');
6-
var AsyncClient = AsyncMQTT.AsyncClient;
6+
const test = require('tape');
77

8-
var test = require('tape');
8+
const SERVER_PORT = 1883;
9+
const SERVER_URL = `mqtt://localhost:${SERVER_PORT}`;
910

10-
var SERVER_PORT = 1883;
11-
var SERVER_URL = 'mqtt://localhost:' + SERVER_PORT;
12-
13-
var server = buildServer().listen(SERVER_PORT);
11+
const server = buildServer().listen(SERVER_PORT);
1412
server.unref();
1513

1614
server.on('listening', runTests);
1715

1816
function runTests () {
19-
test('Connect should return an instance of AsyncClient', function (t) {
17+
test('Connect should return an instance of AsyncClient', t => {
2018
t.plan(1);
21-
var client = AsyncMQTT.connect(SERVER_URL);
19+
const client = AsyncMQTT.connect(SERVER_URL);
2220

2321
t.ok(client instanceof AsyncClient, 'Connect returned an AsyncClient');
2422
client.end();
2523
});
2624

27-
test('Should be able to listen on event on client', function (t) {
25+
test('Should be able to listen on event on client', t => {
2826
t.plan(1);
2927

30-
var client = AsyncMQTT.connect(SERVER_URL);
28+
const client = AsyncMQTT.connect(SERVER_URL);
3129

32-
client.once('connect', function () {
30+
client.once('connect', () => {
3331
t.pass('Connected');
3432
client.end();
3533
});
3634
});
3735

38-
test('Calling end() should resolve once disconnected', function (t) {
36+
test('Calling end() should resolve once disconnected', t => {
3937
t.plan(2);
4038

41-
var client = AsyncMQTT.connect(SERVER_URL);
39+
const client = AsyncMQTT.connect(SERVER_URL);
4240

43-
client.on('close', function () {
41+
client.on('close', () => {
4442
t.pass('Close event occured');
4543
});
4644

47-
client.on('connect', function () {
45+
client.on('connect', () => {
4846
// Wait for connect to emit before ending
49-
client.end().then(function () {
47+
client.end().then(() => {
5048
t.pass('End resolved');
5149
});
5250
});
5351
});
5452

55-
test('Calling subscribe should resolve once subscribed', function (t) {
53+
test('Calling subscribe should resolve once subscribed', t => {
5654
t.plan(1);
5755

58-
var client = AsyncMQTT.connect(SERVER_URL);
56+
const client = AsyncMQTT.connect(SERVER_URL);
5957

6058
client.subscribe('example', {
6159
qos: 1
62-
}).then(function () {
60+
}).then(() => {
6361
t.pass('Subscribed');
6462
client.end();
6563
})
6664
});
6765

68-
test('Calling unsubscribe should resolve once completed', function (t) {
66+
test('Calling unsubscribe should resolve once completed', t => {
6967
t.plan(1);
7068

71-
var client = AsyncMQTT.connect(SERVER_URL);
69+
const client = AsyncMQTT.connect(SERVER_URL);
7270

7371
client.subscribe('example', {
7472
qos: 1
75-
}).then(function () {
76-
return client.unsubscribe('example');
77-
}).then(function () {
73+
}).then(() => client.unsubscribe('example')).then(() => {
7874
t.pass('Unsunbscribed');
7975
return client.end();
8076
});
8177
});
8278

83-
test('Calling publish should resolve once completed', function (t) {
79+
test('Calling publish should resolve once completed', t => {
8480
t.plan(1);
8581

86-
var client = AsyncMQTT.connect(SERVER_URL);
82+
const client = AsyncMQTT.connect(SERVER_URL);
8783

8884
client.publish('example', 'test', {
8985
qos: 1
90-
}).then(function () {
86+
}).then(() => {
9187
t.pass('Published');
9288
return client.end();
9389
});
@@ -96,17 +92,17 @@ function runTests () {
9692

9793
// Taken from MQTT.js tests
9894
function buildServer () {
99-
return new Server(function (client) {
100-
client.on('connect', function (packet) {
101-
if ('invalid' === packet.clientId) {
95+
return new Server(client => {
96+
client.on('connect', ({clientId}) => {
97+
if ('invalid' === clientId) {
10298
client.connack({returnCode: 2})
10399
} else {
104100
client.connack({returnCode: 0})
105101
}
106102
})
107103

108-
client.on('publish', function (packet) {
109-
setImmediate(function () {
104+
client.on('publish', packet => {
105+
setImmediate(() => {
110106
switch (packet.qos) {
111107
case 0:
112108
break
@@ -122,33 +118,31 @@ function buildServer () {
122118
})
123119
})
124120

125-
client.on('pubrel', function (packet) {
121+
client.on('pubrel', packet => {
126122
client.pubcomp(packet)
127123
})
128124

129-
client.on('pubrec', function (packet) {
125+
client.on('pubrec', packet => {
130126
client.pubrel(packet)
131127
})
132128

133-
client.on('pubcomp', function () {
129+
client.on('pubcomp', () => {
134130
// Nothing to be done
135131
})
136132

137-
client.on('subscribe', function (packet) {
133+
client.on('subscribe', ({messageId, subscriptions}) => {
138134
client.suback({
139-
messageId: packet.messageId,
140-
granted: packet.subscriptions.map(function (e) {
141-
return e.qos
142-
})
135+
messageId: messageId,
136+
granted: subscriptions.map(({qos}) => qos)
143137
})
144138
})
145139

146-
client.on('unsubscribe', function (packet) {
140+
client.on('unsubscribe', packet => {
147141
client.unsuback(packet)
148142
})
149143

150-
client.on('pingreq', function () {
144+
client.on('pingreq', () => {
151145
client.pingresp()
152146
})
153-
})
147+
});
154148
}

0 commit comments

Comments
 (0)