-
Notifications
You must be signed in to change notification settings - Fork 14
/
monitor.js
48 lines (35 loc) · 1008 Bytes
/
monitor.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
/* Copyright (C) 2012 Igalia S.L.
*
* Author: Javier Fernandez Garcia-Boente <jfernandez@igalia.com>
*
* Licensed under the MIT license */
'use strict';
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var redisSync = require('redis-sync');
function Monitor(keys) {
var that = this;
var sync = new redisSync.Sync();
var myKeys = keys;
sync.on('command', function(command, args) {
var key = Buffer.concat(args[0]).toString();
if (myKeys.indexOf(key) !== -1) {
console.log('key %s changed: %s', key, command);
that.emit('changed', key, command, args.slice(1));
}
});
sync.on('error', function(err) {
console.error(err);
});
that.connect = function(p, h) {
sync.connect(p, h);
};
that.disconnect = function(eventHandler) {
sync.removeListener('command', eventHandler);
};
that.addKeys = function addKeys(keys) {
myKeys.concat(keys);
}
}
util.inherits(Monitor, EventEmitter);
exports.Monitor = Monitor;