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 _neo4jDriverCore = require("neo4j-driver-core");
|
15 |
|
16 | var _rxjs = require("rxjs");
|
17 |
|
18 | var _operators = require("rxjs/operators");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var States = {
|
39 | READY: 0,
|
40 | STREAMING: 1,
|
41 | COMPLETED: 2
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | var RxResult = function () {
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 | function RxResult(result) {
|
54 | (0, _classCallCheck2["default"])(this, RxResult);
|
55 | var replayedResult = result.pipe((0, _operators.publishReplay)(1), (0, _operators.refCount)());
|
56 | this._result = replayedResult;
|
57 | this._keys = replayedResult.pipe((0, _operators.flatMap)(function (r) {
|
58 | return (0, _rxjs.from)(r.keys());
|
59 | }), (0, _operators.publishReplay)(1), (0, _operators.refCount)());
|
60 | this._records = new _rxjs.Subject();
|
61 | this._summary = new _rxjs.ReplaySubject();
|
62 | this._state = States.READY;
|
63 | }
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | (0, _createClass2["default"])(RxResult, [{
|
77 | key: "keys",
|
78 | value: function keys() {
|
79 | return this._keys;
|
80 | }
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | }, {
|
91 | key: "records",
|
92 | value: function records() {
|
93 | var _this = this;
|
94 |
|
95 | return this._result.pipe((0, _operators.flatMap)(function (result) {
|
96 | return new _rxjs.Observable(function (recordsObserver) {
|
97 | return _this._startStreaming({
|
98 | result: result,
|
99 | recordsObserver: recordsObserver
|
100 | });
|
101 | });
|
102 | }));
|
103 | }
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | }, {
|
115 | key: "consume",
|
116 | value: function consume() {
|
117 | var _this2 = this;
|
118 |
|
119 | return this._result.pipe((0, _operators.flatMap)(function (result) {
|
120 | return new _rxjs.Observable(function (summaryObserver) {
|
121 | return _this2._startStreaming({
|
122 | result: result,
|
123 | summaryObserver: summaryObserver
|
124 | });
|
125 | });
|
126 | }));
|
127 | }
|
128 | }, {
|
129 | key: "_startStreaming",
|
130 | value: function _startStreaming() {
|
131 | var _this3 = this;
|
132 |
|
133 | var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
134 | result = _ref.result,
|
135 | _ref$recordsObserver = _ref.recordsObserver,
|
136 | recordsObserver = _ref$recordsObserver === void 0 ? null : _ref$recordsObserver,
|
137 | _ref$summaryObserver = _ref.summaryObserver,
|
138 | summaryObserver = _ref$summaryObserver === void 0 ? null : _ref$summaryObserver;
|
139 |
|
140 | var subscriptions = [];
|
141 |
|
142 | if (summaryObserver) {
|
143 | subscriptions.push(this._summary.subscribe(summaryObserver));
|
144 | }
|
145 |
|
146 | if (this._state < States.STREAMING) {
|
147 | this._state = States.STREAMING;
|
148 |
|
149 | if (recordsObserver) {
|
150 | subscriptions.push(this._records.subscribe(recordsObserver));
|
151 | }
|
152 |
|
153 | subscriptions.push({
|
154 | unsubscribe: function unsubscribe() {
|
155 | if (result._cancel) {
|
156 | result._cancel();
|
157 | }
|
158 | }
|
159 | });
|
160 |
|
161 | if (this._records.observers.length === 0) {
|
162 | result._cancel();
|
163 | }
|
164 |
|
165 | result.subscribe({
|
166 | onNext: function onNext(record) {
|
167 | _this3._records.next(record);
|
168 | },
|
169 | onCompleted: function onCompleted(summary) {
|
170 | _this3._records.complete();
|
171 |
|
172 | _this3._summary.next(summary);
|
173 |
|
174 | _this3._summary.complete();
|
175 |
|
176 | _this3._state = States.COMPLETED;
|
177 | },
|
178 | onError: function onError(err) {
|
179 | _this3._records.error(err);
|
180 |
|
181 | _this3._summary.error(err);
|
182 |
|
183 | _this3._state = States.COMPLETED;
|
184 | }
|
185 | });
|
186 | } else if (recordsObserver) {
|
187 | recordsObserver.error((0, _neo4jDriverCore.newError)('Streaming has already started/consumed with a previous records or summary subscription.'));
|
188 | }
|
189 |
|
190 | return function () {
|
191 | subscriptions.forEach(function (s) {
|
192 | return s.unsubscribe();
|
193 | });
|
194 | };
|
195 | }
|
196 | }]);
|
197 | return RxResult;
|
198 | }();
|
199 |
|
200 | exports["default"] = RxResult; |
\ | No newline at end of file |