@livequery/client 1.0.23 → 1.0.26

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.
@@ -28,6 +28,8 @@ export declare class CollectionObservable<T extends {
28
28
  private ref;
29
29
  private collection_options;
30
30
  private is_collection_ref;
31
+ private collection_ref;
32
+ private document_id;
31
33
  constructor(ref: string, collection_options: CollectionOption<T>);
32
34
  private push_item;
33
35
  private sync;
@@ -36,11 +38,11 @@ export declare class CollectionObservable<T extends {
36
38
  reset(): void;
37
39
  fetch_more(): void;
38
40
  filter(filters: Partial<QueryOption<T>>): void;
39
- add(payload: T, local?: boolean): Promise<void>;
40
- remove(id: string): Promise<void>;
41
- update({ id, ...payload }: {
41
+ add(payload: T): Promise<T>;
42
+ remove(remove_document_id?: string): Promise<void>;
43
+ update({ id: update_payload_id, ...payload }: Partial<T & {
42
44
  id: string;
43
- } & Partial<T>): Promise<void>;
44
- trigger(name: string, document_id: string | null, payload?: object): Promise<unknown>;
45
+ }>): Promise<T>;
46
+ trigger<T>(name: string, payload?: object, trigger_document_id?: string): Promise<T>;
45
47
  }
46
48
  export {};
@@ -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,52 +30,54 @@ 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;
36
34
  Object.defineProperty(exports, "__esModule", { value: true });
37
35
  exports.CollectionObservable = void 0;
38
36
  const rxjs_1 = require("rxjs");
39
37
  const get_sort_function_1 = require("./helpers/get_sort_function");
40
38
  const operators_1 = require("rxjs/operators");
41
- const uuid_1 = require("uuid");
42
39
  class CollectionObservable extends rxjs_1.Observable {
43
40
  constructor(ref, collection_options) {
44
41
  super(o => {
45
- __classPrivateFieldSet(this, _state, { items: [], options: collection_options.filters, has_more: false });
46
- const subscription = __classPrivateFieldGet(this, _$state).subscribe(o);
42
+ __classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
43
+ const subscription = __classPrivateFieldGet(this, _CollectionObservable_$state, "f").subscribe(o);
47
44
  const auto_reload_interval = collection_options.reload_interval && setInterval(() => this.reload(), collection_options.reload_interval);
48
45
  return () => {
49
- __classPrivateFieldGet(this, _subscriptions).forEach(s => s.unsubscribe());
46
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
50
47
  subscription.unsubscribe();
51
48
  clearInterval(auto_reload_interval);
52
49
  };
53
50
  });
54
51
  this.ref = ref;
55
52
  this.collection_options = collection_options;
56
- _$state.set(this, new rxjs_1.Subject());
57
- _subscriptions.set(this, new Set());
58
- _state.set(this, void 0);
59
- _next_cursor.set(this, null);
53
+ _CollectionObservable_$state.set(this, new rxjs_1.Subject());
54
+ _CollectionObservable_subscriptions.set(this, new Set());
55
+ _CollectionObservable_state.set(this, void 0);
56
+ _CollectionObservable_next_cursor.set(this, null);
57
+ if (ref.startsWith('/') || ref.endsWith('/'))
58
+ throw 'INVAILD_REF_FORMAT';
60
59
  const refs = ref.split('/');
61
60
  this.is_collection_ref = refs.length % 2 == 1;
61
+ this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
62
+ this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
62
63
  }
63
64
  push_item(data) {
64
- const { id } = data;
65
- const item = Object.assign(Object.assign({ __adding: false, __updating: true, __removing: false }, data), { __remove: () => this.remove(id), __trigger: (name, payload) => this.trigger(name, id, payload), __update: (payload) => this.update(Object.assign({ id }, payload)) });
66
- __classPrivateFieldGet(this, _state).items.push(item);
65
+ const item = Object.assign(Object.assign({ __adding: false, __updating: false, __removing: false }, data), { __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({ id: data === null || data === void 0 ? void 0 : data.id }, payload)) });
66
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(item);
67
67
  }
68
68
  sync(stream) {
69
69
  var _a, _b, _c, _d;
70
70
  for (const { data, error } of stream) {
71
71
  // Error & paging
72
- error && (__classPrivateFieldGet(this, _state).error = error);
72
+ error && (__classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error);
73
73
  if (((_a = data === null || data === void 0 ? void 0 : data.paging) === null || _a === void 0 ? void 0 : _a.n) == 0) {
74
- __classPrivateFieldGet(this, _state).has_more = (_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.has_more;
75
- __classPrivateFieldSet(this, _next_cursor, (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor);
76
- __classPrivateFieldGet(this, _state).loading = false;
74
+ __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;
75
+ __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");
76
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
77
77
  }
78
78
  // Sync
79
79
  for (const { data: payload, type } of (data === null || data === void 0 ? void 0 : data.changes) || []) {
80
- const index = __classPrivateFieldGet(this, _state).items.findIndex(item => item.id == payload.id);
80
+ const index = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.findIndex(item => item.id == payload.id);
81
81
  if (index == -1 && type == 'added') {
82
82
  if (
83
83
  // Is first value from HTTP query
@@ -85,14 +85,14 @@ class CollectionObservable extends rxjs_1.Observable {
85
85
  || (
86
86
  // Is realtime update that match filters
87
87
  Object
88
- .keys(__classPrivateFieldGet(this, _state).options || {})
88
+ .keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
89
89
  .filter(key => !key.includes('_'))
90
90
  .every(key => {
91
91
  var _a;
92
92
  try {
93
93
  const [field, expression] = key.split(':');
94
94
  const a = payload[field];
95
- const b = (_a = __classPrivateFieldGet(this, _state).options) === null || _a === void 0 ? void 0 : _a[field];
95
+ const b = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f").options) === null || _a === void 0 ? void 0 : _a[field];
96
96
  if (!expression)
97
97
  return a == b;
98
98
  if (expression == 'ne')
@@ -124,69 +124,56 @@ class CollectionObservable extends rxjs_1.Observable {
124
124
  }
125
125
  if (index >= 0) {
126
126
  if (type == 'added' || type == 'modified') {
127
- __classPrivateFieldGet(this, _state).items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _state).items[index]), { __adding: false, __updating: false, __removing: false }), payload);
127
+ __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);
128
128
  }
129
129
  if (type == 'removed') {
130
- __classPrivateFieldGet(this, _state).items.splice(index, 1);
130
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
131
131
  }
132
132
  }
133
133
  }
134
134
  }
135
- 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');
136
- __classPrivateFieldGet(this, _state).items = __classPrivateFieldGet(this, _state).items.sort(sort_function);
137
- __classPrivateFieldGet(this, _$state).next(__classPrivateFieldGet(this, _state));
135
+ const sort_function = (0, get_sort_function_1.get_sort_function)(__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[0], __classPrivateFieldGet(this, _CollectionObservable_state, "f").options._order_by || 'created_at', __classPrivateFieldGet(this, _CollectionObservable_state, "f").options._sort || 'desc');
136
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.sort(sort_function);
137
+ __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
138
138
  }
139
139
  fetch_data(options = {}, flush = false) {
140
- console.log({ ref: this.ref, options });
141
140
  if (!this.ref)
142
141
  return;
143
142
  if (flush) {
144
- __classPrivateFieldGet(this, _subscriptions).forEach(s => s.unsubscribe());
145
- __classPrivateFieldGet(this, _subscriptions).clear();
143
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
144
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").clear();
146
145
  }
147
- __classPrivateFieldSet(this, _state, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _state)), { items: flush ? [] : __classPrivateFieldGet(this, _state).items, error: null, loading: true, options }));
148
- __classPrivateFieldGet(this, _$state).next(__classPrivateFieldGet(this, _state));
146
+ __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");
147
+ __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
149
148
  const query = this.collection_options.transporter.query(this.ref, options);
150
149
  const sub = Object.assign(query
151
- .pipe(operators_1.bufferTime(this.collection_options.sync_delay || 500), operators_1.filter(stream => stream.length > 0))
150
+ .pipe((0, operators_1.bufferTime)(this.collection_options.sync_delay || 500), (0, operators_1.filter)(stream => stream.length > 0))
152
151
  .subscribe(data => this.sync(data)), { reload: query.reload });
153
- __classPrivateFieldGet(this, _subscriptions).add(sub);
152
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(sub);
154
153
  }
155
154
  reload() {
156
- __classPrivateFieldGet(this, _subscriptions).forEach(s => s.reload());
155
+ __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.reload());
157
156
  }
158
157
  reset() {
159
158
  this.fetch_data({}, true);
160
159
  }
161
160
  fetch_more() {
162
161
  var _a;
163
- const options = (_a = __classPrivateFieldGet(this, _state)) === null || _a === void 0 ? void 0 : _a.options;
164
- __classPrivateFieldGet(this, _next_cursor) && (options._cursor = __classPrivateFieldGet(this, _next_cursor));
162
+ const options = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f")) === null || _a === void 0 ? void 0 : _a.options;
163
+ __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f") && (options._cursor = __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f"));
165
164
  this.fetch_data(options);
