Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated: docs, test, source #11

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ logs
*.log
npm-debug.log*

*.lock

# Runtime data
pids
*.pid
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ The API is the same as [MQTT.js](https://github.com/mqttjs/MQTT.js#api), except
## Example

```javascript
var MQTT = require("async-mqtt");
const MQTT = require("async-mqtt");

var client = MQTT.connect("tcp://somehost.com:1883");
const client = MQTT.connect("tcp://somehost.com:1883");

// When passing async functions as event listeners, make sure to have a try catch block
client.on("connect", doStuff);

async function doStuff() {
const doStuff = async () => {

console.log("Starting");
try {
Expand All @@ -53,18 +52,20 @@ async function doStuff() {
process.exit();
}
}

client.on("connect", doStuff);
```

## Wrapping existing client

```javascript
var AsyncClient = require("async-mqtt").AsyncClient;
const { AsyncClient } = require("async-mqtt");

var client = getRegularMQTTClientFromSomewhere();
const client = getRegularMQTTClientFromSomewhere();

var asyncClient = new AsyncClient(client);
const asyncClient = new AsyncClient(client);

asyncClient.publish("foo/bar", "baz").then(function(){
asyncClient.publish("foo/bar", "baz").then(() => {
console.log("We async now");
return asyncClient.end();
});
Expand Down
167 changes: 91 additions & 76 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,104 @@
'use strict';
var mqtt = require('mqtt');
var inArray = require('in-array');

var RegularClientPrototype = mqtt.MqttClient.prototype;

var ASYNC_METHODS = ['publish',
'subscribe',
'unsubscribe',
'end'
];

var SYNC_METHODS = [
'addListener',
'emit',
'eventNames',
'getMaxListeners',
'listenerCount',
'listeners',
'off',
'on',
'once',
'prependListener',
'prependOnceListener',
'removeAllListeners',
'removeListener',
'setMaxListeners',
'rawListeners'
];
const mqtt = require('mqtt');

module.exports = {
connect: connect,
AsyncClient: AsyncClient
};
const RegularClientPrototype = mqtt.MqttClient.prototype;

function connect (brokerURL, opts) {
var client = mqtt.connect(brokerURL, opts);
class AsyncClient {
constructor(client) {
this.client = client
}

var asyncClient = new AsyncClient(client);
set handleMessage(newHandler) {
this.client.handleMessage = newHandler;
}

return asyncClient;
}
get handleMessage() {
return this.client.handleMessage;
}

function AsyncClient (client) {
this._client = client;
}
publish(...args) {
return Promise.resolve(this.client.publish(...args))
}

subscribe(...args) {
return Promise.resolve(this.client.subscribe(...args))
}

unsubscribe(...args) {
return Promise.resolve(this.client.unsubscribe(...args))
}

AsyncClient.prototype = {
set handleMessage (newHandler) {
this._client.handleMessage = newHandler;
},
get handleMessage () {
return this._client.handleMessage;
end(...args) {
return Promise.resolve(this.client.end(...args))
}
};

ASYNC_METHODS.forEach(defineAsync);
SYNC_METHODS.forEach(definePassthrough);
addListener(...args) {
this.client.addListener(...args);
}

function definePassthrough (name) {
AsyncClient.prototype[name] = function () {
var client = this._client;
return client[name].apply(client, arguments);
};
}
emit(...args) {
this.client.emit(...args);
}

eventNames(...args) {
this.client.eventNames(...args);
}

getMaxListeners(...args) {
this.client.getMaxListeners(...args);
}

listenerCount(...args) {
this.client.listenerCount(...args);
}

listeners(...args) {
this.client.listeners(...args);
}

off(...args) {
this.client.off(...args);
}

on(...args) {
this.client.on(...args);
}

once(...args) {
this.client.once(...args);
}

prependListener(...args) {
this.client.prependListener(...args);
}

prependOnceListener(...args) {
this.client.prependOnceListener(...args);
}

function defineAsync (name) {
AsyncClient.prototype[name] = function asyncMethod () {
var client = this._client;
var args = [];
var length = arguments.length;
var i = 0;
for (i; i < length; i++)
args.push(arguments[i]);

return new Promise(function (resolve, reject) {
args.push(makeCallback(resolve, reject));
client[name].apply(client, args);
});
};
removeAllListeners(...args) {
this.client.removeAllListeners(...args);
}

removeListener(...args) {
this.client.removeListener(...args);
}

setMaxListeners(...args) {
this.client.setMaxListeners(...args);
}

rawListeners(...args) {
this.client.rawListeners(...args);
}
};

const connect = (brokerUrl, opts = {}) => {
const client = mqtt.connect(brokerUrl, opts);
const asyncClient = new AsyncClient(client);
return asyncClient
}

function makeCallback (resolve, reject) {
return function (err, data) {
if (err)
reject(err);
else resolve(data);
};
module.exports = {
connect,
AsyncClient
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"homepage": "https://github.com/mqttjs/async-mqtt#readme",
"dependencies": {
"in-array": "^0.1.2",
"mqtt": "^2.3.1"
},
"devDependencies": {
Expand Down
Loading