This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.js
126 lines (115 loc) · 3.46 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var events = require('events'),
util = require('util'),
Q = require('q'),
_ = require('lodash')
;
/** Hook system for Missy
* Supports synchronous hooks, promise-based asynchronous hooks.
* Each hook call is also translated to an event with the same name.
*
* To set a hook handler, use either:
* - hooks.registerHook('name', function(){ ... }); - sync hook
* - hooks.on('name', function(){ ... }); - async hook
* - hooks.name = function(){ ... }; - sugar. Only works when `hookNames` is provided
*
* @param {Array.<String>?} hookNames
* The list of supported hooks.
* When empty, you can use arbitrary hooks but will loose the 'get/set' magic.
*
* @constructor
* @extends {EventEmitter}
*/
var MissyHooks = exports.MissyHooks = function(hookNames){
this._hookNames = hookNames;
this._hooks = {};
// get/set magic
if (this._hookNames){
_.each(this._hookNames, function(name){
Object.defineProperty(this, name, {
enumerable: true,
configurable: false,
get: function(){
return _.partial(this.invokeHook.bind(this), name);
},
set: function(hook){
return this.registerHook(name, hook);
}
});
}, this);
}
};
util.inherits(MissyHooks, events.EventEmitter);
/** Check hook name against the allowed list
* @param {String} name
* @throws {Error}
* @private
*/
MissyHooks.prototype._checkHookName = function(name){
if (this._hookNames && !_.contains(this._hookNames, name))
throw new Error('Using an unsupported hook name: "'+ name +'"');
};
/** Get the list of available hooks
* @returns {Array.<String>}
*/
MissyHooks.prototype.getHookNames = function(){
return this._hookNames;
};
/** Register a function for the hook
* @param {String} name
* Hook name
* @param {Function} handler
* Hook function to add
* @returns {Function}
*/
MissyHooks.prototype.registerHook = function(name, handler){
this._checkHookName(name);
if (!(name in this._hooks))
this._hooks[name] = [];
this._hooks[name].push(handler);
return handler;
};
/** Unregister a function from the hook
* @param {String} name
* Hook name
* @param {Function} handler
* Hook function to remove
* @returns {Function}
*/
MissyHooks.prototype.unregisterHook = function(name, handler){
this._checkHookName(name);
if (!(name in this._hooks))
return;
this._hooks[name] = _.without(this._hooks[name], handler);
};
/** Unregister all handlers from the given hook
* @param {String} name
* Hook name
*/
MissyHooks.prototype.unregisterAllHooks = function(name){
if (name in this._hooks)
delete this._hooks[name];
};
/** Invoke all registered hook handlers for the given hook
* @param {String} name
* @param {...*} args
* @returns {Q} a promise for `args`
*/
MissyHooks.prototype.invokeHook = function(name){
// Arguments
this._checkHookName(name);
var args = Array.prototype.slice.call(arguments, 1);
// Invoke
try {
if (!(name in this._hooks))
return Q.fulfill(args);
return this._hooks[name].reduce(function(cur, next){
return cur.then(function(){
return next.apply(undefined, args);
});
}, Q(1)).thenResolve(args);
} finally {
// Finally, emit an event
this.emit.apply(this, [name].concat(args));
}
};