1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports["default"] = void 0;
|
9 |
|
10 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
11 |
|
12 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
13 |
|
14 | var _rxjs = require("rxjs");
|
15 |
|
16 | var _operators = require("rxjs/operators");
|
17 |
|
18 | var _resultRx = _interopRequireDefault(require("./result-rx"));
|
19 |
|
20 | var _session = _interopRequireDefault(require("./session"));
|
21 |
|
22 | var _transactionRx = _interopRequireDefault(require("./transaction-rx"));
|
23 |
|
24 | var _constants = require("./internal/constants");
|
25 |
|
26 | var _txConfig = _interopRequireDefault(require("./internal/tx-config"));
|
27 |
|
28 | var _retryLogicRx = _interopRequireDefault(require("./internal/retry-logic-rx"));
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | var RxSession =
|
53 |
|
54 | function () {
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function RxSession() {
|
63 | var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
64 | session = _ref.session,
|
65 | config = _ref.config;
|
66 |
|
67 | (0, _classCallCheck2["default"])(this, RxSession);
|
68 | this._session = session;
|
69 | this._retryLogic = _createRetryLogic(config);
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | (0, _createClass2["default"])(RxSession, [{
|
84 | key: "run",
|
85 | value: function run(query, parameters, transactionConfig) {
|
86 | var _this = this;
|
87 |
|
88 | return new _resultRx["default"](new _rxjs.Observable(function (observer) {
|
89 | try {
|
90 | observer.next(_this._session.run(query, parameters, transactionConfig));
|
91 | observer.complete();
|
92 | } catch (err) {
|
93 | observer.error(err);
|
94 | }
|
95 |
|
96 | return function () {};
|
97 | }));
|
98 | }
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | }, {
|
108 | key: "beginTransaction",
|
109 | value: function beginTransaction(transactionConfig) {
|
110 | return this._beginTransaction(this._session._mode, transactionConfig);
|
111 | }
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | }, {
|
122 | key: "readTransaction",
|
123 | value: function readTransaction(work, transactionConfig) {
|
124 | return this._runTransaction(_constants.ACCESS_MODE_READ, work, transactionConfig);
|
125 | }
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | }, {
|
136 | key: "writeTransaction",
|
137 | value: function writeTransaction(work, transactionConfig) {
|
138 | return this._runTransaction(_constants.ACCESS_MODE_WRITE, work, transactionConfig);
|
139 | }
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | }, {
|
148 | key: "close",
|
149 | value: function close() {
|
150 | var _this2 = this;
|
151 |
|
152 | return new _rxjs.Observable(function (observer) {
|
153 | _this2._session.close().then(function () {
|
154 | observer.complete();
|
155 | })["catch"](function (err) {
|
156 | return observer.error(err);
|
157 | });
|
158 | });
|
159 | }
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | }, {
|
173 | key: "lastBookmark",
|
174 | value: function lastBookmark() {
|
175 | return this._session.lastBookmark();
|
176 | }
|
177 | |
178 |
|
179 |
|
180 |
|
181 | }, {
|
182 | key: "_beginTransaction",
|
183 | value: function _beginTransaction(accessMode, transactionConfig) {
|
184 | var _this3 = this;
|
185 |
|
186 | var txConfig = _txConfig["default"].empty();
|
187 |
|
188 | if (transactionConfig) {
|
189 | txConfig = new _txConfig["default"](transactionConfig);
|
190 | }
|
191 |
|
192 | return new _rxjs.Observable(function (observer) {
|
193 | try {
|
194 | observer.next(new _transactionRx["default"](_this3._session._beginTransaction(accessMode, txConfig)));
|
195 | observer.complete();
|
196 | } catch (err) {
|
197 | observer.error(err);
|
198 | }
|
199 |
|
200 | return function () {};
|
201 | });
|
202 | }
|
203 | |
204 |
|
205 |
|
206 |
|
207 | }, {
|
208 | key: "_runTransaction",
|
209 | value: function _runTransaction(accessMode, work, transactionConfig) {
|
210 | var txConfig = _txConfig["default"].empty();
|
211 |
|
212 | if (transactionConfig) {
|
213 | txConfig = new _txConfig["default"](transactionConfig);
|
214 | }
|
215 |
|
216 | return this._retryLogic.retry(this._beginTransaction(accessMode, transactionConfig).pipe((0, _operators.flatMap)(function (txc) {
|
217 | return (0, _rxjs.defer)(function () {
|
218 | try {
|
219 | return work(txc);
|
220 | } catch (err) {
|
221 | return (0, _rxjs.throwError)(err);
|
222 | }
|
223 | }).pipe((0, _operators.catchError)(function (err) {
|
224 | return txc.rollback().pipe((0, _operators.concat)((0, _rxjs.throwError)(err)));
|
225 | }), (0, _operators.concat)(txc.commit()));
|
226 | })));
|
227 | }
|
228 | }]);
|
229 | return RxSession;
|
230 | }();
|
231 |
|
232 | exports["default"] = RxSession;
|
233 |
|
234 | function _createRetryLogic(config) {
|
235 | var maxRetryTimeout = config && config.maxTransactionRetryTime ? config.maxTransactionRetryTime : null;
|
236 | return new _retryLogicRx["default"]({
|
237 | maxRetryTimeout: maxRetryTimeout
|
238 | });
|
239 | } |
\ | No newline at end of file |