Skip to content

Commit

Permalink
use compose-emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Mar 30, 2016
1 parent dcf4dc4 commit 91961f4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict'

var utils = require('./utils')
var AppBase = require('app-base').AppBase
var AppBase = require('compose-emitter').ComposeEmitter

/**
* > Initialize `AsyncSimpleIterator` with `options`.
Expand Down Expand Up @@ -54,8 +54,11 @@ function AsyncSimpleIterator (options) {
return new AsyncSimpleIterator(options)
}
this.defaultOptions(options)
utils.Emitter(this)
AppBase.call(this)
AppBase.call(this, this.options)
this.on = this.compose('on')
this.off = this.compose('off')
this.once = this.compose('once')
this.emit = this.compose('emit')
}

AppBase.extend(AsyncSimpleIterator)
Expand All @@ -69,7 +72,10 @@ AppBase.extend(AsyncSimpleIterator)
*/

AppBase.define(AsyncSimpleIterator.prototype, 'defaultOptions', function defaultOptions (options) {
options = utils.extend({settle: false}, this.options, options)
options = utils.extend({
emitter: new utils.Emitter(),
settle: false
}, this.options, options)
options.settle = typeof options.settle === 'boolean' ? !!options.settle : false
this.options = options
return this
Expand Down Expand Up @@ -137,7 +143,10 @@ AppBase.define(AsyncSimpleIterator.prototype, 'wrapIterator', function wrapItera
if (typeof iterator !== 'function') {
throw new TypeError('async-simple-iterator: expect `iterator` to be function')
}
this.options = options ? utils.extend(this.options, options) : this.options
var opts = this.options
var ctx = options ? utils.extend({}, opts.context, options.context) : opts.context
this.options = options ? utils.extend({}, opts, options) : opts
this.options.context = ctx

var hooks = ['beforeEach', 'afterEach', 'error']
var len = hooks.length
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"test": "standard && node test.js"
},
"dependencies": {
"app-base": "github:tunnckocore/app-base",
"component-emitter": "^1.2.0",
"compose-emitter": "^1.0.1",
"eventemitter3": "^1.2.0",
"extend-shallow": "^2.0.1",
"is-typeof-error": "^1.1.0",
"lazy-cache": "^1.0.3",
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,27 @@ test('should be able to pass hooks through `options`', function (done) {
done()
})
})

test('should bind correct context (passed from constructor)', function (done) {
var app = new Ctor({
context: {a: 'b'},
beforeEach: function (val) {
test.deepEqual(this, {a: 'b', c: 'd'})
}
})
// @todo
// app.on('afterEach', function (val) {
// test.deepEqual(this, {a: 'b', c: 'd'})
// console.log('after')
// })
var itarator = app.wrapIterator(function (val, next) {
next(null, val)
}, {context: {c: 'd'}})

ctrl.mapSeries(['a', 'b', 'c'], itarator, function (err) {
test.ifError(err)
test.deepEqual(values, ['a', 'b', 'c'])
test.deepEqual(results, values)
done()
})
})
2 changes: 1 addition & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require = utils // eslint-disable-line no-undef, no-native-reassign
* Lazily required module dependencies
*/

require('component-emitter', 'Emitter')
require('eventemitter3', 'Emitter')
require('extend-shallow', 'extend')
require('is-typeof-error', 'isError')
require('sliced', 'slice')
Expand Down

0 comments on commit 91961f4

Please sign in to comment.