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