@livequery/rest 2.0.22 → 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.
@@ -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(of(1), this.socket?.$reconnect || of())
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;
@@ -83,14 +83,18 @@ export class RestTransporter {
83
83
  ...payload ? {
84
84
  'Content-Type': 'application/json'
85
85
  } : {},
86
- ...this.socket ? { socket_id: this.socket.socket_session } : {}
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
+ } : {}
87
91
  };
88
92
  try {
89
93
  const request = {
90
94
  url: `${this.config.base_url()}/${url}${encode_query(query)}`,
91
95
  options: {
92
96
  method,
93
- headers: headers,
97
+ headers,
94
98
  ...payload ? { body: JSON.stringify(payload) } : {},
95
99
  }
96
100
  };
package/build/Socket.d.ts CHANGED
@@ -1,14 +1,19 @@
1
1
  import { UpdatedData } from "@livequery/types";
2
- import { Observable, Subject } from "rxjs";
2
+ import { Observable, BehaviorSubject } from "rxjs";
3
3
  export declare class Socket {
4
4
  #private;
5
5
  private ws_url_fatory;
6
- private topics;
7
- private $input;
8
- readonly $reconnect: Subject<void>;
9
- readonly socket_session: string;
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 { firstValueFrom, fromEvent, Subject, merge, ReplaySubject } from "rxjs";
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
- topics = new Map();
7
- $input = new ReplaySubject(1000);
8
- $reconnect = new Subject();
9
- socket_session = v4();
10
- async #init() {
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
- for (let n = 0; true; n++) {
14
- console.log('Connecting websocket .... ');
15
- const ws = new WebSocket(await this.ws_url_fatory());
16
- const messages_handler = fromEvent(ws, 'message').subscribe((evt) => {
17
- const { data, event } = JSON.parse(evt.data);
18
- this[`$${event}`]?.(data);
19
- });
20
- const data_sender = fromEvent(ws, 'open').subscribe(() => {
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
- $sync(data) {
44
- for (const change of data.changes) {
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.topics.get(change.ref)?.stream.next(change);
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.topics.get(ref)?.stream.next({
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.$input.next({ event: 'subscribe', data: { realtime_token } });
47
+ this.#$input.next({ event: 'subscribe', data: { realtime_token } });
57
48
  }
58
49
  listen(ref) {
59
- if (!this.topics.has(ref)) {
50
+ if (!this.#topics.has(ref)) {
60
51
  const stream = new Subject();
61
- this.topics.set(ref, { stream, listen_count: 0 });
52
+ this.#topics.set(ref, { stream, listen_count: 0 });
62
53
  }
63
- this.topics.get(ref).listen_count++;
64
- return this.topics.get(ref).stream.pipe(finalize(() => {
65
- this.topics.get(ref).listen_count--;
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.topics.get(ref).listen_count == 0) {
68
- this.$input.next({ event: 'unsubscribe', data: { ref } });
58
+ if (this.#topics.get(ref).listen_count == 0) {
59
+ this.#$input.next({ event: 'unsubscribe', data: { ref } });
69
60
  }
70
61
  }, 2000);
71
62
  }));
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": {
4
4
  "url": "https://github.com/livequery/rest"
5
5
  },
6
- "version": "2.0.22",
6
+ "version": "2.0.27",
7
7
  "type": "module",
8
8
  "description": "",
9
9
  "main": "build/index.js",