@livequery/client 1.0.37 → 1.0.39

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.
@@ -40,11 +40,15 @@ export declare class CollectionObservable<T extends {
40
40
  reset(): void;
41
41
  fetch_more(): void;
42
42
  filter(filters: Partial<QueryOption<T>>): void;
43
- add(payload: T): Promise<T>;
44
- remove(remove_document_id?: string): Promise<void>;
43
+ add(payload: T): Promise<{
44
+ data: {
45
+ item: T;
46
+ };
47
+ }>;
45
48
  update({ id: update_payload_id, ...payload }: Partial<T & {
46
49
  id: string;
47
- }>): Promise<T>;
50
+ }>): Promise<any>;
51
+ remove(remove_document_id?: string): Promise<void>;
48
52
  trigger<T>(name: string, payload?: object, trigger_document_id?: string): Promise<T>;
49
53
  }
50
54
  export {};
@@ -66,13 +66,16 @@ class CollectionObservable extends rxjs_1.Observable {
66
66
  this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
67
67
  this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
68
68
  }
69
- sync(stream) {
69
+ sync(stream, from_local = false) {
70
70
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
71
71
  const realtime = (_a = this.collection_options.realtime) !== null && _a !== void 0 ? _a : true;
72
72
  const actions = { update: false, reindex: false };
73
73
  for (const { data, error } of stream) {
74
74
  // Error & paging
75
- error && (__classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error);
75
+ if (error) {
76
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error;
77
+ actions.update = true;
78
+ }
76
79
  if (((_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.n) == 0) {
77
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;
78
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");
@@ -85,14 +88,14 @@ class CollectionObservable extends rxjs_1.Observable {
85
88
  continue;
86
89
  const { data: payload, type } = change;
87
90
  this.$changes.next(change);
88
- const index = (_f = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _f !== void 0 ? _f : -1;
91
+ const index = (_f = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.__local_id || payload.id)) !== null && _f !== void 0 ? _f : -1;
89
92
  if (index == -1 && type == 'added') {
90
93
  if (
91
94
  // Is first value from HTTP query
92
95
  ((_g = data === null || data === void 0 ? void 0 : data.paging) === null || _g === void 0 ? void 0 : _g.n) == 0
93
96
  || (
94
97
  // Is realtime update that match filters
95
- realtime && Object
98
+ (realtime || from_local) && Object
96
99
  .keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
97
100
  .filter(key => !key.includes('_'))
98
101
  .every(key => {
@@ -132,13 +135,13 @@ class CollectionObservable extends rxjs_1.Observable {
132
135
  actions.update = true;
133
136
  }
134
137
  }
135
- if (index >= 0 && realtime) {
138
+ if (index >= 0 && (realtime || from_local)) {
136
139
  if (type == 'added' || type == 'modified') {
137
140
  actions.update = true;
138
141
  if (Object.keys(payload).some(key => { var _a, _b; return ['created_at', (_b = (_a = this.collection_options) === null || _a === void 0 ? void 0 : _a.filters) === null || _b === void 0 ? void 0 : _b._order_by].includes(key); })) {
139
142
  actions.reindex = true;
140
143
  }
141
- __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 });
144
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index]), { __adding: false, __updating: false, __removing: false }), payload);
142
145
  }
143
146
  if (type == 'removed') {
144
147
  actions.reindex = true;
@@ -195,39 +198,67 @@ class CollectionObservable extends rxjs_1.Observable {
195
198
  return yield this.collection_options.transporter.add(`${this.collection_ref}`, payload);
196
199
  });
197
200
  }
198
- remove(remove_document_id) {
201
+ update(_a) {
202
+ var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
199
203
  return __awaiter(this, void 0, void 0, function* () {
200
- const id = remove_document_id || this.document_id;
204
+ const id = update_payload_id || this.document_id;
205
+ // Trigger local update
201
206
  this.sync([{
202
207
  data: {
203
208
  changes: [{
204
- data: { id, __removing: true },
209
+ data: Object.assign(Object.assign({}, payload), { id, __updating: true }),
205
210
  ref: this.ref,
206
211
  type: 'modified'
207
212
  }]
208
213
  }
209
- }]);
210
- // Trigger
214
+ }], true);
211
215
  const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
212
- yield this.collection_options.transporter.remove(ref);
216
+ try {
217
+ return yield this.collection_options.transporter.update(ref, payload);
218
+ }
219
+ catch (e) {
220
+ this.sync([{
221
+ data: {
222
+ changes: [{
223
+ data: { id, __updating: false },
224
+ ref: this.ref,
225
+ type: 'modified'
226
+ }]
227
+ }
228
+ }], true);
229
+ throw e;
230
+ }
213
231
  });
214
232
  }
215
- update(_a) {
216
- var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
233
+ remove(remove_document_id) {
217
234
  return __awaiter(this, void 0, void 0, function* () {
218
- const id = update_payload_id || this.document_id;
219
- // Trigger local update
235
+ const id = remove_document_id || this.document_id;
220
236
  this.sync([{
221
237
  data: {
222
238
  changes: [{
223
- data: Object.assign(Object.assign({}, payload), { id, __updating: true }),
239
+ data: { id, __removing: true },
224
240
  ref: this.ref,
225
241
  type: 'modified'
226
242
  }]
227
243
  }
228
- }]);
244
+ }], true);
245
+ // Trigger
229
246
  const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
230
- return yield this.collection_options.transporter.update(ref, payload);
247
+ try {
248
+ return yield this.collection_options.transporter.remove(ref);
249
+ }
250
+ catch (e) {
251
+ this.sync([{
252
+ data: {
253
+ changes: [{
254
+ data: { id, __removing: false },
255
+ ref: this.ref,
256
+ type: 'modified'
257
+ }]
258
+ }
259
+ }], true);
260
+ throw e;
261
+ }
231
262
  });
232
263
  }
233
264
  trigger(name, payload, trigger_document_id) {
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.37",
6
+ "version": "1.0.39",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",