|
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 | +'use strict' |
30 | 2 |
|
31 | | -module.exports = { |
32 | | - connect: connect, |
33 | | - AsyncClient: AsyncClient |
34 | | -}; |
| 3 | +const mqtt = require('mqtt'); |
35 | 4 |
|
36 | | -function connect (brokerURL, opts) { |
37 | | - var client = mqtt.connect(brokerURL, opts); |
| 5 | +const RegularClientPrototype = mqtt.MqttClient.prototype; |
38 | 6 |
|
39 | | - var asyncClient = new AsyncClient(client); |
40 | | - |
41 | | - return asyncClient; |
42 | | -} |
43 | | - |
44 | | -function AsyncClient (client) { |
45 | | - this._client = client; |
46 | | -} |
| 7 | +class AsyncClient { |
| 8 | + constructor (client) { |
| 9 | + this._client = client; |
| 10 | + } |
47 | 11 |
|
48 | | -AsyncClient.prototype = { |
49 | 12 | set handleMessage (newHandler) { |
50 | 13 | this._client.handleMessage = newHandler; |
51 | | - }, |
| 14 | + } |
| 15 | + |
52 | 16 | get handleMessage () { |
53 | 17 | return this._client.handleMessage; |
54 | 18 | } |
55 | | -}; |
56 | 19 |
|
57 | | -ASYNC_METHODS.forEach(defineAsync); |
58 | | -SYNC_METHODS.forEach(definePassthrough); |
| 20 | + publish (...args) { |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + this._client.publish(...args, (err, result) => { |
| 23 | + if (err) reject(err) |
| 24 | + else resolve(result) |
| 25 | + }) |
| 26 | + }) |
| 27 | + } |
59 | 28 |
|
60 | | -function definePassthrough (name) { |
61 | | - AsyncClient.prototype[name] = function () { |
62 | | - var client = this._client; |
63 | | - return client[name].apply(client, arguments); |
64 | | - }; |
65 | | -} |
| 29 | + subscribe (...args) { |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + this._client.subscribe(...args, (err, result) => { |
| 32 | + if (err) reject(err) |
| 33 | + else resolve(result) |
| 34 | + }) |
| 35 | + }) |
| 36 | + } |
66 | 37 |
|
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 | | - }; |
81 | | -} |
| 38 | + unsubscribe (...args) { |
| 39 | + return new Promise((resolve, reject) => { |
| 40 | + this._client.unsubscribe(...args, (err, result) => { |
| 41 | + if (err) reject(err) |
| 42 | + else resolve(result) |
| 43 | + }) |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + end (...args) { |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + this._client.end(...args, (err, result) => { |
| 50 | + if (err) reject(err) |
| 51 | + else resolve(result) |
| 52 | + }) |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + addListener (...args) { |
| 57 | + return this._client.addListener(...args); |
| 58 | + } |
| 59 | + |
| 60 | + emit (...args) { |
| 61 | + return this._client.emit(...args); |
| 62 | + } |
| 63 | + |
| 64 | + eventNames (...args) { |
| 65 | + return this._client.eventNames(...args); |
| 66 | + } |
| 67 | + |
| 68 | + getMaxListeners (...args) { |
| 69 | + return this._client.getMaxListeners(...args); |
| 70 | + } |
82 | 71 |
|
83 | | -function makeCallback (resolve, reject) { |
84 | | - return function (err, data) { |
85 | | - if (err) |
86 | | - reject(err); |
87 | | - else resolve(data); |
88 | | - }; |
| 72 | + listenerCount (...args) { |
| 73 | + return this._client.listenerCount(...args); |
| 74 | + } |
| 75 | + |
| 76 | + listeners (...args) { |
| 77 | + return this._client.listeners(...args); |
| 78 | + } |
| 79 | + |
| 80 | + off (...args) { |
| 81 | + return this._client.off(...args); |
| 82 | + } |
| 83 | + |
| 84 | + on (...args) { |
| 85 | + return this._client.on(...args); |
| 86 | + } |
| 87 | + |
| 88 | + once (...args) { |
| 89 | + return this._client.once(...args); |
| 90 | + } |
| 91 | + |
| 92 | + prependListener (...args) { |
| 93 | + return this._client.prependListener(...args); |
| 94 | + } |
| 95 | + |
| 96 | + prependOnceListener (...args) { |
| 97 | + return this._client.prependOnceListener(...args); |
| 98 | + } |
| 99 | + |
| 100 | + removeAllListeners (...args) { |
| 101 | + return this._client.removeAllListeners(...args); |
| 102 | + } |
| 103 | + |
| 104 | + removeListener (...args) { |
| 105 | + return this._client.removeListener(...args); |
| 106 | + } |
| 107 | + |
| 108 | + setMaxListeners (...args) { |
| 109 | + return this._client.setMaxListeners(...args); |
| 110 | + } |
| 111 | + |
| 112 | + rawListeners (...args) { |
| 113 | + return this._client.rawListeners(...args); |
| 114 | + } |
89 | 115 | } |
| 116 | + |
| 117 | + |
| 118 | +module.exports = { |
| 119 | + connect (brokerURL, opts) { |
| 120 | + const client = mqtt.connect(brokerURL, opts); |
| 121 | + const asyncClient = new AsyncClient(client); |
| 122 | + |
| 123 | + return asyncClient; |
| 124 | + }, |
| 125 | + AsyncClient |
| 126 | +}; |
0 commit comments