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

Updated encapsulation handling #23

Merged
merged 9 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ all their dependencies. The `'start'` event is not emitted yet.
The callback changes basing on the parameters your are giving:
1. If one parameter is given to the callback, that parameter will be the `error` object.
2. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
3. If three parameters are given to the callback, the first will be the `error` object, the second will be the `context` and the third the `done` callback.
3. If three parameters are given to the callback, the first will be the `error` object, the second will be the top level `context` if you didn't provide the server, otherwise an encapsulated `context`, and the third the `done` callback.

```js
const server = {}
Expand Down Expand Up @@ -231,7 +231,7 @@ Calls a function after all the plugins and `after` call are completed, but befor
The callback changes basing on the parameters your are giving:
1. If one parameter is given to the callback, that parameter will be the `error` object.
2. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
3. If three parameters are given to the callback, the first will be the `error` object, the second will be the `context` and the third the `done` callback.
3. If three parameters are given to the callback, the first will be the `error` object, the second will be the top level `context` if you didn't provide the server, otherwise an encapsulated `context`, and the third the `done` callback.

```js
const server = {}
Expand Down Expand Up @@ -325,7 +325,7 @@ Registers a new callback that will be fired once then `close` api is called.

The callback changes basing on the parameters your are giving:
1. If one parameter is given to the callback, that parameter will be the `context`.
2. If two parameters are given to the callback, the first will be the `context`, the second will be the `done` callback.
2. If two parameters are given to the callback, the first will be the top level `context` if you didn't provide the server, otherwise an encapsulated `context`, the second will be the `done` callback.

```js
const server = {}
Expand Down Expand Up @@ -355,7 +355,7 @@ Starts the shotdown procedure, the callback is called once all the registered ca
The callback changes basing on the parameters your are giving:
1. If one parameter is given to the callback, that parameter will be the `error` object.
2. If two parameters are given to the callback, the first will be the `error` object, the second will be the `done` callback.
3. If three parameters are given to the callback, the first will be the `error` object, the second will be the `context` and the third the `done` callback.
3. If three parameters are given to the callback, the first will be the `error` object, the second will be the top level `context` if you didn't provide the server, otherwise an encapsulated `context`, and the third the `done` callback.

```js
const server = {}
Expand Down
72 changes: 30 additions & 42 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ function wrap (server, opts, instance) {
}

server[afterKey] = function (func) {
instance.after(encapsulateThreeParamServer(func, this, true))
instance.after(encapsulateThreeParam(func, this))
return this
}

server[readyKey] = function (func) {
instance.ready(encapsulateThreeParamServer(func, this, true))
instance.ready(encapsulateThreeParam(func, this))
return this
}

server[onCloseKey] = function (func) {
instance.onClose(encapsulateTwoParamServer(func, this))
instance.onClose(encapsulateTwoParam(func, this))
return this
}

server[closeKey] = function (func) {
instance.close(encapsulateThreeParamServer(func, this))
instance.close(encapsulateThreeParam(func, this))
return this
}
}
Expand Down Expand Up @@ -177,21 +177,14 @@ Boot.prototype.after = function (func, cb) {
this.use(_after.bind(this), cb)

function _after (s, opts, done) {
callWithCbOrNextTick.call(
this,
encapsulateThreeParamAvvio(func, this),
done
)
callWithCbOrNextTick.call(this, func, done)
}

return this
}

