-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.js
100 lines (85 loc) · 2.52 KB
/
route.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
var util = require('./util');
util.inherits = require('util').inherits;
/** @interface */
var Route = function (callback) {
this.callback = callback;
this.loggers = [];
this.enabled = true;
// Bound so that we can unlisten easily
this.onDataBoundFn = this.onData_.bind(this);
};
Route.prototype.onData_ = function (ev) {
if (this.matches_(ev)) {
this.callback.apply(this, [ev.path.join('.')].concat(ev.data));
}
};
Route.prototype.handle = function (logger) {
// TODO(gregp): check if it exists already
this.loggers.push(logger);
if (this.enabled) {
// Handle logged data events
logger.on('data', this.onDataBoundFn);
// TODO(gregp): logger.on('string', function (ev) {
}
};
Route.prototype.disable = function () {
if (!this.enabled) return;
this.loggers.forEach(function (logger) {
logger.removeListener('data', this.onDataBoundFn);
}.bind(this));
this.enabled = false;
};
Route.prototype.clear = function () {
this.disable();
this.loggers = [];
};
Route.prototype.enable = function () {
if (this.enabled) return;
this.loggers.forEach(function (logger) {
logger.on('data', this.onDataBoundFn);
}.bind(this));
this.enabled = true;
};
Route.prototype.matches_ = function (ev) {
return true;
};
/**
* Listens to a set of loggers, determines whether to pass things through,
* then takes the given action.
*/
var LevelRoute = function (level, callback) {
this.level = level;
LevelRoute.super_.call(this, callback);
};
util.inherits(LevelRoute, Route);
LevelRoute.prototype.matches_ = function (ev) {
if (!LevelRoute.super_.prototype.matches_.call(ev)) return false;
// If logged with no level, match it
if (!ev.level) return true;
// If logged closer to 0 (EMERG) than this level, match it
return ev.level < this.level;
};
var PathLevelRoute = function (pathString, level, callback) {
this.path = util.splitPath_(pathString);
PathLevelRoute.super_.prototype.call(this, level, callback);
};
PathLevelRoute.pathLevelMatches_ = function (mine, seen) {
if (!seen) return true;
if (!mine) return false;
do {
if (!seen.length) {
return true;
}
if (seen.shift() != mine.shift()) {
return false;
}
} while (mine.length);
return false;
};
PathLevelRoute.prototype.matches_ = function (ev) {
if (!PathLevelRoute.super_.prototype.matches_.call(ev)) return false;
return PathLevelRoute.pathLevelMatches_(this.path, ev.path);
};
module.exports.Route = Route;
module.exports.LevelRoute = LevelRoute;
module.exports.PathLevelRoute = PathLevelRoute;