@livequery/client 1.0.30 → 1.0.31

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.
@@ -5,6 +5,7 @@ export type CollectionOption<T = any> = {
5
5
  sync_delay?: number;
6
6
  filters?: Partial<QueryOption<T>>;
7
7
  reload_interval?: number;
8
+ realtime?: boolean;
8
9
  };
9
10
  type CollectionStream<T> = {
10
11
  items: SmartQueryItem<T>[];
@@ -31,8 +32,8 @@ export declare class CollectionObservable<T extends {
31
32
  private collection_ref;
32
33
  private document_id;
33
34
  readonly $changes: Subject<UpdatedData<T>>;
35
+ set_realtime(realtime: boolean): void;
34
36
  constructor(ref: string, collection_options: CollectionOption<T>);
35
- private push_item;
36
37
  private sync;
37
38
  private fetch_data;
38
39
  reload(): void;
@@ -36,6 +36,9 @@ exports.CollectionObservable = void 0;
36
36
  const rxjs_1 = require("rxjs");
37
37
  const operators_1 = require("rxjs/operators");
38
38
  class CollectionObservable extends rxjs_1.Observable {
39
+ set_realtime(realtime) {
40
+ this.collection_options.realtime = realtime;
41
+ }
39
42
  constructor(ref, collection_options) {
40
43
  super(o => {
41
44
  __classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
@@ -62,33 +65,30 @@ class CollectionObservable extends rxjs_1.Observable {
62
65
  this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
63
66
  this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
64
67
  }
65
- push_item(data) {
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 })) });
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
- }
70
68
  sync(stream) {
71
- var _a, _b, _c, _d, _e;
69
+ var _a, _b, _c, _d, _e, _f;
70
+ const realtime = (_a = this.collection_options.realtime) !== null && _a !== void 0 ? _a : true;
71
+ const actions = { update: false, reindex: false };
72
72
  for (const { data, error } of stream) {
73
73
  // Error & paging
74
74
  error && (__classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error);
75
- if (((_a = data === null || data === void 0 ? void 0 : data.paging) === null || _a === void 0 ? void 0 : _a.n) == 0) {
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");
75
+ if (((_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.n) == 0) {
76
+ __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;
77
+ __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");
78
78
  __classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
79
79
  }
80
80
  // Sync
81
81
  for (const change of (data === null || data === void 0 ? void 0 : data.changes) || []) {
82
82
  const { data: payload, type } = change;
83
83
  this.$changes.next(change);
84
- const index = (_d = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _d !== void 0 ? _d : -1;
84
+ const index = (_e = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _e !== void 0 ? _e : -1;
85
85
  if (index == -1 && type == 'added') {
86
86
  if (
87
87
  // Is first value from HTTP query
88
- ((_e = data === null || data === void 0 ? void 0 : data.paging) === null || _e === void 0 ? void 0 : _e.n) == 0
88
+ ((_f = data === null || data === void 0 ? void 0 : data.paging) === null || _f === void 0 ? void 0 : _f.n) == 0
89
89
  || (
90
90
  // Is realtime update that match filters
91
- Object
91
+ realtime && Object
92
92
  .keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
93
93
  .filter(key => !key.includes('_'))
94
94
  .every(key => {
@@ -123,14 +123,22 @@ class CollectionObservable extends rxjs_1.Observable {
123
123
  catch (e) { }
124
124
  return false;
125
125
  }))) {
126
- this.push_item(payload);
126
+ __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, payload) => this.trigger(name, payload, payload === null || payload === void 0 ? void 0 : payload.id), __update: (payload) => this.update(Object.assign(Object.assign({}, payload), { id: payload === null || payload === void 0 ? void 0 : payload.id })) }));
127
+ actions.reindex = true;
128
+ actions.update = true;
127
129
  }
128
130
  }
129
- if (index >= 0) {
131
+ if (index >= 0 && realtime) {
130
132
  if (type == 'added' || type == 'modified') {
133
+ actions.update = true;
134
+ if (Object.keys(payload).some(key => ['created_at', this.collection_options.filters._order_by].includes(key))) {
135
+ actions.reindex = true;
136
+ }
131
137
  __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 });
132
138
  }
133
139
  if (type == 'removed') {
140
+ actions.reindex = true;
141
+ actions.update = true;
134
142
  __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
135
143
  for (const [document_id, i] of __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f")) {
136
144
  i == index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").delete(document_id);
@@ -140,7 +148,8 @@ class CollectionObservable extends rxjs_1.Observable {
140
148
  }
141
149
  }
142
150
  }
143
- __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
151
+ actions.reindex && (__classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear(), __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.map((item, index) => __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, index)));
152
+ actions.update && __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
144
153
  }
145
154
  fetch_data(options = {}, flush = false) {
146
155
  if (!this.ref)
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.30",
6
+ "version": "1.0.31",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",