@livequery/client 1.0.39 → 1.0.41

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,46 +50,51 @@ 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
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
72
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
71
73
  const realtime = (_a = this.collection_options.realtime) !== null && _a !== void 0 ? _a : true;
72
74
  const actions = { update: false, reindex: false };
73
- for (const { data, error } of stream) {
75
+ for (const { data, error, ref } of stream) {
74
76
  // Error & paging
75
77
  if (error) {
76
78
  __classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error;
77
79
  actions.update = true;
78
80
  }
79
81
  if (((_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.n) == 0) {
80
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.has_more;
81
- __classPrivateFieldSet(this, _CollectionObservable_next_cursor, (_d = data === null || data === void 0 ? void 0 : data.paging) === null || _d === void 0 ? void 0 : _d.next_cursor, "f");
82
+ __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] = (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor;
83
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = Object.values(__classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")).some(v => v && v != '#');
82
84
  __classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
83
85
  actions.update = true;
84
86
  }
85
87
  // Sync
86
88
  for (const change of (data === null || data === void 0 ? void 0 : data.changes) || []) {
87
- if (!((_e = change === null || change === void 0 ? void 0 : change.data) === null || _e === void 0 ? void 0 : _e.id))
89
+ if (!((_d = change === null || change === void 0 ? void 0 : change.data) === null || _d === void 0 ? void 0 : _d.id))
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 = (_e = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _e !== void 0 ? _e : -1;
92
94
  if (index == -1 && type == 'added') {
93
95
  if (
94
96
  // Is first value from HTTP query
95
- ((_g = data === null || data === void 0 ? void 0 : data.paging) === null || _g === void 0 ? void 0 : _g.n) == 0
97
+ ((_f = data === null || data === void 0 ? void 0 : data.paging) === null || _f === void 0 ? void 0 : _f.n) == 0
96
98
  || (
97
99
  // Is realtime update that match filters
98
100
  (realtime || from_local) && Object
@@ -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
  }
@@ -156,7 +158,7 @@ class CollectionObservable extends rxjs_1.Observable {
156
158
  }
157
159
  }
158
160
  if (actions.reindex) {
159
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").items = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.sort((0, get_sort_function_1.get_sort_function)(__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[0], ((_j = (_h = this.collection_options) === null || _h === void 0 ? void 0 : _h.filters) === null || _j === void 0 ? void 0 : _j._order_by) || 'created_at', (_l = (_k = this.collection_options) === null || _k === void 0 ? void 0 : _k.filters) === null || _l === void 0 ? void 0 : _l._sort));
161
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.sort((0, get_sort_function_1.get_sort_function)(__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[0], ((_h = (_g = this.collection_options) === null || _g === void 0 ? void 0 : _g.filters) === null || _h === void 0 ? void 0 : _h._order_by) || 'created_at', (_k = (_j = this.collection_options) === null || _j === void 0 ? void 0 : _j.filters) === null || _k === void 0 ? void 0 : _k._sort));
160
162
  __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
161
163
  __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.map((item, index) => __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, index));
162
164
  }
@@ -166,17 +168,22 @@ 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.bufferTime)(500), (0, operators_1.filter)(list => list.length > 0), (0, operators_1.map)(data => this.sync(data)));
185
+ const subscription = Object.assign($.subscribe(), { reload });
186
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(subscription);
180
187
  }
181
188
  reload() {
182
189
  __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.reload());
@@ -187,7 +194,6 @@ class CollectionObservable extends rxjs_1.Observable {
187
194
  fetch_more() {
188
195
  var _a;
189
196
  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
197
  this.fetch_data(options);
192
198
  }
193
199
  filter(filters) {
@@ -195,33 +201,36 @@ class CollectionObservable extends rxjs_1.Observable {
195
201
  }
196
202
  add(payload) {
197
203
  return __awaiter(this, void 0, void 0, function* () {
198
- return yield this.collection_options.transporter.add(`${this.collection_ref}`, payload);
204
+ if (this.ref.includes('.'))
205
+ throw 'INVAILD_COLLECTION_REF_FOR_ADDING';
206
+ return yield this.collection_options.transporter.add(`${this.ref}`, payload);
199
207
  });
200
208
  }
201
209
  update(_a) {
202
210
  var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
203
211
  return __awaiter(this, void 0, void 0, function* () {
204
- const id = update_payload_id || this.document_id;
212
+ const { id, ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, update_payload_id);
205
213
  // Trigger local update
206
214
  this.sync([{
215
+ ref,
207
216
  data: {
208
217
  changes: [{
209
218
  data: Object.assign(Object.assign({}, payload), { id, __updating: true }),
210
- ref: this.ref,
219
+ ref,
211
220
  type: 'modified'
212
221
  }]
213
222
  }
214
223
  }], true);
215
- const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
216
224
  try {
217
225
  return yield this.collection_options.transporter.update(ref, payload);
218
226
  }
219
227
  catch (e) {
220
228
  this.sync([{
229
+ ref,
221
230
  data: {
222
231
  changes: [{
223
232
  data: { id, __updating: false },
224
- ref: this.ref,
233
+ ref,
225
234
  type: 'modified'
226
235
  }]
227
236
  }
@@ -232,27 +241,28 @@ class CollectionObservable extends rxjs_1.Observable {
232
241
  }
233
242
  remove(remove_document_id) {
234
243
  return __awaiter(this, void 0, void 0, function* () {
235
- const id = remove_document_id || this.document_id;
244
+ const { id, ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, remove_document_id);
236
245
  this.sync([{
246
+ ref,
237
247
  data: {
238
248
  changes: [{
239
249
  data: { id, __removing: true },
240
- ref: this.ref,
250
+ ref,
241
251
  type: 'modified'
242
252
  }]
243
253
  }
244
254
  }], true);
245
255
  // Trigger
246
- const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
247
256
  try {
248
257
  return yield this.collection_options.transporter.remove(ref);
249
258
  }
250
259
  catch (e) {
251
260
  this.sync([{
261
+ ref,
252
262
  data: {
253
263
  changes: [{
254
264
  data: { id, __removing: false },
255
- ref: this.ref,
265
+ ref,
256
266
  type: 'modified'
257
267
  }]
258
268
  }
@@ -263,11 +273,29 @@ class CollectionObservable extends rxjs_1.Observable {
263
273
  }
264
274
  trigger(name, payload, trigger_document_id) {
265
275
  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}` : ''}`;
276
+ const { ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, trigger_document_id);
268
277
  return yield this.collection_options.transporter.trigger(ref, name, {}, payload);
269
278
  });
270
279
  }
271
280
  }
272
281
  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();
282
+ _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) {
283
+ const refs_builder = (paths) => {
284
+ const [a, b, ...c] = paths;
285
+ if (!b)
286
+ return paths;
287
+ const r = a.map(aa => b.map(bb => `${aa}/${bb}`)).flat(2);
288
+ const d = [r, ...c];
289
+ return refs_builder(d);
290
+ };
291
+ const b = path.split('/').map(l => l.split('.'));
292
+ return refs_builder(b).flat(2);
293
+ }, _CollectionObservable_find_ref_by_id = function _CollectionObservable_find_ref_by_id(id = __classPrivateFieldGet(this, _CollectionObservable_document_id, "f")) {
294
+ if (!id)
295
+ throw 'ID_NOT_FOUND';
296
+ const collection_ref = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[__classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(id)].__collection_ref;
297
+ if (!collection_ref)
298
+ throw 'COLLECTION_REF_NOT_FOUND';
299
+ const ref = `${collection_ref}${id ? `/${id}` : ''}`;
300
+ return { ref, id, collection_ref };
301
+ };
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.41",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",