handle array of topcis

master
David Kebler 2018-04-05 15:50:39 -07:00
parent c8071ed66e
commit 5dda6b1e9e
4 changed files with 40 additions and 42 deletions

View File

@ -2,7 +2,7 @@
"name": "@uci/mqtt",
"version": "0.0.1",
"description": "mqtt client with json payloads and mqtt custom broker",
"main": "src/index.js",
"main": "src",
"scripts": {
"testw": "mocha -r @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs",
"test": "mocha -r @std/esm test/*.test.mjs",

View File

@ -1,7 +1,6 @@
import { connect } from 'async-mqtt'
import merge from 'lodash.merge'
import btc from 'better-try-catch'
import isArray from 'lodash.isarray'
import union from 'lodash.union'
import xor from 'lodash.xor'
import logger from '@uci/logger'
@ -14,7 +13,8 @@ export default class Client {
this.id = opts.id || opts.name || 'mqtt:'+ new Date().getTime()
this.url = opts.url
// subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs
this.topics = isArray(opts.topics) ? opts.topic : (opts.topics.split(',') || ['default'])
this.topics = Array.isArray(opts.topics) ? opts.topics : (opts.topics ? opts.topics.split(',') : [this.id])
console.log(this.topics)
this.opts = opts.connect || {} // see options for new mqtt.Client
// self bindings
this.connect = this.connect.bind(this)
@ -70,7 +70,7 @@ export default class Client {
async send(topics,payload,options) {
if (typeof topics !=='string'|| !isArray(topics)) {
if (typeof topics !=='string'|| !Array.isArray(topics)) {
payload = topics
topics = this.topics
}
@ -106,7 +106,7 @@ export default class Client {
this.on('message',messageProcess.bind(this))
async function messageProcess (topic,payload) {
// console.log('incoming messeage on topic', topic)
console.log('incoming messeage on topic', topic)
let packet = this._handlePayload(payload)
if (packet) await this._packetProcess (packet,topic)

View File

@ -1,12 +1,12 @@
'use strict'
import aClass from '../src'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
const
Changeme = require('../src/changeme'),
expect = require('chai').expect,
pause = require('@uci/utils').pPause
chai.use(chaiAsPromised)
const expect = chai.expect
describe(
`Testing `,
'Testing ',
function () {
hooks()
sometests()
@ -18,8 +18,7 @@ function sometests() {
it('==> test something', async function () {
let result = await someasyncfunction()
expect(result, `test failed`).to.equal('expectedresult')
await pause(1000)
expect(result, 'test failed').to.equal('expectedresult')
})
}
@ -28,8 +27,7 @@ function someothertests() {
it('==> test something', async function () {
let result = await someasyncfunction()
expect(result, `test failed`).to.equal('expectedresult')
await pause(1000)
expect(result, 'test failed').to.equal('expectedresult')
})
}

View File

@ -1,9 +1,9 @@
'use strict'
const chai = require('chai'),
chaiAsPromised = require("chai-as-promised"),
lib = require('../')
import changeme from '../src/changeme'
import path from 'path'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
const { assert } = chai;
chai.use(chaiAsPromised);
const expect = chai.expect
@ -12,8 +12,8 @@ describe('Promise Stuff - ', function () {
it('Can test a promise', function () {
return expect(lib.apromise).to.eventually.equal('some promise hey')
return Promise.resolve().then(() => expect(lib.apromise).to.eventually.equal('some promise hey'))
return expect(changeme.apromise).to.eventually.equal('some promise hey')
return Promise.resolve().then(() => expect(changeme.apromise).to.eventually.equal('some promise hey'))
})
})