-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRedisEvent.js
42 lines (35 loc) · 1.04 KB
/
RedisEvent.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
/**
* Creates a event listener for redis keyspace notifications
* @param client {Redis} - An instance of node-redis client to connect with
* @param event {String} - The type of keyspace event
* @param keyPattern {RegExp} - RegExp pattern to match for key names
* @constructor
*/
var RedisEvent = function(client, event, keyPattern){
var _this = this;
this.redis = client;
this.redis.client('setname', 'event-listener');
this.keyPattern = keyPattern;
this.handler = null;
this.redis.subscribe('__keyevent@0__:' + event);
this.redis.on('message', function(event, key){
var match = _this.keyPattern.exec(key);
if(match != null){
if(_this.handler != null){
_this.handler(match);
} else {
throw 'NoHandlerException';
}
}
});
};
RedisEvent.prototype = {
};
/**
* Sets the handler for the event
* @param {Function} handler - The function that is called when the event occurs.
*/
RedisEvent.prototype.defineHandler = function(handler){
this.handler = handler;
};
module.exports = RedisEvent;