Methods
addAsyncHooks()
alias for GrapplingHook#addHooks.
addHooks(methods) → {GrapplingHook}
Wraps asynchronous methods/functions with pre
and/or post
hooks
Parameters:
Name | Type | Description |
---|---|---|
methods |
String | Array.<String> | Object | Array.<Object> | method(s) that need(s) to emit |
- Source:
- See:
-
- GrapplingHook#addSyncHooks for wrapping synchronous methods
- GrapplingHook#addThenableHooks for wrapping thenable methods
Returns:
- Type
- GrapplingHook
Examples
//wrap existing methods
instance.addHooks('save', 'pre:remove');
//add method and wrap it
instance.addHooks({
save: instance._upload,
"pre:remove": function(){
//...
}
});
addSyncHooks(methods) → {GrapplingHook}
Wraps synchronous methods/functions with pre
and/or post
hooks
Parameters:
Name | Type | Description |
---|---|---|
methods |
String | Array.<String> | Object | Array.<Object> | method(s) that need(s) to emit |
- Since:
- 2.4.0
- Source:
- See:
-
- GrapplingHook#addHooks for wrapping asynchronous methods
- GrapplingHook#addThenableHooks for wrapping thenable methods
Returns:
- Type
- GrapplingHook
addThenableHooks(methods) → {GrapplingHook}
Wraps thenable methods/functions with pre
and/or post
hooks
Parameters:
Name | Type | Description |
---|---|---|
methods |
String | Array.<String> | Object | Array.<Object> | method(s) that need(s) to emit |
- Since:
- 3.0.0
- Source:
- See:
-
- GrapplingHook#addHooks for wrapping asynchronous methods
- GrapplingHook#addSyncHooks for wrapping synchronous methods
Returns:
- Type
- GrapplingHook
allowHooks(hooks) → {GrapplingHook}
Explicitly declare hooks
Parameters:
Name | Type | Description |
---|---|---|
hooks |
string | Array.<string> | (qualified) hooks e.g. |
Returns:
- Type
- GrapplingHook
callAsyncHook()
alias for GrapplingHook#callHook.
callHook(contextopt, qualifiedHook, …parametersopt, callbackopt) → {GrapplingHook}
Calls all middleware subscribed to the asynchronous qualifiedHook
and passes remaining parameters to them
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
context |
* |
<optional> |
the context in which the middleware will be called |
qualifiedHook |
String | qualified hook e.g. |
|
parameters |
* |
<optional> <repeatable> |
any parameters you wish to pass to the middleware. |
callback |
function |
<optional> |
will be called when all middleware have finished |
- Source:
- See:
-
- GrapplingHook#callSyncHook for calling synchronous hooks
- GrapplingHook#callThenableHook for calling thenable hooks
Returns:
- Type
- GrapplingHook
callSyncHook(contextopt, qualifiedHook, …parametersopt) → {GrapplingHook}
Calls all middleware subscribed to the synchronous qualifiedHook
and passes remaining parameters to them
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
context |
* |
<optional> |
the context in which the middleware will be called |
qualifiedHook |
String | qualified hook e.g. |
|
parameters |
* |
<optional> <repeatable> |
any parameters you wish to pass to the middleware. |
- Since:
- 2.4.0
- Source:
- See:
-
- GrapplingHook#callHook for calling asynchronous hooks
- GrapplingHook#callThenableHook for calling thenable hooks
Returns:
- Type
- GrapplingHook
callThenableHook(contextopt, qualifiedHook, …parametersopt) → {thenable}
Calls all middleware subscribed to the synchronous qualifiedHook
and passes remaining parameters to them
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
context |
* |
<optional> |
the context in which the middleware will be called |
qualifiedHook |
String | qualified hook e.g. |
|
parameters |
* |
<optional> <repeatable> |
any parameters you wish to pass to the middleware. |
- Since:
- 3.0.0
- Source:
- See:
-
- GrapplingHook#callHook for calling asynchronous hooks
- GrapplingHook#callSyncHook for calling synchronous hooks
Returns:
- a thenable, as created with options.createThenable
- Type
- thenable
getMiddleware(qualifiedHook) → {Array.<middleware>}
Retrieve all middleware registered to qualifiedHook
Parameters:
Name | Type | Description |
---|---|---|
qualifiedHook |
qualified hook, e.g. |
Returns:
- Type
- Array.<middleware>
hasMiddleware(qualifiedHook) → {boolean}
Determines whether any middleware is registered to qualifiedHook
.
Parameters:
Name | Type | Description |
---|---|---|
qualifiedHook |
string | qualified hook, e.g. |
Returns:
- Type
- boolean
hook(qualifiedHook, middleware) → {GrapplingHook|thenable}
Adds middleware to a qualified hook. Convenience method which allows you to add middleware dynamically more easily.
Parameters:
Name | Type | Description |
---|---|---|
qualifiedHook |
String | qualified hook e.g. |
middleware |
middleware | Array.<middleware> | middleware to call |
Returns:
- Type
- GrapplingHook | thenable
Example
instance.hook('pre:save', function(next) {
console.log('before saving');
next();
}
hookable(qualifiedHook) → {boolean}
Determines whether registration of middleware to qualifiedHook
is allowed. (Always returns true
for lenient instances)
Parameters:
Name | Type | Description |
---|---|---|
qualifiedHook |
String | Array.<String> | qualified hook e.g. |
Returns:
- Type
- boolean
post(hook, middlewareopt) → {GrapplingHook|thenable}
Registers middleware
to be executed after hook
.
This is a dynamically added method, that may not be present if otherwise configured in options.qualifiers.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
hook |
string | hook name, e.g. |
|
middleware |
middleware | Array.<middleware> |
<optional> |
middleware to register |
- Source:
- See:
-
- GrapplingHook#pre for registering middleware functions to `post` hooks.
Returns:
the GrapplingHook instance itself, or a thenable if no middleware was provided.
- Type
- GrapplingHook | thenable
Example
instance.post('save', function(){
console.log('after saving');
});
pre(hook, middlewareopt) → {GrapplingHook|thenable}
Registers middleware
to be executed before hook
.
This is a dynamically added method, that may not be present if otherwise configured in options.qualifiers.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
hook |
string | hook name, e.g. |
|
middleware |
middleware | Array.<middleware> |
<optional> |
middleware to register |
- Source:
- See:
-
- GrapplingHook#post for registering middleware functions to `post` hooks.
Returns:
the GrapplingHook instance itself, or a thenable if no middleware was provided.
- Type
- GrapplingHook | thenable
Example
instance.pre('save', function(){
console.log('before saving');
});
unhook(hookopt, middlewareopt) → {GrapplingHook}
Removes middleware for hook
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
hook |
String |
<optional> |
(qualified) hooks e.g. |
middleware |
middleware | Array.<middleware> |
<optional> |
function(s) to be removed |
Returns:
- Type
- GrapplingHook
Examples
//removes `onPreSave` Function as a `pre:save` middleware
instance.unhook('pre:save', onPreSave);
//removes all middleware for `pre:save`
instance.unhook('pre:save');
//removes all middleware for `pre:save` and `post:save`
instance.unhook('save');
//removes ALL middleware
instance.unhook();