@livequery/rest 1.0.16 → 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.
@@ -10,6 +10,7 @@ export declare type RestTransporterConfig = {
10
10
  realtime?: boolean;
11
11
  };
12
12
  export declare class RestTransporter implements Transporter {
13
+ #private;
13
14
  private config;
14
15
  private queries;
15
16
  private socket;
@@ -17,7 +18,7 @@ export declare class RestTransporter implements Transporter {
17
18
  get<T>(ref: string, query?: any): Promise<T>;
18
19
  query<T extends {
19
20
  id: string;
20
- }>(query_id: number, ref: string, options?: Partial<QueryOption<T>>): Observable<QueryStream<T> | {
21
+ }>(ref: string, options?: Partial<QueryOption<T>>): Observable<QueryStream<T> | {
21
22
  error: any;
22
23
  }> & {
23
24
  reload: () => void;
@@ -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
  }
@@ -26,13 +33,13 @@ class RestTransporter {
26
33
  return yield this.call(ref, 'GET', query, null);
27
34
  });
28
35
  }
29
- query(query_id, ref, options) {
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,21 +59,20 @@ 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 || this.queries.has(query_id)) ? rxjs_2.from([]) : (this.queries.add(query_id),
57
- this.socket.listen(ref).pipe(operators_1.map((change) => ({ data: { changes: [change] } })), operators_1.finalize(() => this.queries.delete(query_id))));
58
- 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), {
59
65
  reload: () => $on_reload.next(0)
60
66
  });
61
67
  }
62
68
  call(url, method, query = {}, payload) {
63
69
  var _a, _b;
64
70
  return __awaiter(this, void 0, void 0, function* () {
65
- const headers = Object.assign(Object.assign({}, (yield ((_b = (_a = this.config).headers) === null || _b === void 0 ? void 0 : _b.call(_a))) || {}), payload ? {
71
+ const headers = Object.assign(Object.assign(Object.assign({}, (yield ((_b = (_a = this.config).headers) === null || _b === void 0 ? void 0 : _b.call(_a))) || {}), payload ? {
66
72
  'Content-Type': 'application/json'
67
- } : {});
73
+ } : {}), this.config.realtime ? { socket_id: this.socket.socket_session } : {});
68
74
  try {
69
- const { data, error, statusCode, message } = yield fetch(`${this.config.base_url()}/${url}${query ? `?${query_string_1.stringify(query)}` : ''}`, Object.assign({ method, headers: headers }, payload ? { body: JSON.stringify(payload) } : {}))
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) } : {}))
70
76
  .then(r => r.json());
71
77
  if (error)
72
78
  throw typeof error == 'string' ? { error: { message: error, code: error } } : error;
@@ -101,3 +107,9 @@ class RestTransporter {
101
107
  }
102
108
  }
103
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,11 +68,13 @@ 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
- if (this.topics.get(ref).listen_count == 0) {
74
- this.$input.next({ event: 'unsubscribe', data: { ref } });
75
- }
73
+ setTimeout(() => {
74
+ if (this.topics.get(ref).listen_count == 0) {
75
+ this.$input.next({ event: 'unsubscribe', data: { ref } });
76
+ }
77
+ }, 2000);
76
78
  }));
77
79
  return stream;
78
80
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": {
4
4
  "url": "https://github.com/livequery/rest"
5
5
  },
6
- "version": "1.0.16",
6
+ "version": "1.0.19",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "uuid": "^8.3.2"
18
18
  },
19
19
  "devDependencies": {
20
- "@livequery/types": "^1.0.24"
20
+ "@livequery/types": "^1.0.29"
21
21
  },
22
22
  "peerDependencies": {},
23
23
  "scripts": {