@livequery/client 1.0.27 → 1.0.29

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.
@@ -1,19 +1,19 @@
1
- import { Observable } from 'rxjs';
2
- import { ErrorInfo, QueryOption, Transporter } from '@livequery/types';
3
- export declare type CollectionOption<T = any> = {
1
+ import { Subject, Observable } from 'rxjs';
2
+ import { ErrorInfo, QueryOption, Transporter, UpdatedData } from '@livequery/types';
3
+ export type CollectionOption<T = any> = {
4
4
  transporter: Transporter;
5
5
  sync_delay?: number;
6
6
  filters?: Partial<QueryOption<T>>;
7
7
  reload_interval?: number;
8
8
  };
9
- declare type CollectionStream<T> = {
9
+ type CollectionStream<T> = {
10
10
  items: SmartQueryItem<T>[];
11
11
  error?: ErrorInfo;
12
12
  has_more: boolean;
13
13
  loading?: boolean;
14
14
  options: Partial<QueryOption<T>>;
15
15
  };
16
- export declare type SmartQueryItem<T> = T & {
16
+ export type SmartQueryItem<T> = T & {
17
17
  __removing: boolean;
18
18
  __updating: boolean;
19
19
  __adding: boolean;
@@ -30,6 +30,7 @@ export declare class CollectionObservable<T extends {
30
30
  private is_collection_ref;
31
31
  private collection_ref;
32
32
  private document_id;
33
+ readonly $changes: Subject<UpdatedData<T>>;
33
34
  constructor(ref: string, collection_options: CollectionOption<T>);
34
35
  private push_item;
35
36
  private sync;
@@ -8,18 +8,16 @@ 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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
12
- if (!privateMap.has(receiver)) {
13
- throw new TypeError("attempted to set private field on non-instance");
14
- }
15
- privateMap.set(receiver, value);
16
- return value;
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
17
16
  };
18
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
19
- if (!privateMap.has(receiver)) {
20
- throw new TypeError("attempted to get private field on non-instance");
21
- }
22
- return privateMap.get(receiver);
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ 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");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
23
21
  };
24
22
  var __rest = (this && this.__rest) || function (s, e) {
25
23
  var t = {};
@@ -32,30 +30,31 @@ var __rest = (this && this.__rest) || function (s, e) {
32
30
  }
33
31
  return t;
34
32
  };
35
- var _$state, _subscriptions, _state, _next_cursor;
33
+ var _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor, _CollectionObservable_IdMap;
36
34
  Object.defineProperty(exports, "__esModule", { value: true });
37
35
  exports.CollectionObservable = void 0;
38
36
  const rxjs_1 = require("rxjs");
39
- const get_sort_function_1 = require("./helpers/get_sort_function");
40
37
  const operators_1 = require("rxjs/operators");
41
38
  class CollectionObservable extends rxjs_1.Observable {
42
39
  constructor(ref, collection_options) {
43
40
  super(o => {
44
- __classPrivateFieldSet(this, _state, { items: [], options: collection_options.filters, has_more: false });
45
- const subscription = __classPrivateFieldGet(this, _$state).subscribe(o);
41
+ __classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
42
+ const subscription = __classPrivateFieldGet(this, _CollectionObservable_$state, "f").subscribe(o);
46
43
  const auto_reload_interval = collection_options.reload_interval && setInterval(() => this.reload(), collection_options.reload_interval);
47
44
  return () => {
48
- __classPrivateFieldGet(this, _subscriptions).forEach(s => s.unsubscribe());
45
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
49
46
  subscription.unsubscribe();
50
47
  clearInterval(auto_reload_interval);
51
48
  };
52
49
  });
53
50
  this.ref = ref;
54
51
  this.collection_options = collection_options;
55
- _$state.set(this, new rxjs_1.Subject());
56
- _subscriptions.set(this, new Set());
57
- _state.set(this, void 0);
58
- _next_cursor.set(this, null);
52
+ _CollectionObservable_$state.set(this, new rxjs_1.Subject());
53
+ _CollectionObservable_subscriptions.set(this, new Set());
54
+ _CollectionObservable_state.set(this, void 0);
55
+ _CollectionObservable_next_cursor.set(this, null);
56
+ this.$changes = new rxjs_1.Subject();
57
+ _CollectionObservable_IdMap.set(this, new Map());
59
58
  if (ref.startsWith('/') || ref.endsWith('/'))
60
59
  throw 'INVAILD_REF_FORMAT';
61
60
  const refs = ref.split('/');
@@ -65,21 +64,24 @@ class CollectionObservable extends rxjs_1.Observable {
65
64
  }
66
65
  push_item(data) {
67
66
  const item = Object.assign(Object.assign({}, data), { __adding: false, __updating: false, __removing: false, __remove: () => this.remove(data === null || data === void 0 ? void 0 : data.id), __trigger: (name, payload) => this.trigger(name, payload, data === null || data === void 0 ? void 0 : data.id), __update: (payload) => this.update(Object.assign(Object.assign({}, payload), { id: data === null || data === void 0 ? void 0 : data.id })) });
68
- __classPrivateFieldGet(this, _state).items.push(item);
67
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(item);
68
+ __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.length - 1);
69
69
  }
70
70
  sync(stream) {
71
71
  var _a, _b, _c, _d;
72
72
  for (const { data, error } of stream) {
73
73
  // Error & paging
74
- error && (__classPrivateFieldGet(this, _state).error = error);
74
+ error && (__classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error);
75
75
  if (((_a = data === null || data === void 0 ? void 0 : data.paging) === null || _a === void 0 ? void 0 : _a.n) == 0) {
76
- __classPrivateFieldGet(this, _state).has_more = (_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.has_more;
77
- __classPrivateFieldSet(this, _next_cursor, (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor);
78
- __classPrivateFieldGet(this, _state).loading = false;
76
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = (_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.has_more;
77
+ __classPrivateFieldSet(this, _CollectionObservable_next_cursor, (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor, "f");
78
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
79
79
  }
80
80
  // Sync
81
- for (const { data: payload, type } of (data === null || data === void 0 ? void 0 : data.changes) || []) {
82
- const index = __classPrivateFieldGet(this, _state).items.findIndex(item => item.id == payload.id);
81
+ for (const change of (data === null || data === void 0 ? void 0 : data.changes) || []) {
82
+ const { data: payload, type } = change;
83
+ this.$changes.next(change);
84
+ const index = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id) || -1;
83
85
  if (index == -1 && type == 'added') {
84
86
  if (
85
87
  // Is first value from HTTP query
@@ -87,14 +89,14 @@ class CollectionObservable extends rxjs_1.Observable {
87
89
  || (
88
90
  // Is realtime update that match filters
89
91
  Object
90
- .keys(__classPrivateFieldGet(this, _state).options || {})
92
+ .keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
91
93
  .filter(key => !key.includes('_'))
92
94
  .every(key => {
93
95
  var _a;
94
96
  try {
95
97
  const [field, expression] = key.split(':');
96
98
  const a = payload[field];
97
- const b = (_a = __classPrivateFieldGet(this, _state).options) === null || _a === void 0 ? void 0 : _a[field];
99
+ const b = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f").options) === null || _a === void 0 ? void 0 : _a[field];
98
100
  if (!expression)
99
101
  return a == b;
100
102
  if (expression == 'ne')
@@ -126,43 +128,46 @@ class CollectionObservable extends rxjs_1.Observable {
126
128
  }
127
129
  if (index >= 0) {
128
130
  if (type == 'added' || type == 'modified') {
129
- __classPrivateFieldGet(this, _state).items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _state).items[index]), payload), { __adding: false, __updating: false, __removing: false });
131
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index]), payload), { __adding: false, __updating: false, __removing: false });
130
132
  }
131
133
  if (type == 'removed') {
132
- __classPrivateFieldGet(this, _state).items.splice(index, 1);
134
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
135
+ for (const [document_id, i] of __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f")) {
136
+ i == index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").delete(document_id);
137
+ i > index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(document_id, i - 1);
138
+ }
133
139
  }
134
140
  }
135
141
  }
136
142
  }
137
- const sort_function = get_sort_function_1.get_sort_function(__classPrivateFieldGet(this, _state).items[0], __classPrivateFieldGet(this, _state).options._order_by || 'created_at', __classPrivateFieldGet(this, _state).options._sort || 'desc');
138
- __classPrivateFieldGet(this, _state).items = __classPrivateFieldGet(this, _state).items.sort(sort_function);
139
- __classPrivateFieldGet(this, _$state).next(__classPrivateFieldGet(this, _state));
143
+ __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
140
144
  }
141
145
  fetch_data(options = {}, flush = false) {
142
146
  if (!this.ref)
143
147
  return;
144
148
  if (flush) {
145
- __classPrivateFieldGet(this, _subscriptions).forEach(s => s.unsubscribe());
146
- __classPrivateFieldGet(this, _subscriptions).clear();
149
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
150
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").clear();
147
151
  }
148
- __classPrivateFieldSet(this, _state, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _state)), { items: flush ? [] : __classPrivateFieldGet(this, _state).items, error: null, loading: true, options }));
149
- __classPrivateFieldGet(this, _$state).next(__classPrivateFieldGet(this, _state));
152
+ flush && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
153
+ __classPrivateFieldSet(this, _CollectionObservable_state, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _CollectionObservable_state, "f")), { items: flush ? [] : __classPrivateFieldGet(this, _CollectionObservable_state, "f").items, error: null, loading: true, options }), "f");
154
+ __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
150
155
  const query = this.collection_options.transporter.query(this.ref, options);
151
156
  const sub = Object.assign(query
152
- .pipe(operators_1.bufferTime(this.collection_options.sync_delay || 500), operators_1.filter(stream => stream.length > 0))
157
+ .pipe((0, operators_1.bufferTime)(this.collection_options.sync_delay || 500), (0, operators_1.filter)(stream => stream.length > 0))
153
158
  .subscribe(data => this.sync(data)), { reload: query.reload });
154
- __classPrivateFieldGet(this, _subscriptions).add(sub);
159
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(sub);
155
160
  }
156
161
  reload() {
157
- __classPrivateFieldGet(this, _subscriptions).forEach(s => s.reload());
162
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.reload());
158
163
  }
