Skip to content

Commit 8a0bb2d

Browse files
author
RyLee Harrison
committed
added async promise logic
1 parent a41655f commit 8a0bb2d

File tree

2 files changed

+72
-60
lines changed

2 files changed

+72
-60
lines changed

.eslintrc.json

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{
22
"env": {
3+
"es6": true,
34
"browser": true,
45
"node": true,
56
"mocha": true
67
},
8+
9+
"parserOptions": {
10+
"ecmaVersion": 2017
11+
},
12+
713
"rules": {
814
"quotes": [
915
1,
@@ -21,7 +27,9 @@
2127
"indent": [
2228
2,
2329
2,
24-
{"SwitchCase": 1}
30+
{
31+
"SwitchCase": 1
32+
}
2533
],
2634
"brace-style": [
2735
2,
@@ -46,8 +54,10 @@
4654
"always"
4755
],
4856
"keyword-spacing": [
49-
2,
50-
{"after": true}
57+
2,
58+
{
59+
"after": true
60+
}
5161
],
5262
"space-before-blocks": 2,
5363
"spaced-comment": [
@@ -59,9 +69,9 @@
5969
],
6070
"markers": [
6171
"eslint",
62-
"jshint",
72+
"jshint",
6373
"global"
64-
]
74+
]
6575
}
6676
],
6777
"strict": [
@@ -72,4 +82,4 @@
7282
"camelcase": 0,
7383
"new-cap": 0
7484
}
75-
}
85+
}

index.js

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,106 @@
1+
'use strict'
2+
13
const mqtt = require('mqtt');
24

35
const RegularClientPrototype = mqtt.MqttClient.prototype;
46

57
class AsyncClient {
6-
constructor(client) {
7-
this.client = client
8+
constructor (client) {
9+
this._client = client;
810
}
911

10-
set handleMessage(newHandler) {
11-
this.client.handleMessage = newHandler;
12+
set handleMessage (newHandler) {
13+
this._client.handleMessage = newHandler;
1214
}
1315

14-
get handleMessage() {
15-
return this.client.handleMessage;
16+
get handleMessage () {
17+
return this._client.handleMessage;
1618
}
1719

18-
publish(...args) {
19-
return Promise.resolve(this.client.publish(...args))
20-
}
21-
22-
subscribe(...args) {
23-
return Promise.resolve(this.client.subscribe(...args))
20+
async publish (...args) {
21+
return this._client.publish(...args);
2422
}
25-
26-
unsubscribe(...args) {
27-
return Promise.resolve(this.client.unsubscribe(...args))
23+
24+
async subscribe (...args) {
25+
return this._client.subscribe(...args)
2826
}
2927

30-
end(...args) {
31-
return Promise.resolve(this.client.end(...args))
28+
async unsubscribe (...args) {
29+
return this._client.unsubscribe(...args);
3230
}
3331

34-
addListener(...args) {
35-
this.client.addListener(...args);
32+
async end (...args) {
33+
return this._client.end(...args);
3634
}
3735

38-
emit(...args) {
39-
this.client.emit(...args);
36+
addListener (...args) {
37+
return this._client.addListener(...args);
4038
}
4139

42-
eventNames(...args) {
43-
this.client.eventNames(...args);
40+
emit (...args) {
41+
return this._client.emit(...args);
4442
}
4543

46-
getMaxListeners(...args) {
47-
this.client.getMaxListeners(...args);
44+
eventNames (...args) {
45+
return this._client.eventNames(...args);
4846
}
4947

50-
listenerCount(...args) {
51-
this.client.listenerCount(...args);
48+
getMaxListeners (...args) {
49+
return this._client.getMaxListeners(...args);
5250
}
5351

54-
listeners(...args) {
55-
this.client.listeners(...args);
52+
listenerCount (...args) {
53+
return this._client.listenerCount(...args);
5654
}
5755

58-
off(...args) {
59-
this.client.off(...args);
56+
listeners (...args) {
57+
return this._client.listeners(...args);
6058
}
6159

62-
on(...args) {
63-
this.client.on(...args);
60+
off (...args) {
61+
return this._client.off(...args);
6462
}
6563

66-
once(...args) {
67-
this.client.once(...args);
64+
on (...args) {
65+
return this._client.on(...args);
6866
}
6967

70-
prependListener(...args) {
71-
this.client.prependListener(...args);
68+
once (...args) {
69+
return this._client.once(...args);
7270
}
7371

74-
prependOnceListener(...args) {
75-
this.client.prependOnceListener(...args);
72+
prependListener (...args) {
73+
return this._client.prependListener(...args);
7674
}
7775

78-
removeAllListeners(...args) {
79-
this.client.removeAllListeners(...args);
76+
prependOnceListener (...args) {
77+
return this._client.prependOnceListener(...args);
8078
}
8179

82-
removeListener(...args) {
83-
this.client.removeListener(...args);
80+
removeAllListeners (...args) {
81+
return this._client.removeAllListeners(...args);
8482
}
8583

86-
setMaxListeners(...args) {
87-
this.client.setMaxListeners(...args);
84+
removeListener (...args) {
85+
return this._client.removeListener(...args);
8886
}
8987

90-
rawListeners(...args) {
91-
this.client.rawListeners(...args);
88+
setMaxListeners (...args) {
89+
return this._client.setMaxListeners(...args);
9290
}
93-
};
9491

95-
const connect = (brokerUrl, opts = {}) => {
96-
const client = mqtt.connect(brokerUrl, opts);
97-
const asyncClient = new AsyncClient(client);
98-
return asyncClient
92+
rawListeners (...args) {
93+
return this._client.rawListeners(...args);
94+
}
9995
}
10096

97+
10198
module.exports = {
102-
connect,
99+
connect (brokerURL, opts) {
100+
const client = mqtt.connect(brokerURL, opts);
101+
const asyncClient = new AsyncClient(client);
102+
103+
return asyncClient;
104+
},
103105
AsyncClient
104-
}
106+
};

0 commit comments

Comments
 (0)