|
| 1 | +'use strict' |
| 2 | + |
1 | 3 | const mqtt = require('mqtt'); |
2 | 4 |
|
3 | 5 | const RegularClientPrototype = mqtt.MqttClient.prototype; |
4 | 6 |
|
5 | 7 | class AsyncClient { |
6 | | - constructor(client) { |
7 | | - this.client = client |
| 8 | + constructor (client) { |
| 9 | + this._client = client; |
8 | 10 | } |
9 | 11 |
|
10 | | - set handleMessage(newHandler) { |
11 | | - this.client.handleMessage = newHandler; |
| 12 | + set handleMessage (newHandler) { |
| 13 | + this._client.handleMessage = newHandler; |
12 | 14 | } |
13 | 15 |
|
14 | | - get handleMessage() { |
15 | | - return this.client.handleMessage; |
| 16 | + get handleMessage () { |
| 17 | + return this._client.handleMessage; |
16 | 18 | } |
17 | 19 |
|
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); |
24 | 22 | } |
25 | | - |
26 | | - unsubscribe(...args) { |
27 | | - return Promise.resolve(this.client.unsubscribe(...args)) |
| 23 | + |
| 24 | + async subscribe (...args) { |
| 25 | + return this._client.subscribe(...args) |
28 | 26 | } |
29 | 27 |
|
30 | | - end(...args) { |
31 | | - return Promise.resolve(this.client.end(...args)) |
| 28 | + async unsubscribe (...args) { |
| 29 | + return this._client.unsubscribe(...args); |
32 | 30 | } |
33 | 31 |
|
34 | | - addListener(...args) { |
35 | | - this.client.addListener(...args); |
| 32 | + async end (...args) { |
| 33 | + return this._client.end(...args); |
36 | 34 | } |
37 | 35 |
|
38 | | - emit(...args) { |
39 | | - this.client.emit(...args); |
| 36 | + addListener (...args) { |
| 37 | + return this._client.addListener(...args); |
40 | 38 | } |
41 | 39 |
|
42 | | - eventNames(...args) { |
43 | | - this.client.eventNames(...args); |
| 40 | + emit (...args) { |
| 41 | + return this._client.emit(...args); |
44 | 42 | } |
45 | 43 |
|
46 | | - getMaxListeners(...args) { |
47 | | - this.client.getMaxListeners(...args); |
| 44 | + eventNames (...args) { |
| 45 | + return this._client.eventNames(...args); |
48 | 46 | } |
49 | 47 |
|
50 | | - listenerCount(...args) { |
51 | | - this.client.listenerCount(...args); |
| 48 | + getMaxListeners (...args) { |
| 49 | + return this._client.getMaxListeners(...args); |
52 | 50 | } |
53 | 51 |
|
54 | | - listeners(...args) { |
55 | | - this.client.listeners(...args); |
| 52 | + listenerCount (...args) { |
| 53 | + return this._client.listenerCount(...args); |
56 | 54 | } |
57 | 55 |
|
58 | | - off(...args) { |
59 | | - this.client.off(...args); |
| 56 | + listeners (...args) { |
| 57 | + return this._client.listeners(...args); |
60 | 58 | } |
61 | 59 |
|
62 | | - on(...args) { |
63 | | - this.client.on(...args); |
| 60 | + off (...args) { |
| 61 | + return this._client.off(...args); |
64 | 62 | } |
65 | 63 |
|
66 | | - once(...args) { |
67 | | - this.client.once(...args); |
| 64 | + on (...args) { |
| 65 | + return this._client.on(...args); |
68 | 66 | } |
69 | 67 |
|
70 | | - prependListener(...args) { |
71 | | - this.client.prependListener(...args); |
| 68 | + once (...args) { |
| 69 | + return this._client.once(...args); |
72 | 70 | } |
73 | 71 |
|
74 | | - prependOnceListener(...args) { |
75 | | - this.client.prependOnceListener(...args); |
| 72 | + prependListener (...args) { |
| 73 | + return this._client.prependListener(...args); |
76 | 74 | } |
77 | 75 |
|
78 | | - removeAllListeners(...args) { |
79 | | - this.client.removeAllListeners(...args); |
| 76 | + prependOnceListener (...args) { |
| 77 | + return this._client.prependOnceListener(...args); |
80 | 78 | } |
81 | 79 |
|
82 | | - removeListener(...args) { |
83 | | - this.client.removeListener(...args); |
| 80 | + removeAllListeners (...args) { |
| 81 | + return this._client.removeAllListeners(...args); |
84 | 82 | } |
85 | 83 |
|
86 | | - setMaxListeners(...args) { |
87 | | - this.client.setMaxListeners(...args); |
| 84 | + removeListener (...args) { |
| 85 | + return this._client.removeListener(...args); |
88 | 86 | } |
89 | 87 |
|
90 | | - rawListeners(...args) { |
91 | | - this.client.rawListeners(...args); |
| 88 | + setMaxListeners (...args) { |
| 89 | + return this._client.setMaxListeners(...args); |
92 | 90 | } |
93 | | -}; |
94 | 91 |
|
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 | + } |
99 | 95 | } |
100 | 96 |
|
| 97 | + |
101 | 98 | 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 | + }, |
103 | 105 | AsyncClient |
104 | | -} |
| 106 | +}; |
0 commit comments