Type Definitions
middleware(…parametersopt, nextopt, doneopt)
Middleware are callbacks that will be executed when a hook is called. The type of middleware is determined through the parameters it declares.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
parameters |
* |
<optional> <repeatable> |
parameters passed to the hook |
next |
function |
<optional> |
pass control to the next middleware |
done |
function |
<optional> |
mark parallel middleware to have completed |
Examples
function(){
//synchronous execution
}
function(){
//create promise, i.e. further execution is halted until the promise is resolved.
return promise;
}
function(next){
//asynchronous execution, i.e. further execution is halted until `next` is called.
setTimeout(next, 1000);
}
function(next, done){
//asynchronous execution, i.e. further execution is halted until `next` is called.
setTimeout(next, 1000);
//full middleware queue handling is halted until `done` is called.
setTimeout(done, 2000);
}
options
Type:
- Object
Properties:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
strict |
Boolean |
<optional> |
true | Will disallow subscribing to middleware bar the explicitly registered ones. |
|||||||||||||||
qualifiers |
Object |
<optional> |
Properties
|
||||||||||||||||
createThenable |
function |
<optional> |
Set a Promise A+ compliant factory function for creating promises. |
Examples
//creates a GrapplingHook instance with `before` and `after` hooking
var instance = grappling.create({
qualifiers: {
pre: 'before',
post: 'after'
}
});
instance.before('save', console.log);
//creates a GrapplingHook instance with a promise factory
var P = require('bluebird');
var instance = grappling.create({
createThenable: function(fn){
return new P(fn);
}
});
instance.allowHooks('save');
instance.pre('save', console.log);
instance.callThenableHook('pre:save', 'Here we go!').then(function(){
console.log('And finish!');
});
//outputs:
//Here we go!
//And finish!
thenable
The GrapplingHook documentation uses the term "thenable" instead of "promise", since what we need here is not necessarily a promise, but a thenable, as defined in the Promises A+ spec.
Thenable middleware for instance can be any object that has a then function.
Strictly speaking the only instance where we adhere to the full Promises A+ definition of a promise is in options.createThenable.
For reasons of clarity, uniformity and symmetry we chose createThenable, although strictly speaking it should've been createPromise.
Most people would find it confusing if part of the API uses 'thenable' and another part 'promise'.
Type:
- Object
Properties:
| Name | Type | Description |
|---|---|---|
then |
function | see Promises A+ spec |
- Source:
- See:
-
- options.createThenable
- isThenable