-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathindex.js
82 lines (68 loc) · 2.13 KB
/
index.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
import { EventEmitter } from 'events';
import { Command } from 'ioredis';
import * as commands from './commands';
import * as commandsStream from './commands-stream';
import createCommand from './command';
import createData from './data';
import createExpires from './expires';
import Pipeline from './pipeline';
import promiseContainer from './promise-container';
class RedisMock extends EventEmitter {
constructor({ data = {} } = {}) {
super();
this.channels = {};
this.batch = undefined;
this.expires = createExpires();
this.data = createData(this.expires, data);
Object.keys(commands).forEach(command => {
this[command] = createCommand(
commands[command].bind(this),
command,
this
);
});
Object.keys(commandsStream).forEach(command => {
this[command] = commandsStream[command].bind(this);
});
process.nextTick(() => {
this.emit('connect');
this.emit('ready');
});
}
multi(batch = []) {
this.batch = new Pipeline(this);
// eslint-disable-next-line no-underscore-dangle
this.batch._transactions += 1;
batch.forEach(([command, ...options]) => this.batch[command](...options));
return this.batch;
}
pipeline(batch = []) {
this.batch = new Pipeline(this);
batch.forEach(([command, ...options]) => this.batch[command](...options));
return this.batch;
}
exec(callback) {
const Promise = promiseContainer.get();
if (!this.batch) {
return Promise.reject(new Error('ERR EXEC without MULTI'));
}
const pipeline = this.batch;
this.batch = undefined;
return pipeline.exec(callback);
}
}
RedisMock.prototype.Command = {
// eslint-disable-next-line no-underscore-dangle
transformers: Command._transformer,
setArgumentTransformer: (name, func) => {
RedisMock.prototype.Command.transformers.argument[name] = func;
},
setReplyTransformer: (name, func) => {
RedisMock.prototype.Command.transformers.reply[name] = func;
},
};
Object.defineProperty(RedisMock, 'Promise', {
get: () => promiseContainer.get(),
set: lib => promiseContainer.set(lib),
});
module.exports = RedisMock;