166
165
  }
167
166
  filter(filters) {
168
167
  this.fetch_data(filters, true);
169
168
  }
170
- add(payload, local = false) {
169
+ add(payload) {
171
170
  return __awaiter(this, void 0, void 0, function* () {
172
- if (local) {
173
- const data = Object.assign(Object.assign({ id: uuid_1.v4() }, payload), { __adding: true });
174
- this.sync([{
175
- data: {
176
- changes: [{
177
- data: Object.assign(Object.assign({}, payload), { __adding: true }),
178
- ref: this.ref,
179
- type: 'added'
180
- }]
181
- }
182
- }]);
183
- return yield this.collection_options.transporter.add(`${this.ref}`, data);
184
- }
185
- return yield this.collection_options.transporter.add(`${this.ref}`, payload);
171
+ return yield this.collection_options.transporter.add(`${this.collection_ref}`, payload);
186
172
  });
187
173
  }
188
- remove(id) {
174
+ remove(remove_document_id) {
189
175
  return __awaiter(this, void 0, void 0, function* () {
176
+ const id = remove_document_id || this.document_id;
190
177
  this.sync([{
191
178
  data: {
192
179
  changes: [{
@@ -197,13 +184,14 @@ class CollectionObservable extends rxjs_1.Observable {
197
184
  }
198
185
  }]);
199
186
  // Trigger
200
- const ref = `${this.ref}${this.is_collection_ref ? `/${id}` : ''}`;
201
- return yield this.collection_options.transporter.remove(ref);
187
+ const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
188
+ yield this.collection_options.transporter.remove(ref);
202
189
  });
203
190
  }
204
191
  update(_a) {
205
- var { id } = _a, payload = __rest(_a, ["id"]);
192
+ var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
206
193
  return __awaiter(this, void 0, void 0, function* () {
194
+ const id = update_payload_id || this.document_id;
207
195
  // Trigger local update
208
196
  this.sync([{
209
197
  data: {
@@ -214,16 +202,17 @@ class CollectionObservable extends rxjs_1.Observable {
214
202
  }]
215
203
  }
216
204
  }]);
217
- const ref = `${this.ref}${this.is_collection_ref ? `/${id}` : ''}`;
205
+ const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
218
206
  return yield this.collection_options.transporter.update(ref, payload);
219
207
  });
220
208
  }
221
- trigger(name, document_id, payload) {
209
+ trigger(name, payload, trigger_document_id) {
222
210
  return __awaiter(this, void 0, void 0, function* () {
223
- const ref = `${this.ref}${this.is_collection_ref ? `/${document_id}` : ''}`;
211
+ const id = trigger_document_id || this.document_id;
212
+ const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
224
213
  return yield this.collection_options.transporter.trigger(ref, name, {}, payload);
225
214
  });
226
215
  }
227
216
  }
228
217
  exports.CollectionObservable = CollectionObservable;
229
- _$state = new WeakMap(), _subscriptions = new WeakMap(), _state = new WeakMap(), _next_cursor = new WeakMap();
218
+ _CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap();
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.23",
6
+ "version": "1.0.26",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  "uuid": "^8.3.2"
16
16
  },
17
17
  "devDependencies": {
18
- "@livequery/types": "^1.0.29"
18
+ "@livequery/types": "^1.0.31"
19
19
  },
20
20
  "scripts": {
21
21
  "test": "echo \"Error: no test specified\" && exit 1",