Skip to content

Commit

Permalink
Merge branch 'release-4.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 12, 2017
2 parents 4d27e61 + 6ae57f9 commit a642b37
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="4.0.4"></a>
## [4.0.4](https://github.com/poppinss/adonis-fold/compare/v4.0.3...v4.0.4) (2017-09-12)


### Bug Fixes

* **ioc:** fix execution cycle of extend calls ([bc2c084](https://github.com/poppinss/adonis-fold/commit/bc2c084))



<a name="4.0.3"></a>
## [4.0.3](https://github.com/poppinss/adonis-fold/compare/v4.0.2...v4.0.3) (2017-09-06)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/fold",
"version": "4.0.3",
"version": "4.0.4",
"description": "Dependency manager and ioc container for your next NodeJs application",
"main": "index.js",
"directories": {
Expand Down
47 changes: 36 additions & 11 deletions src/Ioc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class Ioc {
* @type {Map}
*/
this._fakes = new Map()

/**
* Reference to all extend calls. Extend calls
* are executed right after all providers
* have been booted
*
* @type {Object}
*/
this._extendCalls = []
}

/**
Expand Down Expand Up @@ -562,19 +571,35 @@ class Ioc {
* })
* ```
*/
extend (namespace, key, closure, ...options) {
if (!this._hasManager(namespace)) {
const message = `${namespace} cannot be extended, since their is no public interface to extend`
throw GE.InvalidArgumentException.invoke(message, 500, 'E_CANNOT_EXTEND_BINDING')
}
extend (...args) {
this._extendCalls.push(args)
}

if (typeof (closure) !== 'function') {
throw GE.InvalidArgumentException.invalidParameter('Ioc.extend expects 3rd parameter to be a closure', closure)
}
/**
* Executes all extend calls in sequence. Successfully
* executed extend calls will be removed from the
* array, so that they are not executed again.
*
* @method executeExtendCalls
*
* @return {void}
*/
executeExtendCalls () {
_.remove(this._extendCalls, ([ namespace, key, closure, ...options ]) => {
if (!this._hasManager(namespace)) {
const message = `${namespace} cannot be extended, since their is no public interface to extend`
throw GE.InvalidArgumentException.invoke(message, 500, 'E_CANNOT_EXTEND_BINDING')
}

if (typeof (closure) !== 'function') {
throw GE.InvalidArgumentException.invalidParameter('Ioc.extend expects 3rd parameter to be a closure', closure)
}

const resolvedValue = closure(this)
debug('extending %s namespace %j', namespace, { key, options: [...options] })
this._managers[namespace].extend(key, resolvedValue, ...options)
const resolvedValue = closure(this)
debug('extending %s namespace %j', namespace, { key, options: [...options] })
this._managers[namespace].extend(key, resolvedValue, ...options)
return true
})
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Registrar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class Registrar {
*/
async boot () {
await Promise.all(this._bootProviders(this._providers))
this.Ioc.executeExtendCalls()
emitter.emit(this.PROVIDERS_BOOTED)
}

Expand Down
8 changes: 6 additions & 2 deletions test/unit/ioc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ test.group('Ioc', function () {

test('should throw an exception when trying to extend a binding which does not have a manager', function () {
const ioc = new Ioc()
const fn = () => ioc.extend('App/Foo')
ioc.extend('App/Foo')
const fn = () => ioc.executeExtendCalls()
assert.throw(fn, 'E_CANNOT_EXTEND_BINDING: App/Foo cannot be extended, since their is no public interface to extend')
})

Expand All @@ -197,7 +198,8 @@ test.group('Ioc', function () {
ioc.manager('App/Foo', class Foo {
static extend () {}
})
const fn = () => ioc.extend('App/Foo', 'redis')
ioc.extend('App/Foo', 'redis')
const fn = () => ioc.executeExtendCalls()
assert.throw(fn, 'E_INVALID_PARAMETER: Ioc.extend expects 3rd parameter to be a closure')
})

Expand All @@ -216,6 +218,7 @@ test.group('Ioc', function () {
return 'I am redis'
})

ioc.executeExtendCalls()
assert.deepEqual(extendedValues, {redis: 'I am redis'})
})

Expand All @@ -234,6 +237,7 @@ test.group('Ioc', function () {
return 'I am redis'
}, 'boom')

ioc.executeExtendCalls()
assert.deepEqual(extendedValues, {options: 'boom'})
})

Expand Down

0 comments on commit a642b37

Please sign in to comment.