@azteam/redis-async 1.0.117 → 1.0.118
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/RedisAsync.js +150 -7
- package/package.json +2 -2
- package/src/RedisAsync.js +81 -8
package/lib/RedisAsync.js
CHANGED
|
@@ -4,9 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
var
|
|
7
|
+
var _ioredis = _interopRequireDefault(require("ioredis"));
|
|
8
8
|
var _util = require("@azteam/util");
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
9
10
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
12
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
13
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
14
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
15
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
16
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
10
17
|
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function c(t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == _typeof(f) && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i["return"] && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(_typeof(r) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), c(GeneratorFunctionPrototype, u, GeneratorFunction.displayName = "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function reset(r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function abrupt(t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function complete(t, r) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f; }, finish: function finish(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, "catch": function _catch(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; }
|
|
11
18
|
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
12
19
|
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
@@ -22,6 +29,7 @@ var RedisAsync = /*#__PURE__*/function () {
|
|
|
22
29
|
this.host = config.host;
|
|
23
30
|
this.port = config.port;
|
|
24
31
|
this.prefix = config.prefix;
|
|
32
|
+
this.subscriptions = {};
|
|
25
33
|
this.connect();
|
|
26
34
|
}
|
|
27
35
|
return _createClass(RedisAsync, [{
|
|
@@ -77,21 +85,25 @@ var RedisAsync = /*#__PURE__*/function () {
|
|
|
77
85
|
value: function connect() {
|
|
78
86
|
var _this = this;
|
|
79
87
|
this._alert('connecting', 'Redis connecting...');
|
|
80
|
-
this.client =
|
|
88
|
+
this.client = new _ioredis["default"]({
|
|
81
89
|
host: this.host,
|
|
82
90
|
port: this.port,
|
|
83
|
-
|
|
91
|
+
lazyConnect: true,
|
|
92
|
+
retryStrategy: function retryStrategy() {
|
|
84
93
|
_this.connected = false;
|
|
85
94
|
_this._alert('connect', 'Redis disconnected');
|
|
86
|
-
|
|
95
|
+
return null;
|
|
87
96
|
}
|
|
88
97
|
});
|
|
89
|
-
this.client.connect()
|
|
98
|
+
this.client.connect()["catch"](function (err) {
|
|
99
|
+
_this._alert('error', "Redis Connection Error: ".concat(err));
|
|
100
|
+
});
|
|
90
101
|
this.client.on('connect', function () {
|
|
91
102
|
_this.connected = true;
|
|
92
103
|
_this._alert('connect', 'Redis connected');
|
|
93
104
|
});
|
|
94
|
-
this.client.on('
|
|
105
|
+
this.client.on('close', function () {
|
|
106
|
+
_this.connected = false;
|
|
95
107
|
_this._alert('end', 'Redis end');
|
|
96
108
|
});
|
|
97
109
|
this.client.on('error', function (err) {
|
|
@@ -245,6 +257,7 @@ var RedisAsync = /*#__PURE__*/function () {
|
|
|
245
257
|
prefixKey,
|
|
246
258
|
regexKey,
|
|
247
259
|
keys,
|
|
260
|
+
_this$client,
|
|
248
261
|
_args6 = arguments;
|
|
249
262
|
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
250
263
|
while (1) switch (_context6.prev = _context6.next) {
|
|
@@ -275,7 +288,7 @@ var RedisAsync = /*#__PURE__*/function () {
|
|
|
275
288
|
_context6.next = 15;
|
|
276
289
|
break;
|
|
277
290
|
}
|
|
278
|
-
return _context6.abrupt("return", this.client.del(keys));
|
|
291
|
+
return _context6.abrupt("return", (_this$client = this.client).del.apply(_this$client, _toConsumableArray(keys)));
|
|
279
292
|
case 15:
|
|
280
293
|
_context6.next = 21;
|
|
281
294
|
break;
|
|
@@ -301,6 +314,136 @@ var RedisAsync = /*#__PURE__*/function () {
|
|
|
301
314
|
}
|
|
302
315
|
return remove;
|
|
303
316
|
}()
|
|
317
|
+
}, {
|
|
318
|
+
key: "publish",
|
|
319
|
+
value: function () {
|
|
320
|
+
var _publish = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(channel, data) {
|
|
321
|
+
var count,
|
|
322
|
+
prefixChannel,
|
|
323
|
+
_args7 = arguments;
|
|
324
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
325
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
326
|
+
case 0:
|
|
327
|
+
count = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : 0;
|
|
328
|
+
prefixChannel = this.parsePrefix(channel);
|
|
329
|
+
if (!this.connected) {
|
|
330
|
+
_context7.next = 4;
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
return _context7.abrupt("return", this.client.publish(prefixChannel, JSON.stringify(data)));
|
|
334
|
+
case 4:
|
|
335
|
+
this.connect();
|
|
336
|
+
if (!(count < 5)) {
|
|
337
|
+
_context7.next = 7;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
return _context7.abrupt("return", this.publish(channel, data, count + 1));
|
|
341
|
+
case 7:
|
|
342
|
+
return _context7.abrupt("return", false);
|
|
343
|
+
case 8:
|
|
344
|
+
case "end":
|
|
345
|
+
return _context7.stop();
|
|
346
|
+
}
|
|
347
|
+
}, _callee7, this);
|
|
348
|
+
}));
|
|
349
|
+
function publish(_x7, _x8) {
|
|
350
|
+
return _publish.apply(this, arguments);
|
|
351
|
+
}
|
|
352
|
+
return publish;
|
|
353
|
+
}()
|
|
354
|
+
}, {
|
|
355
|
+
key: "subscribe",
|
|
356
|
+
value: function () {
|
|
357
|
+
var _subscribe = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(channel, callback) {
|
|
358
|
+
var _this2 = this;
|
|
359
|
+
var prefixChannel;
|
|
360
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
361
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
362
|
+
case 0:
|
|
363
|
+
prefixChannel = this.parsePrefix(channel);
|
|
364
|
+
if (!this.subscriberClient) {
|
|
365
|
+
this.subscriberClient = this.client.duplicate();
|
|
366
|
+
this.subscriberClient.connect()["catch"](function (err) {
|
|
367
|
+
_this2._alert('error', "Redis Subscriber Error: ".concat(err));
|
|
368
|
+
});
|
|
369
|
+
this.subscriberClient.on('message', function (chan, message) {
|
|
370
|
+
var parsedMsg = message;
|
|
371
|
+
try {
|
|
372
|
+
parsedMsg = JSON.parse(message);
|
|
373
|
+
} catch (e) {
|
|
374
|
+
// ignore if not JSON
|
|
375
|
+
}
|
|
376
|
+
if (_this2.subscriptions[chan]) {
|
|
377
|
+
_this2.subscriptions[chan].forEach(function (cb) {
|
|
378
|
+
return cb(parsedMsg);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
if (this.subscriptions[prefixChannel]) {
|
|
384
|
+
_context8.next = 6;
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
this.subscriptions[prefixChannel] = [];
|
|
388
|
+
_context8.next = 6;
|
|
389
|
+
return this.subscriberClient.subscribe(prefixChannel);
|
|
390
|
+
case 6:
|
|
391
|
+
this.subscriptions[prefixChannel].push(callback);
|
|
392
|
+
case 7:
|
|
393
|
+
case "end":
|
|
394
|
+
return _context8.stop();
|
|
395
|
+
}
|
|
396
|
+
}, _callee8, this);
|
|
397
|
+
}));
|
|
398
|
+
function subscribe(_x9, _x0) {
|
|
399
|
+
return _subscribe.apply(this, arguments);
|
|
400
|
+
}
|
|
401
|
+
return subscribe;
|
|
402
|
+
}()
|
|
403
|
+
}, {
|
|
404
|
+
key: "unsubscribe",
|
|
405
|
+
value: function () {
|
|
406
|
+
var _unsubscribe = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(channel, callback) {
|
|
407
|
+
var prefixChannel;
|
|
408
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
409
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
410
|
+
case 0:
|
|
411
|
+
prefixChannel = this.parsePrefix(channel);
|
|
412
|
+
if (this.subscriptions[prefixChannel]) {
|
|
413
|
+
_context9.next = 3;
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
return _context9.abrupt("return");
|
|
417
|
+
case 3:
|
|
418
|
+
if (callback) {
|
|
419
|
+
this.subscriptions[prefixChannel] = this.subscriptions[prefixChannel].filter(function (cb) {
|
|
420
|
+
return cb !== callback;
|
|
421
|
+
});
|
|
422
|
+
} else {
|
|
423
|
+
this.subscriptions[prefixChannel] = [];
|
|
424
|
+
}
|
|
425
|
+
if (!(this.subscriptions[prefixChannel].length === 0)) {
|
|
426
|
+
_context9.next = 9;
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
delete this.subscriptions[prefixChannel];
|
|
430
|
+
if (!this.subscriberClient) {
|
|
431
|
+
_context9.next = 9;
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
_context9.next = 9;
|
|
435
|
+
return this.subscriberClient.unsubscribe(prefixChannel);
|
|
436
|
+
case 9:
|
|
437
|
+
case "end":
|
|
438
|
+
return _context9.stop();
|
|
439
|
+
}
|
|
440
|
+
}, _callee9, this);
|
|
441
|
+
}));
|
|
442
|
+
function unsubscribe(_x1, _x10) {
|
|
443
|
+
return _unsubscribe.apply(this, arguments);
|
|
444
|
+
}
|
|
445
|
+
return unsubscribe;
|
|
446
|
+
}()
|
|
304
447
|
}, {
|
|
305
448
|
key: "setAlertCallback",
|
|
306
449
|
value: function setAlertCallback(callback) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/redis-async",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.118",
|
|
4
4
|
"description": "N/A",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"toda",
|
|
@@ -17,6 +17,6 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@azteam/util": "1.0.65",
|
|
20
|
-
"
|
|
20
|
+
"ioredis": "^5.10.1"
|
|
21
21
|
}
|
|
22
22
|
}
|
package/src/RedisAsync.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
2
|
|
|
3
3
|
import {timeout} from '@azteam/util';
|
|
4
4
|
|
|
@@ -8,6 +8,7 @@ class RedisAsync {
|
|
|
8
8
|
this.host = config.host;
|
|
9
9
|
this.port = config.port;
|
|
10
10
|
this.prefix = config.prefix;
|
|
11
|
+
this.subscriptions = {};
|
|
11
12
|
this.connect();
|
|
12
13
|
}
|
|
13
14
|
|
|
@@ -30,25 +31,28 @@ class RedisAsync {
|
|
|
30
31
|
connect() {
|
|
31
32
|
this._alert('connecting', 'Redis connecting...');
|
|
32
33
|
|
|
33
|
-
this.client =
|
|
34
|
+
this.client = new Redis({
|
|
34
35
|
host: this.host,
|
|
35
36
|
port: this.port,
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
lazyConnect: true,
|
|
38
|
+
retryStrategy: () => {
|
|
38
39
|
this.connected = false;
|
|
39
40
|
this._alert('connect', 'Redis disconnected');
|
|
40
|
-
|
|
41
|
+
return null;
|
|
41
42
|
},
|
|
42
43
|
});
|
|
43
44
|
|
|
44
|
-
this.client.connect()
|
|
45
|
+
this.client.connect().catch((err) => {
|
|
46
|
+
this._alert('error', `Redis Connection Error: ${err}`);
|
|
47
|
+
});
|
|
45
48
|
|
|
46
49
|
this.client.on('connect', () => {
|
|
47
50
|
this.connected = true;
|
|
48
51
|
this._alert('connect', 'Redis connected');
|
|
49
52
|
});
|
|
50
53
|
|
|
51
|
-
this.client.on('
|
|
54
|
+
this.client.on('close', () => {
|
|
55
|
+
this.connected = false;
|
|
52
56
|
this._alert('end', 'Redis end');
|
|
53
57
|
});
|
|
54
58
|
|
|
@@ -115,7 +119,7 @@ class RedisAsync {
|
|
|
115
119
|
keys = await this.client.keys(regexKey);
|
|
116
120
|
|
|
117
121
|
if (keys.length > 0) {
|
|
118
|
-
return this.client.del(keys);
|
|
122
|
+
return this.client.del(...keys);
|
|
119
123
|
}
|
|
120
124
|
}
|
|
121
125
|
} else {
|
|
@@ -128,6 +132,75 @@ class RedisAsync {
|
|
|
128
132
|
return false;
|
|
129
133
|
}
|
|
130
134
|
|
|
135
|
+
async publish(channel, data, count = 0) {
|
|
136
|
+
const prefixChannel = this.parsePrefix(channel);
|
|
137
|
+
|
|
138
|
+
if (this.connected) {
|
|
139
|
+
return this.client.publish(prefixChannel, JSON.stringify(data));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.connect();
|
|
143
|
+
if (count < 5) {
|
|
144
|
+
return this.publish(channel, data, count + 1);
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async subscribe(channel, callback) {
|
|
150
|
+
const prefixChannel = this.parsePrefix(channel);
|
|
151
|
+
|
|
152
|
+
if (!this.subscriberClient) {
|
|
153
|
+
this.subscriberClient = this.client.duplicate();
|
|
154
|
+
|
|
155
|
+
this.subscriberClient.connect().catch((err) => {
|
|
156
|
+
this._alert('error', `Redis Subscriber Error: ${err}`);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
this.subscriberClient.on('message', (chan, message) => {
|
|
160
|
+
let parsedMsg = message;
|
|
161
|
+
try {
|
|
162
|
+
parsedMsg = JSON.parse(message);
|
|
163
|
+
} catch (e) {
|
|
164
|
+
// ignore if not JSON
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (this.subscriptions[chan]) {
|
|
168
|
+
this.subscriptions[chan].forEach((cb) => cb(parsedMsg));
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!this.subscriptions[prefixChannel]) {
|
|
174
|
+
this.subscriptions[prefixChannel] = [];
|
|
175
|
+
await this.subscriberClient.subscribe(prefixChannel);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.subscriptions[prefixChannel].push(callback);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async unsubscribe(channel, callback) {
|
|
182
|
+
const prefixChannel = this.parsePrefix(channel);
|
|
183
|
+
|
|
184
|
+
if (!this.subscriptions[prefixChannel]) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (callback) {
|
|
189
|
+
this.subscriptions[prefixChannel] = this.subscriptions[prefixChannel].filter(
|
|
190
|
+
(cb) => cb !== callback
|
|
191
|
+
);
|
|
192
|
+
} else {
|
|
193
|
+
this.subscriptions[prefixChannel] = [];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (this.subscriptions[prefixChannel].length === 0) {
|
|
197
|
+
delete this.subscriptions[prefixChannel];
|
|
198
|
+
if (this.subscriberClient) {
|
|
199
|
+
await this.subscriberClient.unsubscribe(prefixChannel);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
131
204
|
setAlertCallback(callback) {
|
|
132
205
|
this.alertCallback = callback;
|
|
133
206
|
}
|