UNPKG

8.31 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [0, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37Object.defineProperty(exports, "__esModule", { value: true });
38var Transaction = (function () {
39 /**
40 * Constructor
41 */
42 function Transaction(client) {
43 this._client = client;
44 this._multi = client.client.multi();
45 }
46 /**
47 * Commit a transaction
48 */
49 Transaction.prototype.commit = function () {
50 return __awaiter(this, void 0, void 0, function () {
51 var result;
52 return __generator(this, function (_a) {
53 switch (_a.label) {
54 case 0:
55 this._checkDisposed();
56 return [4 /*yield*/, this._multi.execAsync()];
57 case 1:
58 result = _a.sent();
59 this.rollback();
60 if (!result) {
61 throw new Error('Transaction failed');
62 }
63 return [2 /*return*/];
64 }
65 });
66 });
67 };
68 /**
69 * Roll the transaction back
70 */
71 Transaction.prototype.rollback = function () {
72 this._checkDisposed();
73 this._multi.discard();
74 this._multi = null;
75 };
76 Object.defineProperty(Transaction.prototype, "isOpen", {
77 /**
78 * Is a transaction still open
79 */
80 get: function () {
81 return !!this._multi;
82 },
83 enumerable: true,
84 configurable: true
85 });
86 /**
87 * Set a key value
88 */
89 Transaction.prototype.set = function (key, value, options) {
90 return __awaiter(this, void 0, void 0, function () {
91 return __generator(this, function (_a) {
92 this._checkDisposed();
93 this._multi.set(key, value);
94 return [2 /*return*/, this._checkExpiry(key, options)];
95 });
96 });
97 };
98 /**
99 * Increment an integer value
100 */
101 Transaction.prototype.incrby = function (key, value, options) {
102 return __awaiter(this, void 0, void 0, function () {
103 return __generator(this, function (_a) {
104 this._checkDisposed();
105 this._multi.incrby(key, value);
106 return [2 /*return*/, this._checkExpiry(key, options)];
107 });
108 });
109 };
110 /**
111 * Increment a float value
112 */
113 Transaction.prototype.incrbyfloat = function (key, value, options) {
114 return __awaiter(this, void 0, void 0, function () {
115 return __generator(this, function (_a) {
116 this._checkDisposed();
117 this._multi.incrbyfloat(key, value);
118 return [2 /*return*/, this._checkExpiry(key, options)];
119 });
120 });
121 };
122 /**
123 * Increment an integer value in a hash set
124 */
125 Transaction.prototype.hincrby = function (key, hashKey, value, options) {
126 return __awaiter(this, void 0, void 0, function () {
127 return __generator(this, function (_a) {
128 this._checkDisposed();
129 this._multi.hincrby(key, hashKey, value);
130 return [2 /*return*/, this._checkExpiry(key, options)];
131 });
132 });
133 };
134 /**
135 * Increment a float value in a hash set
136 */
137 Transaction.prototype.hincrbyfloat = function (key, hashKey, value, options) {
138 return __awaiter(this, void 0, void 0, function () {
139 return __generator(this, function (_a) {
140 this._checkDisposed();
141 this._multi.hincrbyfloat(key, hashKey, value);
142 return [2 /*return*/, this._checkExpiry(key, options)];
143 });
144 });
145 };
146 /**
147 * Hash map set
148 */
149 Transaction.prototype.hmset = function (key, hash, options) {
150 return __awaiter(this, void 0, void 0, function () {
151 return __generator(this, function (_a) {
152 this._checkDisposed();
153 this._multi.hmset(key, hash);
154 return [2 /*return*/, this._checkExpiry(key, options)];
155 });
156 });
157 };
158 /**
159 * Delete a key
160 */
161 Transaction.prototype.del = function (key) {
162 return __awaiter(this, void 0, void 0, function () {
163 return __generator(this, function (_a) {
164 this._checkDisposed();
165 this._multi.del(key);
166 return [2 /*return*/];
167 });
168 });
169 };
170 /**
171 * Add to a set
172 */
173 Transaction.prototype.sadd = function (key, value, options) {
174 return __awaiter(this, void 0, void 0, function () {
175 return __generator(this, function (_a) {
176 this._checkDisposed();
177 this._multi.sadd(key, value);
178 return [2 /*return*/, this._checkExpiry(key, options)];
179 });
180 });
181 };
182 /**
183 * Remove from set
184 */
185 Transaction.prototype.srem = function (key, value) {
186 return __awaiter(this, void 0, void 0, function () {
187 return __generator(this, function (_a) {
188 this._checkDisposed();
189 this._multi.srem(key, value);
190 return [2 /*return*/];
191 });
192 });
193 };
194 /**
195 * Set the expiry in seconds
196 */
197 Transaction.prototype._checkExpiry = function (key, options) {
198 return __awaiter(this, void 0, void 0, function () {
199 return __generator(this, function (_a) {
200 if (options && options.expire) {
201 this._multi.expire(key, options.expire);
202 }
203 return [2 /*return*/];
204 });
205 });
206 };
207 /**
208 * Check to make sure this transaction is still open
209 */
210 Transaction.prototype._checkDisposed = function () {
211 if (!this._multi) {
212 throw new Error('Transaction disposed');
213 }
214 };
215 return Transaction;
216}());
217exports.default = Transaction;
218//# sourceMappingURL=transaction.js.map
\No newline at end of file