1 | "use strict";
|
2 |
|
3 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
4 |
|
5 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
6 |
|
7 | var Events, RedisConnection, Scripts, parser;
|
8 | parser = require("./parser");
|
9 | Events = require("./Events");
|
10 | Scripts = require("./Scripts");
|
11 |
|
12 | RedisConnection = function () {
|
13 | class RedisConnection {
|
14 | constructor(options = {}) {
|
15 | parser.load(options, this.defaults, this);
|
16 |
|
17 | if (this.Redis == null) {
|
18 | this.Redis = eval("require")("redis");
|
19 | }
|
20 |
|
21 | if (this.Events == null) {
|
22 | this.Events = new Events(this);
|
23 | }
|
24 |
|
25 | this.terminated = false;
|
26 |
|
27 | if (this.client == null) {
|
28 | this.client = this.Redis.createClient(this.clientOptions);
|
29 | }
|
30 |
|
31 | this.subscriber = this.client.duplicate();
|
32 | this.limiters = {};
|
33 | this.shas = {};
|
34 | this.ready = this.Promise.all([this._setup(this.client, false), this._setup(this.subscriber, true)]).then(() => {
|
35 | return this._loadScripts();
|
36 | }).then(() => {
|
37 | return {
|
38 | client: this.client,
|
39 | subscriber: this.subscriber
|
40 | };
|
41 | });
|
42 | }
|
43 |
|
44 | _setup(client, sub) {
|
45 | client.setMaxListeners(0);
|
46 | return new this.Promise((resolve, reject) => {
|
47 | client.on("error", e => {
|
48 | return this.Events.trigger("error", e);
|
49 | });
|
50 |
|
51 | if (sub) {
|
52 | client.on("message", (channel, message) => {
|
53 | var ref;
|
54 | return (ref = this.limiters[channel]) != null ? ref._store.onMessage(channel, message) : void 0;
|
55 | });
|
56 | }
|
57 |
|
58 | if (client.ready) {
|
59 | return resolve();
|
60 | } else {
|
61 | return client.once("ready", resolve);
|
62 | }
|
63 | });
|
64 | }
|
65 |
|
66 | _loadScript(name) {
|
67 | return new this.Promise((resolve, reject) => {
|
68 | var payload;
|
69 | payload = Scripts.payload(name);
|
70 | return this.client.multi([["script", "load", payload]]).exec((err, replies) => {
|
71 | if (err != null) {
|
72 | return reject(err);
|
73 | }
|
74 |
|
75 | this.shas[name] = replies[0];
|
76 | return resolve(replies[0]);
|
77 | });
|
78 | });
|
79 | }
|
80 |
|
81 | _loadScripts() {
|
82 | return this.Promise.all(Scripts.names.map(k => {
|
83 | return this._loadScript(k);
|
84 | }));
|
85 | }
|
86 |
|
87 | __runCommand__(cmd) {
|
88 | var _this = this;
|
89 |
|
90 | return _asyncToGenerator(function* () {
|
91 | yield _this.ready;
|
92 | return new _this.Promise((resolve, reject) => {
|
93 | return _this.client.multi([cmd]).exec_atomic(function (err, replies) {
|
94 | if (err != null) {
|
95 | return reject(err);
|
96 | } else {
|
97 | return resolve(replies[0]);
|
98 | }
|
99 | });
|
100 | });
|
101 | })();
|
102 | }
|
103 |
|
104 | __addLimiter__(instance) {
|
105 | return this.Promise.all([instance.channel(), instance.channel_client()].map(channel => {
|
106 | return new this.Promise((resolve, reject) => {
|
107 | var handler;
|
108 |
|
109 | handler = chan => {
|
110 | if (chan === channel) {
|
111 | this.subscriber.removeListener("subscribe", handler);
|
112 | this.limiters[channel] = instance;
|
113 | return resolve();
|
114 | }
|
115 | };
|
116 |
|
117 | this.subscriber.on("subscribe", handler);
|
118 | return this.subscriber.subscribe(channel);
|
119 | });
|
120 | }));
|
121 | }
|
122 |
|
123 | __removeLimiter__(instance) {
|
124 | var _this2 = this;
|
125 |
|
126 | return this.Promise.all([instance.channel(), instance.channel_client()].map(
|
127 |
|
128 | function () {
|
129 | var _ref = _asyncToGenerator(function* (channel) {
|
130 | if (!_this2.terminated) {
|
131 | yield new _this2.Promise((resolve, reject) => {
|
132 | return _this2.subscriber.unsubscribe(channel, function (err, chan) {
|
133 | if (err != null) {
|
134 | return reject(err);
|
135 | }
|
136 |
|
137 | if (chan === channel) {
|
138 | return resolve();
|
139 | }
|
140 | });
|
141 | });
|
142 | }
|
143 |
|
144 | return delete _this2.limiters[channel];
|
145 | });
|
146 |
|
147 | return function (_x) {
|
148 | return _ref.apply(this, arguments);
|
149 | };
|
150 | }()));
|
151 | }
|
152 |
|
153 | __scriptArgs__(name, id, args, cb) {
|
154 | var keys;
|
155 | keys = Scripts.keys(name, id);
|
156 | return [this.shas[name], keys.length].concat(keys, args, cb);
|
157 | }
|
158 |
|
159 | __scriptFn__(name) {
|
160 | return this.client.evalsha.bind(this.client);
|
161 | }
|
162 |
|
163 | disconnect(flush = true) {
|
164 | var i, k, len, ref;
|
165 | ref = Object.keys(this.limiters);
|
166 |
|
167 | for (i = 0, len = ref.length; i < len; i++) {
|
168 | k = ref[i];
|
169 | clearInterval(this.limiters[k]._store.heartbeat);
|
170 | }
|
171 |
|
172 | this.limiters = {};
|
173 | this.terminated = true;
|
174 | this.client.end(flush);
|
175 | this.subscriber.end(flush);
|
176 | return this.Promise.resolve();
|
177 | }
|
178 |
|
179 | }
|
180 |
|
181 | ;
|
182 | RedisConnection.prototype.datastore = "redis";
|
183 | RedisConnection.prototype.defaults = {
|
184 | Redis: null,
|
185 | clientOptions: {},
|
186 | client: null,
|
187 | Promise: Promise,
|
188 | Events: null
|
189 | };
|
190 | return RedisConnection;
|
191 | }.call(void 0);
|
192 |
|
193 | module.exports = RedisConnection; |
\ | No newline at end of file |