159
164
  reset() {
160
165
  this.fetch_data({}, true);
161
166
  }
162
167
  fetch_more() {
163
168
  var _a;
164
- const options = (_a = __classPrivateFieldGet(this, _state)) === null || _a === void 0 ? void 0 : _a.options;
165
- __classPrivateFieldGet(this, _next_cursor) && (options._cursor = __classPrivateFieldGet(this, _next_cursor));
169
+ const options = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f")) === null || _a === void 0 ? void 0 : _a.options;
170
+ __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f") && (options._cursor = __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f"));
166
171
  this.fetch_data(options);
167
172
  }
168
173
  filter(filters) {
@@ -217,4 +222,4 @@ class CollectionObservable extends rxjs_1.Observable {
217
222
  }
218
223
  }
219
224
  exports.CollectionObservable = CollectionObservable;
220
- _$state = new WeakMap(), _subscriptions = new WeakMap(), _state = new WeakMap(), _next_cursor = new WeakMap();
225
+ _CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap(), _CollectionObservable_IdMap = new WeakMap();
package/build/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": {
4
4
  "url": "https://github.com/livequery/client"
5
5
  },
6
- "version": "1.0.27",
6
+ "version": "1.0.29",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",
@@ -12,6 +12,7 @@
12
12
  ],
13
13
  "dependencies": {
14
14
  "rxjs": "^7.1.0",
15
+ "typescript": "^4.9.5",
15
16
  "uuid": "^8.3.2"
16
17
  },
17
18
  "devDependencies": {