@livequery/client 1.0.39 → 1.0.40

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.
@@ -7,13 +7,6 @@ export type CollectionOption<T = any> = {
7
7
  reload_interval?: number;
8
8
  realtime?: boolean;
9
9
  };
10
- type CollectionStream<T> = {
11
- items: SmartQueryItem<T>[];
12
- error?: ErrorInfo;
13
- has_more: boolean;
14
- loading?: boolean;
15
- options: Partial<QueryOption<T>>;
16
- };
17
10
  export type SmartQueryItem<T> = T & {
18
11
  __removing: boolean;
19
12
  __updating: boolean;
@@ -21,6 +14,14 @@ export type SmartQueryItem<T> = T & {
21
14
  __remove: Function;
22
15
  __update: (data: Partial<T>) => any;
23
16
  __trigger: (name: string, payload?: any) => any;
17
+ __collection_ref: string;
18
+ };
19
+ type CollectionStream<T> = {
20
+ items: SmartQueryItem<T>[];
21
+ error?: ErrorInfo;
22
+ has_more: boolean;
23
+ loading?: boolean;
24
+ options: Partial<QueryOption<T>>;
24
25
  };
25
26
  export declare class CollectionObservable<T extends {
26
27
  id: string;
@@ -28,12 +29,9 @@ export declare class CollectionObservable<T extends {
28
29
  #private;
29
30
  private ref;
30
31
  private collection_options;
31
- private is_collection_ref;
32
- private collection_ref;
33
- private document_id;
34
32
  readonly $changes: Subject<UpdatedData<T>>;
35
- set_realtime(realtime: boolean): void;
36
33
  constructor(ref: string, collection_options: CollectionOption<T>);
34
+ set_realtime(realtime: boolean): void;
37
35
  private sync;
38
36
  private fetch_data;
39
37
  reload(): void;
@@ -30,16 +30,13 @@ var __rest = (this && this.__rest) || function (s, e) {
30
30
  }
31
31
  return t;
32
32
  };
33
- var _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor, _CollectionObservable_IdMap;
33
+ var _CollectionObservable_instances, _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor, _CollectionObservable_document_id, _CollectionObservable_IdMap, _CollectionObservable_refs, _CollectionObservable_ref_parser, _CollectionObservable_find_ref_by_id;
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.CollectionObservable = void 0;
36
36
  const rxjs_1 = require("rxjs");
37
37
  const get_sort_function_1 = require("./helpers/get_sort_function");
38
38
  const operators_1 = require("rxjs/operators");
39
39
  class CollectionObservable extends rxjs_1.Observable {
40
- set_realtime(realtime) {
41
- this.collection_options.realtime = realtime;
42
- }
43
40
  constructor(ref, collection_options) {
44
41
  super(o => {
45
42
  __classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
@@ -53,18 +50,23 @@ class CollectionObservable extends rxjs_1.Observable {
53
50
  });
54
51
  this.ref = ref;
55
52
  this.collection_options = collection_options;
53
+ _CollectionObservable_instances.add(this);
54
+ this.$changes = new rxjs_1.Subject();
56
55
  _CollectionObservable_$state.set(this, new rxjs_1.Subject());
57
56
  _CollectionObservable_subscriptions.set(this, new Set());
58
57
  _CollectionObservable_state.set(this, void 0);
59
- _CollectionObservable_next_cursor.set(this, null);
60
- this.$changes = new rxjs_1.Subject();
58
+ _CollectionObservable_next_cursor.set(this, {});
59
+ _CollectionObservable_document_id.set(this, void 0);
61
60
  _CollectionObservable_IdMap.set(this, new Map());
61
+ _CollectionObservable_refs.set(this, []);
62
62
  if (ref.startsWith('/') || ref.endsWith('/'))
63
63
  throw 'INVAILD_REF_FORMAT';
64
64
  const refs = ref.split('/');
65
- this.is_collection_ref = refs.length % 2 == 1;
66
- this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
67
- this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
65
+ __classPrivateFieldSet(this, _CollectionObservable_document_id, refs.length % 2 == 1 ? null : refs[refs.length - 1], "f");
66
+ __classPrivateFieldSet(this, _CollectionObservable_refs, __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_ref_parser).call(this, ref), "f");
67
+ }
68
+ set_realtime(realtime) {
69
+ this.collection_options.realtime = realtime;
68
70
  }
69
71
  sync(stream, from_local = false) {
70
72
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
@@ -88,7 +90,7 @@ class CollectionObservable extends rxjs_1.Observable {
88
90
  continue;
89
91
  const { data: payload, type } = change;
90
92
  this.$changes.next(change);
91
- const index = (_f = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.__local_id || payload.id)) !== null && _f !== void 0 ? _f : -1;
93
+ const index = (_f = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _f !== void 0 ? _f : -1;
92
94
  if (index == -1 && type == 'added') {
93
95
  if (
94
96
  // Is first value from HTTP query
@@ -130,7 +132,7 @@ class CollectionObservable extends rxjs_1.Observable {
130
132
  catch (e) { }
131
133
  return false;
132
134
  }))) {
133
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(Object.assign(Object.assign({}, payload), { __adding: false, __updating: false, __removing: false, __remove: () => this.remove(payload === null || payload === void 0 ? void 0 : payload.id), __trigger: (name, input) => this.trigger(name, input, payload === null || payload === void 0 ? void 0 : payload.id), __update: (input) => this.update(Object.assign(Object.assign({}, input), { id: payload === null || payload === void 0 ? void 0 : payload.id })) }));
135
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(Object.assign(Object.assign({}, payload), { __adding: false, __updating: false, __removing: false, __remove: () => this.remove(payload === null || payload === void 0 ? void 0 : payload.id), __trigger: (name, input) => this.trigger(name, input, payload === null || payload === void 0 ? void 0 : payload.id), __update: (input) => this.update(Object.assign(Object.assign({}, input), { id: payload === null || payload === void 0 ? void 0 : payload.id })), __collection_ref: change.ref }));
134
136
  actions.reindex = true;
135
137
  actions.update = true;
136
138
  }
@@ -166,17 +168,32 @@ class CollectionObservable extends rxjs_1.Observable {
166
168
  if (!this.ref)
167
169
  return;
168
170
  if (flush) {
171
+ __classPrivateFieldSet(this, _CollectionObservable_next_cursor, {}, "f");
169
172
  __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
170
173
  __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").clear();
171
174
  }
172
175
  flush && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
173
176
  __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");
174
177
  __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
175
- const query = this.collection_options.transporter.query(this.ref, options);
176
- const sub = Object.assign(query
177
- .pipe((0, operators_1.bufferTime)(this.collection_options.sync_delay || 500), (0, operators_1.filter)(stream => stream.length > 0))
178
- .subscribe(data => this.sync(data)), { reload: query.reload });
179
- __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(sub);
178
+ const has_more_data_refs = __classPrivateFieldGet(this, _CollectionObservable_refs, "f").filter(ref => __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] != '#');
179
+ const queries = has_more_data_refs.map(ref => (this
180
+ .collection_options
181
+ .transporter
182
+ .query(ref, Object.assign(Object.assign({}, options), { _cursor: __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] }))));
183
+ const reload = () => queries.map(q => q.reload());
184
+ const $ = (0, rxjs_1.merge)(...queries.map((q, index) => q.pipe((0, operators_1.map)(data => (Object.assign(Object.assign({}, data), { ref: has_more_data_refs[index] })))))).pipe((0, operators_1.bufferCount)(has_more_data_refs.length), (0, operators_1.map)(list => ({
185
+ error: list.find(e => e.error).error,
186
+ data: {
187
+ changes: list.map(l => l.data.changes).flat(2),
188
+ paging: {
189
+ has_more: list.some(item => { var _a, _b; return (_b = (_a = item === null || item === void 0 ? void 0 : item.data) === null || _a === void 0 ? void 0 : _a.paging) === null || _b === void 0 ? void 0 : _b.has_more; }),
190
+ n: Math.max(0, ...list.map(l => { var _a, _b; return (_b = (_a = l.data) === null || _a === void 0 ? void 0 : _a.paging) === null || _b === void 0 ? void 0 : _b.n; }).filter(x => x != undefined)),
191
+ next_cursor: list.reduce((p, c) => { var _a, _b; return (Object.assign(Object.assign({}, p), { [c.ref]: ((_b = (_a = c.data) === null || _a === void 0 ? void 0 : _a.paging) === null || _b === void 0 ? void 0 : _b.next_cursor) || '#' })); }, {})
192
+ }
193
+ }
194
+ })), (0, operators_1.toArray)(), (0, operators_1.map)(data => this.sync(data)));
195
+ const subscription = Object.assign($.subscribe(), { reload });
196
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(subscription);
180
197
  }
181
198
  reload() {
182
199
  __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.reload());
@@ -187,7 +204,6 @@ class CollectionObservable extends rxjs_1.Observable {
187
204
  fetch_more() {
188
205
  var _a;
189
206
  const options = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f")) === null || _a === void 0 ? void 0 : _a.options;
190
- __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f") && (options._cursor = __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f"));
191
207
  this.fetch_data(options);
192
208
  }
193
209
  filter(filters) {
@@ -195,24 +211,25 @@ class CollectionObservable extends rxjs_1.Observable {
195
211
  }
196
212
  add(payload) {
197
213
  return __awaiter(this, void 0, void 0, function* () {
198
- return yield this.collection_options.transporter.add(`${this.collection_ref}`, payload);
214
+ if (this.ref.includes('.'))
215
+ throw 'INVAILD_COLLECTION_REF_FOR_ADDING';
216
+ return yield this.collection_options.transporter.add(`${this.ref}`, payload);
199
217
  });
200
218
  }
201
219
  update(_a) {
202
220
  var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
203
221
  return __awaiter(this, void 0, void 0, function* () {
204
- const id = update_payload_id || this.document_id;
222
+ const { id, ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, update_payload_id);
205
223
  // Trigger local update
206
224
  this.sync([{
207
225
  data: {
208
226
  changes: [{
209
227
  data: Object.assign(Object.assign({}, payload), { id, __updating: true }),
210
- ref: this.ref,
228
+ ref,
211
229
  type: 'modified'
212
230
  }]
213
231
  }
214
232
  }], true);
215
- const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
216
233
  try {
217
234
  return yield this.collection_options.transporter.update(ref, payload);
218
235
  }
@@ -221,7 +238,7 @@ class CollectionObservable extends rxjs_1.Observable {
221
238
  data: {
222
239
  changes: [{
223
240
  data: { id, __updating: false },
224
- ref: this.ref,
241
+ ref,
225
242
  type: 'modified'
226
243
  }]
227
244
  }
@@ -232,18 +249,17 @@ class CollectionObservable extends rxjs_1.Observable {
232
249
  }
233
250
  remove(remove_document_id) {
234
251
  return __awaiter(this, void 0, void 0, function* () {
235
- const id = remove_document_id || this.document_id;
252
+ const { id, ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, remove_document_id);
236
253
  this.sync([{
237
254
  data: {
238
255
  changes: [{
239
256
  data: { id, __removing: true },
240
- ref: this.ref,
257
+ ref,
241
258
  type: 'modified'
242
259
  }]
243
260
  }
244
261
  }], true);
245
262
  // Trigger
246
- const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
247
263
  try {
248
264
  return yield this.collection_options.transporter.remove(ref);
249
265
  }
@@ -252,7 +268,7 @@ class CollectionObservable extends rxjs_1.Observable {
252
268
  data: {
253
269
  changes: [{
254
270
  data: { id, __removing: false },
255
- ref: this.ref,
271
+ ref,
256
272
  type: 'modified'
257
273
  }]
258
274
  }
@@ -263,11 +279,29 @@ class CollectionObservable extends rxjs_1.Observable {
263
279
  }
264
280
  trigger(name, payload, trigger_document_id) {
265
281
  return __awaiter(this, void 0, void 0, function* () {
266
- const id = trigger_document_id || this.document_id;
267
- const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
282
+ const { ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, trigger_document_id);
268
283
  return yield this.collection_options.transporter.trigger(ref, name, {}, payload);
269
284
  });
270
285
  }
271
286
  }
272
287
  exports.CollectionObservable = CollectionObservable;
273
- _CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap(), _CollectionObservable_IdMap = new WeakMap();
288
+ _CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap(), _CollectionObservable_document_id = new WeakMap(), _CollectionObservable_IdMap = new WeakMap(), _CollectionObservable_refs = new WeakMap(), _CollectionObservable_instances = new WeakSet(), _CollectionObservable_ref_parser = function _CollectionObservable_ref_parser(path) {
289
+ const refs_builder = (paths) => {
290
+ const [a, b, ...c] = paths;
291
+ if (!b)
292
+ return paths;
293
+ const r = a.map(aa => b.map(bb => `${aa}/${bb}`)).flat(2);
294
+ const d = [r, ...c];
295
+ return refs_builder(d);
296
+ };
297
+ const b = path.split('/').map(l => l.split('.'));
298
+ return refs_builder(b).flat(2);
299
+ }, _CollectionObservable_find_ref_by_id = function _CollectionObservable_find_ref_by_id(id = __classPrivateFieldGet(this, _CollectionObservable_document_id, "f")) {
300
+ if (!id)
301
+ throw 'ID_NOT_FOUND';
302
+ const collection_ref = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[__classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(id)].__collection_ref;
303
+ if (!collection_ref)
304
+ throw 'COLLECTION_REF_NOT_FOUND';
305
+ const ref = `${collection_ref}${id ? `/${id}` : ''}`;
306
+ return { ref, id, collection_ref };
307
+ };
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.39",
6
+ "version": "1.0.40",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",