-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.js
70 lines (57 loc) · 1.6 KB
/
hook.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
var _ = require('underscore');
var stack = require('./stack.js').main;
var uuid = require('./uuid');
function hook(Gamify) {
this.Gamify = Gamify;
this.hooks = {};
this.modifiers = {};
}
hook.prototype.register = function(endpoint, method, callback) {
if (!this.hooks[endpoint]) {
this.hooks[endpoint] = {};
}
if (!this.hooks[endpoint][method]) {
this.hooks[endpoint][method] = [];
}
this.hooks[endpoint][method].push({
uuid: uuid.v4(),
callback: callback
});
}
hook.prototype.registerModifier = function(endpoint, method, callback) {
if (!this.modifiers[endpoint]) {
this.modifiers[endpoint] = {};
}
if (!this.modifiers[endpoint][method]) {
this.modifiers[endpoint][method] = callback;
} else {
console.log("******* WARNING *******\n\tYou have more than one modifier assigned to "+endpoint+"."+method+"!");
}
}
hook.prototype.trigger = function(endpoint, method, params, response, callback) {
//console.log("****** Hooks:\n",this.hooks[endpoint][method]);
var i;
var l
// Process Hooks
if (this.hooks[endpoint] && this.hooks[endpoint][method]) {
l = this.hooks[endpoint][method].length;
var hookStack = new stack();
var hookResponseStack = new stack();
for (i=0;i<l;i++) {
this.hooks[endpoint][method][i].callback(params, response);
}
}
// Process Modifiers
if (!this.modifiers[endpoint]) {
callback(response);
return false;
}
if (!this.modifiers[endpoint][method]) {
callback(response);
return false;
}
this.modifiers[endpoint][method](response, function(modifiedResponse) {
callback(modifiedResponse);
});
}
exports.main = hook;