UNPKG

7.95 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = void 0;
9
10var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
12var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
14var _rxjs = require("rxjs");
15
16var _operators = require("rxjs/operators");
17
18var _resultRx = _interopRequireDefault(require("./result-rx"));
19
20var _session = _interopRequireDefault(require("./session"));
21
22var _transactionRx = _interopRequireDefault(require("./transaction-rx"));
23
24var _constants = require("./internal/constants");
25
26var _txConfig = _interopRequireDefault(require("./internal/tx-config"));
27
28var _retryLogicRx = _interopRequireDefault(require("./internal/retry-logic-rx"));
29
30/**
31 * Copyright (c) 2002-2019 "Neo4j,"
32 * Neo4j Sweden AB [http://neo4j.com]
33 *
34 * This file is part of Neo4j.
35 *
36 * Licensed under the Apache License, Version 2.0 (the "License");
37 * you may not use this file except in compliance with the License.
38 * You may obtain a copy of the License at
39 *
40 * http://www.apache.org/licenses/LICENSE-2.0
41 *
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS,
44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 * See the License for the specific language governing permissions and
46 * limitations under the License.
47 */
48
49/**
50 * A Reactive session, which provides the same functionality as {@link Session} but through a Reactive API.
51 */
52var RxSession =
53/*#__PURE__*/
54function () {
55 /**
56 * Constructs a reactive session with given default session instance and provided driver configuration.
57 *
58 * @protected
59 * @param {Object} param - Object parameter
60 * @param {Session} param.session - The underlying session instance to relay requests
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 * Creates a reactive result that will execute the query with the provided parameters and the provided
73 * transaction configuration that applies to the underlying auto-commit transaction.
74 *
75 * @public
76 * @param {string} query - Query to be executed.
77 * @param {Object} parameters - Parameter values to use in query execution.
78 * @param {TransactionConfig} transactionConfig - Configuration for the new auto-commit transaction.
79 * @returns {RxResult} - A reactive result
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 * Starts a new explicit transaction with the provided transaction configuration.
101 *
102 * @public
103 * @param {TransactionConfig} transactionConfig - Configuration for the new transaction.
104 * @returns {Observable<RxTransaction>} - A reactive stream that will generate at most **one** RxTransaction instance.
105 */
106
107 }, {
108 key: "beginTransaction",
109 value: function beginTransaction(transactionConfig) {
110 return this._beginTransaction(this._session._mode, transactionConfig);
111 }
112 /**
113 * Executes the provided unit of work in a {@link READ} reactive transaction which is created with the provided
114 * transaction configuration.
115 * @public
116 * @param {function(txc: RxTransaction): Observable} work - A unit of work to be executed.
117 * @param {TransactionConfig} transactionConfig - Configuration for the enclosing transaction created by the driver.
118 * @returns {Observable} - A reactive stream returned by the unit of work.
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 * Executes the provided unit of work in a {@link WRITE} reactive transaction which is created with the provided
128 * transaction configuration.
129 * @public
130 * @param {function(txc: RxTransaction): Observable} work - A unit of work to be executed.
131 * @param {TransactionConfig} transactionConfig - Configuration for the enclosing transaction created by the driver.
132 * @returns {Observable} - A reactive stream returned by the unit of work.
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 * Closes this reactive session.
142 *
143 * @public
144 * @returns {Observable} - An empty reactive stream
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 * Returns the bookmark received following the last successfully completed query, which is executed
162 * either in an {@link RxTransaction} obtained from this session instance or directly through one of
163 * the {@link RxSession#run} method of this session instance.
164 *
165 * If no bookmark was received or if this transaction was rolled back, the bookmark value will not be
166 * changed.
167 *
168 * @public
169 * @returns {string}
170 */
171
172 }, {
173 key: "lastBookmark",
174 value: function lastBookmark() {
175 return this._session.lastBookmark();
176 }
177 /**
178 * @private
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 * @private
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
232exports["default"] = RxSession;
233
234function _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