Skip to content

Commit

Permalink
Remove lodash, hoek and fastparellel and use built in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Mar 20, 2018
1 parent de126c6 commit c614ba5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 40 deletions.
3 changes: 1 addition & 2 deletions packages/hemera/lib/add.js
Expand Up @@ -9,7 +9,6 @@
*
*/

const _ = require('lodash')
const Series = require('fastseries')

/**
Expand Down Expand Up @@ -52,7 +51,7 @@ class Add {
* @memberOf Add
*/
use(handler) {
if (_.isArray(handler)) {
if (Array.isArray(handler)) {
handler.forEach(h => this._use(h))
} else {
this._use(handler)
Expand Down
3 changes: 1 addition & 2 deletions packages/hemera/lib/extensionManager.js
Expand Up @@ -9,7 +9,6 @@
*
*/

const _ = require('lodash')
const Constants = require('./constants')
const Errors = require('./errors')

Expand Down Expand Up @@ -61,7 +60,7 @@ class ExtensionManager {
* @memberOf Extension
*/
add(type, handler) {
if (_.isArray(handler)) {
if (Array.isArray(handler)) {
handler.forEach(h => this._add(type, h))
} else {
this._add(type, handler)
Expand Down
63 changes: 30 additions & 33 deletions packages/hemera/lib/index.js
Expand Up @@ -16,13 +16,11 @@ const EventEmitter = require('events')
const Bloomrun = require('bloomrun')
const Errio = require('errio')
const Heavy = require('heavy')
const _ = require('lodash')
const Pino = require('pino')
const TinySonic = require('tinysonic')
const SuperError = require('super-error')
const Joi = require('joi')
const Avvio = require('avvio')
const Hoek = require('hoek')
const Series = require('fastseries')

const Errors = require('./errors')
Expand Down Expand Up @@ -388,7 +386,7 @@ class Hemera extends EventEmitter {
* @memberOf Hemera
*/
ext(type, handler) {
if (!_.isFunction(handler)) {
if (typeof handler !== 'function') {
let error = new Errors.HemeraError(Constants.INVALID_EXTENSION_HANDLER, {
type,
handler
Expand Down Expand Up @@ -497,8 +495,7 @@ class Hemera extends EventEmitter {
* @memberof Hemera
*/
use(plugin, opts) {
let pluginOpts = Hoek.clone(plugin[Symbols.pluginOptions] || {})
pluginOpts = Object.assign(pluginOpts, opts)
let pluginOpts = Object.assign({}, plugin[Symbols.pluginOptions], opts)
this.register(plugin, pluginOpts)
return this._avvio
}
Expand Down Expand Up @@ -577,7 +574,7 @@ class Hemera extends EventEmitter {
}

// callback style
if (_.isFunction(cb)) {
if (typeof cb === 'function') {
ready(err => {
if (err) {
this.log.error(err)
Expand Down Expand Up @@ -837,10 +834,7 @@ class Hemera extends EventEmitter {
}

// when sid was passed
if (_.isNumber(topic)) {
self._transport.unsubscribe(topic, maxMessages)
return true
} else {
if (typeof topic === 'string') {
// when topic name was passed
const subId = self._topics[topic]

Expand All @@ -850,6 +844,9 @@ class Hemera extends EventEmitter {
this.cleanTopic(topic)
return true
}
} else {
self._transport.unsubscribe(topic, maxMessages)
return true
}

return false
Expand All @@ -865,7 +862,7 @@ class Hemera extends EventEmitter {
// release topic so we can add it again
delete this._topics[topic]
// remove pattern which belongs to the topic
_.each(this.list(), add => {
this.list().forEach(add => {
if (add.pattern.topic === topic) {
this.router.remove(add.pattern)
}
Expand All @@ -881,17 +878,17 @@ class Hemera extends EventEmitter {
* @memberOf Hemera
*/
add(definition, cb) {
// check for use quick syntax for JSON objects
if (_.isString(definition)) {
definition = TinySonic(definition)
}

if (!_.isObject(definition)) {
if (!definition) {
let error = new Errors.HemeraError(Constants.ADD_PATTERN_REQUIRED)
this.log.error(error)
throw error
}

// check for use quick syntax for JSON objects
if (typeof definition === 'string') {
definition = TinySonic(definition)
}

// topic is needed to subscribe on a subject in NATS
if (!definition.topic) {
let error = new Errors.HemeraError(Constants.NO_TOPIC_TO_SUBSCRIBE, {
Expand Down Expand Up @@ -1122,17 +1119,17 @@ class Hemera extends EventEmitter {
* @memberOf Hemera
*/
act(pattern, cb) {
// check for use quick syntax for JSON objects
if (_.isString(pattern)) {
pattern = TinySonic(pattern)
}

if (!_.isObject(pattern)) {
if (!pattern) {
let error = new Errors.HemeraError(Constants.ACT_PATTERN_REQUIRED)
this.log.error(error)
throw error
}

// check for use quick syntax for JSON objects
if (typeof pattern === 'string') {
pattern = TinySonic(pattern)
}

// create new execution context
let hemera = this.createContext()
hemera._pattern = pattern
Expand Down Expand Up @@ -1375,7 +1372,9 @@ class Hemera extends EventEmitter {
* @memberof Hemera
*/
removeAll() {
_.each(this._topics, (val, key) => this.remove(key))
for (const key in this._topics) {
this.remove(key)
}
}

/**
Expand All @@ -1387,16 +1386,14 @@ class Hemera extends EventEmitter {
const self = this

// callback style
if (_.isFunction(cb)) {
if (typeof cb === 'function') {
self.shutdown((err, instance, done) => {
instance._onClose(() => {
if (_.isFunction(cb)) {
if (err) {
self.log.error(err)
cb(err)
} else {
cb()
}
if (err) {
self.log.error(err)
cb(err)
} else {
cb()
}
done(err)
})
Expand Down Expand Up @@ -1436,7 +1433,7 @@ class Hemera extends EventEmitter {
self._heavy.stop()
// Does not throw an issue when connection is not available
self._transport.close()
if (_.isFunction(cb)) {
if (typeof cb === 'function') {
cb()
}
})
Expand Down
3 changes: 0 additions & 3 deletions packages/hemera/package.json
Expand Up @@ -43,12 +43,9 @@
"bloomrun": "4.1.x",
"errio": "1.2.x",
"fast-safe-stringify": "2.0.x",
"fastparallel": "2.3.x",
"fastseries": "1.7.x",
"heavy": "4.0.x",
"hoek": "4.2.x",
"joi": "11.1.x",
"lodash": "4.17.x",
"pino": "4.13.0",
"super-error": "2.2.x",
"tinysonic": "1.3.x"
Expand Down
64 changes: 64 additions & 0 deletions test/hemera/plugin.spec.js
Expand Up @@ -105,6 +105,70 @@ describe('Plugin interface', function() {
})
})

it('Should merge default plugin options', function(done) {
const nats = require('nats').connect(authUrl)

const hemera = new Hemera(nats)

// Plugin
let myPlugin = function(hemera, options, done) {
expect(options).to.be.equals({
port: 3000,
host: '127.0.0.1',
errors: {
propBlacklist: []
},
pattern: {}
})

hemera.add(
{
topic: 'math',
cmd: 'add'
},
(resp, cb) => {
cb(null, {
result: resp.a + resp.b
})
}
)

done()
}

myPlugin[Symbol.for('name')] = 'myPlugin'
myPlugin[Symbol.for('options')] = {
port: 3000,
host: '127.0.0.1',
errors: {
propBlacklist: ['stack']
},
pattern: {}
}

hemera.use(myPlugin, {
errors: {
propBlacklist: []
}
})

hemera.ready(() => {
hemera.act(
{
topic: 'math',
cmd: 'add',
a: 1,
b: 2
},
(err, resp) => {
expect(err).to.be.not.exists()
expect(resp).not.to.be.equals(3)
hemera.close(done)
}
)
})
})

it('Should load plugins only on ready', function(done) {
const nats = require('nats').connect(authUrl)

Expand Down

0 comments on commit c614ba5

Please sign in to comment.