@livequery/rest 1.0.18 → 1.0.19
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/build/RestTransporter.d.ts +1 -0
- package/build/RestTransporter.js +21 -8
- package/build/Socket.js +5 -5
- package/package.json +1 -1
package/build/RestTransporter.js
CHANGED
|
@@ -8,6 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
+
};
|
|
16
|
+
var _RestTransporter_instances, _RestTransporter_encode_query;
|
|
11
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
18
|
exports.RestTransporter = void 0;
|
|
13
19
|
const rxjs_1 = require("rxjs");
|
|
@@ -18,6 +24,7 @@ const query_string_1 = require("query-string");
|
|
|
18
24
|
class RestTransporter {
|
|
19
25
|
constructor(config) {
|
|
20
26
|
this.config = config;
|
|
27
|
+
_RestTransporter_instances.add(this);
|
|
21
28
|
this.queries = new Set();
|
|
22
29
|
config.realtime && (this.socket = new Socket_1.Socket(config.websocket_url));
|
|
23
30
|
}
|
|
@@ -29,10 +36,10 @@ class RestTransporter {
|
|
|
29
36
|
query(ref, options) {
|
|
30
37
|
var _a;
|
|
31
38
|
const $on_reload = new rxjs_2.Subject();
|
|
32
|
-
const $when_socket_ready = ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.$last_state.pipe(operators_1.filter(s => s == 1))) || rxjs_1.of(true);
|
|
33
|
-
const $on_can_reload = $on_reload.pipe(operators_1.filter(() => !this.socket || this.socket.$last_state.getValue() == 1));
|
|
34
|
-
const http_request = rxjs_2.merge($when_socket_ready, $on_can_reload)
|
|
35
|
-
.pipe(operators_1.mergeMap(() => this.call(ref, 'GET', options)), operators_1.map(response => {
|
|
39
|
+
const $when_socket_ready = ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.$last_state.pipe((0, operators_1.filter)(s => s == 1))) || (0, rxjs_1.of)(true);
|
|
40
|
+
const $on_can_reload = $on_reload.pipe((0, operators_1.filter)(() => !this.socket || this.socket.$last_state.getValue() == 1));
|
|
41
|
+
const http_request = (0, rxjs_2.merge)($when_socket_ready, $on_can_reload)
|
|
42
|
+
.pipe((0, operators_1.mergeMap)(() => this.call(ref, 'GET', options)), (0, operators_1.map)(response => {
|
|
36
43
|
const collection_response = response;
|
|
37
44
|
const document_response = response;
|
|
38
45
|
// If collection
|
|
@@ -52,9 +59,9 @@ class RestTransporter {
|
|
|
52
59
|
paging: { n: 0 }
|
|
53
60
|
}
|
|
54
61
|
};
|
|
55
|
-
}), operators_1.catchError(error => rxjs_1.of({ error })));
|
|
56
|
-
const websocket_sync = (!this.socket || options._cursor) ? rxjs_2.from([]) : (this.socket.listen(ref).pipe(operators_1.map((change) => ({ data: { changes: [change] } }))));
|
|
57
|
-
return Object.assign(rxjs_2.merge(http_request, websocket_sync), {
|
|
62
|
+
}), (0, operators_1.catchError)(error => (0, rxjs_1.of)({ error })));
|
|
63
|
+
const websocket_sync = (!this.socket || options._cursor) ? (0, rxjs_2.from)([]) : (this.socket.listen(ref).pipe((0, operators_1.map)((change) => ({ data: { changes: [change] } }))));
|
|
64
|
+
return Object.assign((0, rxjs_2.merge)(http_request, websocket_sync), {
|
|
58
65
|
reload: () => $on_reload.next(0)
|
|
59
66
|
});
|
|
60
67
|
}
|
|
@@ -65,7 +72,7 @@ class RestTransporter {
|
|
|
65
72
|
'Content-Type': 'application/json'
|
|
66
73
|
} : {}), this.config.realtime ? { socket_id: this.socket.socket_session } : {});
|
|
67
74
|
try {
|
|
68
|
-
const { data, error, statusCode, message } = yield fetch(`${this.config.base_url()}/${url}${
|
|
75
|
+
const { data, error, statusCode, message } = yield fetch(`${this.config.base_url()}/${url}${__classPrivateFieldGet(this, _RestTransporter_instances, "m", _RestTransporter_encode_query).call(this, query)}`, Object.assign({ method, headers: headers }, payload ? { body: JSON.stringify(payload) } : {}))
|
|
69
76
|
.then(r => r.json());
|
|
70
77
|
if (error)
|
|
71
78
|
throw typeof error == 'string' ? { error: { message: error, code: error } } : error;
|
|
@@ -100,3 +107,9 @@ class RestTransporter {
|
|
|
100
107
|
}
|
|
101
108
|
}
|
|
102
109
|
exports.RestTransporter = RestTransporter;
|
|
110
|
+
_RestTransporter_instances = new WeakSet(), _RestTransporter_encode_query = function _RestTransporter_encode_query(query) {
|
|
111
|
+
if (!query || Object.keys(query || {}).length == 0)
|
|
112
|
+
return '';
|
|
113
|
+
const encoded_query = Object.keys(query).reduce((o, key) => (Object.assign(Object.assign({}, o), { [key]: typeof query[key] == 'object' ? JSON.stringify(query[key]) : query[key] })), {});
|
|
114
|
+
return `?${(0, query_string_1.stringify)(encoded_query)}`;
|
|
115
|
+
};
|
package/build/Socket.js
CHANGED
|
@@ -19,7 +19,7 @@ class Socket {
|
|
|
19
19
|
this.topics = new Map();
|
|
20
20
|
this.$input = new rxjs_1.Subject();
|
|
21
21
|
this.$last_state = new rxjs_1.BehaviorSubject(0);
|
|
22
|
-
this.socket_session = uuid_1.v4();
|
|
22
|
+
this.socket_session = (0, uuid_1.v4)();
|
|
23
23
|
this.init();
|
|
24
24
|
}
|
|
25
25
|
init() {
|
|
@@ -30,19 +30,19 @@ class Socket {
|
|
|
30
30
|
for (let n = 0; true; n++) {
|
|
31
31
|
console.log('Try connecting websocket .... ');
|
|
32
32
|
const ws = new WebSocket(yield this.ws_url_fatory());
|
|
33
|
-
const messages_handler = rxjs_1.fromEvent(ws, 'message').subscribe((evt) => {
|
|
33
|
+
const messages_handler = (0, rxjs_1.fromEvent)(ws, 'message').subscribe((evt) => {
|
|
34
34
|
var _a;
|
|
35
35
|
const { data, event } = JSON.parse(evt.data);
|
|
36
36
|
(_a = this[`$${event}`]) === null || _a === void 0 ? void 0 : _a.call(this, data);
|
|
37
37
|
});
|
|
38
|
-
const connection_handler = rxjs_1.fromEvent(ws, 'open').subscribe(() => {
|
|
38
|
+
const connection_handler = (0, rxjs_1.fromEvent)(ws, 'open').subscribe(() => {
|
|
39
39
|
console.log('Websocket connected');
|
|
40
40
|
ws.send(JSON.stringify({ event: 'start', data: { id: this.socket_session } }));
|
|
41
41
|
const { unsubscribe } = this.$input.subscribe(data => ws.send(JSON.stringify(data)));
|
|
42
42
|
this.$last_state.next(ws.readyState);
|
|
43
43
|
return unsubscribe;
|
|
44
44
|
});
|
|
45
|
-
yield rxjs_1.firstValueFrom(rxjs_1.merge(rxjs_1.fromEvent(ws, 'error'), rxjs_1.fromEvent(ws, 'close'), rxjs_1.fromEvent(ws, 'closed')));
|
|
45
|
+
yield (0, rxjs_1.firstValueFrom)((0, rxjs_1.merge)((0, rxjs_1.fromEvent)(ws, 'error'), (0, rxjs_1.fromEvent)(ws, 'close'), (0, rxjs_1.fromEvent)(ws, 'closed')));
|
|
46
46
|
this.$last_state.next(ws.readyState);
|
|
47
47
|
connection_handler.unsubscribe();
|
|
48
48
|
messages_handler.unsubscribe();
|
|
@@ -68,7 +68,7 @@ class Socket {
|
|
|
68
68
|
this.topics.set(ref, { stream, listen_count: 0 });
|
|
69
69
|
}
|
|
70
70
|
this.topics.get(ref).listen_count++;
|
|
71
|
-
const stream = this.topics.get(ref).stream.pipe(operators_1.finalize(() => {
|
|
71
|
+
const stream = this.topics.get(ref).stream.pipe((0, operators_1.finalize)(() => {
|
|
72
72
|
this.topics.get(ref).listen_count--;
|
|
73
73
|
setTimeout(() => {
|
|
74
74
|
if (this.topics.get(ref).listen_count == 0) {
|