Boot.prototype.onClose = function (func) {
this._closeQ.unshift(
encapsulateTwoParamAvvio(func, this),
callback.bind(this)
)
this._closeQ.unshift(func, callback.bind(this))

function callback (err) {
if (err) this._error = err
Expand All @@ -203,49 +196,58 @@ Boot.prototype.onClose = function (func) {
Boot.prototype.close = function (func) {
this._error = null
if (func) {
this._closeQ.push(encapsulateThreeParamAvvio(func, this))
this._closeQ.push(func)
this._thereIsCloseCb = true
}
process.nextTick(this._closeQ.resume.bind(this._closeQ))
}

Boot.prototype.ready = function (func) {
this._readyQ.push(encapsulateThreeParamAvvio(func, this))
this._readyQ.push(func)
return this
}

function noop () {}

function callWithCbOrNextTick (func, cb, context) {
context = this._server
var err = this._error

// with this the error will appear just in the next after/ready callback
this._error = null
func(err, cb)

if (func.length === 0 || func.length === 1) {
func(err)
process.nextTick(cb)
} else if (func.length === 2) {
func(err, cb)
} else {
func(err, context, cb)
}
}

function closeWithCbOrNextTick (func, cb, context) {
context = this._server
if (this._closeQ.length() === 0 && this._thereIsCloseCb) {
func(this._error, cb)
if (func.length === 0 || func.length === 1) {
func(this._error)
process.nextTick(cb)
} else if (func.length === 2) {
func(this._error, cb)
} else {
func(this._error, context, cb)
}
} else {
func(context, cb)
}
}

function encapsulateTwoParamAvvio (func, that) {
return _encapsulateTwoParam.bind(that)
function _encapsulateTwoParam (context, cb) {
if (func.length === 0 || func.length === 1) {
func(this._server)
func(context)
process.nextTick(cb)
} else {
func(this._server, cb)
func(context, cb)
}
}
}

function encapsulateTwoParamServer (func, that) {
function encapsulateTwoParam (func, that) {
return _encapsulateTwoParam.bind(that)
function _encapsulateTwoParam (context, cb) {
if (func.length === 0 || func.length === 1) {
Expand All @@ -257,21 +259,7 @@ function encapsulateTwoParamServer (func, that) {
}
}

function encapsulateThreeParamAvvio (func, that, useContext) {
return _encapsulateThreeParam.bind(that)
function _encapsulateThreeParam (err, cb) {
if (func.length === 0 || func.length === 1) {
func(err)
process.nextTick(cb)
} else if (func.length === 2) {
func(err, cb)
} else {
func(err, this._server, cb)
}
}
}

function encapsulateThreeParamServer (func, that, useContext) {
function encapsulateThreeParam (func, that, useContext) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useContext is not "used" here anymore

return _encapsulateThreeParam.bind(that)
function _encapsulateThreeParam (err, cb) {
if (func.length === 0 || func.length === 1) {
Expand Down
8 changes: 4 additions & 4 deletions test/after-and-ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ test('should pass the errors from after to ready', (t) => {
})
})

test('after encapsulation', t => {
test('after no encapsulation', t => {
t.plan(4)

const app = boot()
Expand All @@ -386,7 +386,7 @@ test('after encapsulation', t => {
instance.test = true
instance.after(function (err, i, done) {
t.error(err)
t.ok(i.test)
t.notOk(i.test)
done()
})
next()
Expand All @@ -399,7 +399,7 @@ test('after encapsulation', t => {
})
})

test('ready encapsulation', t => {
test('ready no encapsulation', t => {
t.plan(4)

const app = boot()
Expand All @@ -412,7 +412,7 @@ test('ready encapsulation', t => {
instance.test = true
instance.ready(function (err, i, done) {
t.error(err)
t.ok(i.test)
t.notOk(i.test)
done()
})
next()
Expand Down
4 changes: 2 additions & 2 deletions test/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test('onClose arguments - fastify encapsulation test case', (t) => {
})
})

test('onClose arguments - encapsulation test case', (t) => {
test('onClose arguments - encapsulation test case no server', (t) => {
t.plan(5)

const app = boot()
Expand All @@ -104,7 +104,7 @@ test('onClose arguments - encapsulation test case', (t) => {
app.use(function (instance, opts, next) {
instance.test = true
instance.onClose((i, done) => {
t.ok(i.test)
t.notOk(i.test)
done()
})
next()
Expand Down