@livequery/rest 2.0.21 → 2.0.27
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.js +9 -3
- package/build/Socket.d.ts +10 -5
- package/build/Socket.js +36 -45
- package/package.json +1 -1
package/build/RestTransporter.js
CHANGED
|
@@ -40,7 +40,7 @@ export class RestTransporter {
|
|
|
40
40
|
return this.#query(ref, options);
|
|
41
41
|
}
|
|
42
42
|
#query(ref, options, hook) {
|
|
43
|
-
const http_request = merge(
|
|
43
|
+
const http_request = merge(this.socket?.$reconnect || of(1))
|
|
44
44
|
.pipe(mergeMap(() => this.call(ref, 'GET', undefined, options, hook)), map(({ data, error }) => {
|
|
45
45
|
if (error)
|
|
46
46
|
throw error;
|
|
@@ -50,6 +50,7 @@ export class RestTransporter {
|
|
|
50
50
|
if (collection.items) {
|
|
51
51
|
return {
|
|
52
52
|
data: {
|
|
53
|
+
summary: collection.summary,
|
|
53
54
|
changes: collection.items.map(data => ({ data, type: 'added', ref })),
|
|
54
55
|
paging: { ...collection, n: 0 }
|
|
55
56
|
}
|
|
@@ -59,6 +60,7 @@ export class RestTransporter {
|
|
|
59
60
|
const document = data;
|
|
60
61
|
return {
|
|
61
62
|
data: {
|
|
63
|
+
summary: collection.summary,
|
|
62
64
|
changes: [{ data: document.item, type: 'added', ref }],
|
|
63
65
|
paging: { ...collection, n: 0 }
|
|
64
66
|
}
|
|
@@ -81,14 +83,18 @@ export class RestTransporter {
|
|
|
81
83
|
...payload ? {
|
|
82
84
|
'Content-Type': 'application/json'
|
|
83
85
|
} : {},
|
|
84
|
-
...this.socket ? {
|
|
86
|
+
...this.socket ? {
|
|
87
|
+
socket_id: this.socket.client_id,
|
|
88
|
+
'x-lcid': this.socket.client_id,
|
|
89
|
+
'x-lgid': await firstValueFrom(this.socket.$gateway.pipe(map(g => g.id)))
|
|
90
|
+
} : {}
|
|
85
91
|
};
|
|
86
92
|
try {
|
|
87
93
|
const request = {
|
|
88
94
|
url: `${this.config.base_url()}/${url}${encode_query(query)}`,
|
|
89
95
|
options: {
|
|
90
96
|
method,
|
|
91
|
-
headers
|
|
97
|
+
headers,
|
|
92
98
|
...payload ? { body: JSON.stringify(payload) } : {},
|
|
93
99
|
}
|
|
94
100
|
};
|
package/build/Socket.d.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { UpdatedData } from "@livequery/types";
|
|
2
|
-
import { Observable,
|
|
2
|
+
import { Observable, BehaviorSubject } from "rxjs";
|
|
3
3
|
export declare class Socket {
|
|
4
4
|
#private;
|
|
5
5
|
private ws_url_fatory;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
readonly client_id: string;
|
|
7
|
+
readonly $gateway: BehaviorSubject<{
|
|
8
|
+
id: string;
|
|
9
|
+
}>;
|
|
10
|
+
readonly $reconnect: BehaviorSubject<number>;
|
|
10
11
|
constructor(ws_url_fatory: () => string | Promise<string>);
|
|
12
|
+
stop(): void;
|
|
11
13
|
private $sync;
|
|
14
|
+
$hello(e: {
|
|
15
|
+
gid: string;
|
|
16
|
+
}): void;
|
|
12
17
|
subscribe(realtime_token: string): void;
|
|
13
18
|
listen(ref: string): Observable<UpdatedData>;
|
|
14
19
|
}
|
package/build/Socket.js
CHANGED
|
@@ -1,71 +1,62 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { finalize } from "rxjs/operators";
|
|
1
|
+
import { fromEvent, Subject, BehaviorSubject, merge, ReplaySubject, of, interval } from "rxjs";
|
|
2
|
+
import { delay, finalize, map, mergeMap, retry, switchMap, tap } from "rxjs/operators";
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
export class Socket {
|
|
5
5
|
ws_url_fatory;
|
|
6
|
-
|
|
7
|
-
$
|
|
8
|
-
$reconnect = new
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
client_id = v4();
|
|
7
|
+
$gateway = new BehaviorSubject(undefined);
|
|
8
|
+
$reconnect = new BehaviorSubject(0);
|
|
9
|
+
#topics = new Map();
|
|
10
|
+
#$input = new ReplaySubject(1000);
|
|
11
|
+
#running;
|
|
12
|
+
#init() {
|
|
11
13
|
if (typeof WebSocket == 'undefined')
|
|
12
14
|
return;
|
|
13
|
-
|
|
14
|
-
console.log('
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
console.log(n == 0 ? 'Websocket connected' : 'Websocket re-connected');
|
|
22
|
-
ws.send(JSON.stringify({ event: 'start', data: { id: this.socket_session } }));
|
|
23
|
-
const subscription = this.$input.subscribe(data => ws.send(JSON.stringify(data)));
|
|
24
|
-
n > 0 && this.$reconnect.next();
|
|
25
|
-
return () => {
|
|
26
|
-
subscription.unsubscribe();
|
|
27
|
-
this.$input.complete();
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
await firstValueFrom(merge(fromEvent(ws, 'error'), fromEvent(ws, 'close'), fromEvent(ws, 'closed')));
|
|
31
|
-
this.$input = new ReplaySubject(1000);
|
|
32
|
-
data_sender.unsubscribe();
|
|
33
|
-
messages_handler.unsubscribe();
|
|
34
|
-
ws.close();
|
|
35
|
-
console.log(`Websocket connection dropped, reconnect in 1s ...`);
|
|
36
|
-
await new Promise(s => setTimeout(s, 1000));
|
|
37
|
-
}
|
|
15
|
+
return of(1).pipe(mergeMap(() => this.ws_url_fatory()), map(url => new WebSocket(url)), switchMap(ws => merge(fromEvent(ws, 'closed').pipe(map(e => { throw e; })), fromEvent(ws, 'close').pipe(map(e => { throw e; })), fromEvent(ws, 'error').pipe(map(e => { throw e; })), fromEvent(ws, 'open').pipe(switchMap(() => interval(60000)), tap(() => ws.send(JSON.stringify({ event: 'ping' })))), fromEvent(ws, 'open').pipe(tap(() => {
|
|
16
|
+
console.log(this.$reconnect.getValue() == 0 ? 'Websocket connected' : `Websocket re-connected (${this.$reconnect.getValue()})`);
|
|
17
|
+
this.$reconnect.next(this.$reconnect.getValue() + 1);
|
|
18
|
+
ws.send(JSON.stringify({ event: 'start', data: { id: this.client_id } }));
|
|
19
|
+
}), delay(1000), tap(() => !this.$gateway.getValue() && this.$gateway.next({ id: '' })), mergeMap(() => this.#$input), tap(data => ws.send(JSON.stringify(data)))), fromEvent(ws, 'message').pipe(tap((evt) => {
|
|
20
|
+
const e = JSON.parse(evt.data);
|
|
21
|
+
this[`$${e.event}`]?.(e);
|
|
22
|
+
}))).pipe(finalize(() => ws.close()))), retry({ delay: 1000 })).subscribe();
|
|
38
23
|
}
|
|
39
24
|
constructor(ws_url_fatory) {
|
|
40
25
|
this.ws_url_fatory = ws_url_fatory;
|
|
41
|
-
this.#init();
|
|
26
|
+
this.#running = this.#init();
|
|
42
27
|
}
|
|
43
|
-
|
|
44
|
-
|
|
28
|
+
stop() {
|
|
29
|
+
this.#running?.unsubscribe();
|
|
30
|
+
}
|
|
31
|
+
$sync(e) {
|
|
32
|
+
for (const change of e.data.changes) {
|
|
45
33
|
// Collection ref broadcast
|
|
46
|
-
this
|
|
34
|
+
this.#topics.get(change.ref)?.stream.next(change);
|
|
47
35
|
// Document ref broadcast
|
|
48
36
|
const ref = `${change.ref}/${change.data.id}`;
|
|
49
|
-
this
|
|
37
|
+
this.#topics.get(ref)?.stream.next({
|
|
50
38
|
...change,
|
|
51
39
|
ref
|
|
52
40
|
});
|
|
53
41
|
}
|
|
54
42
|
}
|
|
43
|
+
$hello(e) {
|
|
44
|
+
this.$gateway.next({ id: e.gid });
|
|
45
|
+
}
|
|
55
46
|
subscribe(realtime_token) {
|
|
56
|
-
this
|
|
47
|
+
this.#$input.next({ event: 'subscribe', data: { realtime_token } });
|
|
57
48
|
}
|
|
58
49
|
listen(ref) {
|
|
59
|
-
if (!this
|
|
50
|
+
if (!this.#topics.has(ref)) {
|
|
60
51
|
const stream = new Subject();
|
|
61
|
-
this
|
|
52
|
+
this.#topics.set(ref, { stream, listen_count: 0 });
|
|
62
53
|
}
|
|
63
|
-
this
|
|
64
|
-
return this
|
|
65
|
-
this
|
|
54
|
+
this.#topics.get(ref).listen_count++;
|
|
55
|
+
return this.#topics.get(ref).stream.pipe(finalize(() => {
|
|
56
|
+
this.#topics.get(ref).listen_count--;
|
|
66
57
|
setTimeout(() => {
|
|
67
|
-
if (this
|
|
68
|
-
this
|
|
58
|
+
if (this.#topics.get(ref).listen_count == 0) {
|
|
59
|
+
this.#$input.next({ event: 'unsubscribe', data: { ref } });
|
|
69
60
|
}
|
|
70
61
|
}, 2000);
|
|
71
62
|
}));
|