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

Override #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ jspm_packages

# Optional REPL history
.node_repl_history

# mac files
.DS_Store

# vim swap files
*.swp
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ app.use(function first (s1, opts, cb) {
})
```

You can also force the ovverride of the instance by passing the `override` option to *avvio*.
```js
const boot = require('avvio')
const assert = require('assert')
const server = { count: 0 }
const app = boot(server, { override: true })

app.use(function first (s, opts, cb) {
assert.notEqual(s, server)
assert(server.isPrototypeOf(s))
cb()
})
```

-------------------------------------------------------

## Acknowledgements
Expand Down
7 changes: 5 additions & 2 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function Boot (server, opts, done) {

this._server = server
this._current = []
this._forceOverride = !!opts.override

if (done) {
this.once('start', done)
Expand Down Expand Up @@ -105,6 +106,9 @@ Boot.prototype._init = function () {

// allows to override the instance of a server, given a plugin
Boot.prototype.override = function (server) {
if (this._forceOverride) {
return Object.create(server)
}
return server
}

Expand Down Expand Up @@ -201,12 +205,11 @@ function loadPlugin (toLoad, cb) {
const last = this._current[0]
// place the plugin at the top of _current
this._current.unshift(toLoad)
toLoad.exec(last && last.server || this._server, (err) => {
toLoad.exec((last && last.server) || this._server, (err) => {
if (err || !(toLoad.q.length() > 0 || toLoad.q.running() > 0)) {
// finish now, because there is nothing left to do
this._current.shift()
toLoad.finish(err, cb)
return
} else {
// finish when the queue of nested plugins to load is empty
toLoad.q.drain = () => {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
},
"homepage": "https://github.com/mcollina/avvio#readme",
"devDependencies": {
"express": "^4.14.0",
"pre-commit": "^1.1.3",
"standard": "^8.6.0",
"tap": "^9.0.0"
"express": "^4.15.2",
"pre-commit": "^1.2.2",
"standard": "^9.0.2",
"tap": "^10.3.0"
},
"dependencies": {
"fastq": "^1.4.1"
"fastq": "^1.5.0"
}
}
14 changes: 14 additions & 0 deletions test/override.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,17 @@ test('custom inheritance multiple levels with multiple heads', (t) => {
t.equal(server.count, 0)
})
})

test('force override', (t) => {
t.plan(3)

const server = { my: 'server' }
const app = boot(server, { override: true })
t.ok(app._forceOverride)

app.use(function first (s, opts, cb) {
t.notEqual(s, server)
t.ok(server.isPrototypeOf(s))
cb()
})
})