@livequery/client 1.0.80 → 1.0.92

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.
@@ -1,4 +1,4 @@
1
- import { Subject, Observable } from 'rxjs';
1
+ import { Subject, Observable, ReplaySubject } from 'rxjs';
2
2
  import { ErrorInfo, LivequeryBaseEntity, QueryOption, Transporter, UpdatedData } from '@livequery/types';
3
3
  export type CollectionOption<T extends LivequeryBaseEntity = LivequeryBaseEntity> = {
4
4
  transporter: Transporter;
@@ -29,22 +29,31 @@ export declare class CollectionObservable<T extends LivequeryBaseEntity = Livequ
29
29
  private collection_options;
30
30
  readonly $changes: Subject<UpdatedData<T>>;
31
31
  value: CollectionStream<T>;
32
- $: Subject<CollectionStream<T>>;
32
+ $: ReplaySubject<CollectionStream<T>>;
33
33
  constructor(ref: string | false | null | '' | undefined, collection_options: CollectionOption<T>);
34
34
  set_realtime(realtime: boolean): void;
35
- private sync;
36
35
  private fetch_data;
37
36
  reload(): void;
38
37
  reset(): void;
39
38
  fetch_more(): void;
40
39
  filter(filters: Partial<QueryOption<T>>): void;
41
- add(payload: Partial<T>): Promise<{
40
+ add<R = {
42
41
  data: {
43
42
  item: T;
44
43
  };
45
- }>;
46
- update({ id: update_payload_id, ...payload }: Partial<T>): Promise<any>;
47
- remove(remove_document_id?: string): Promise<void>;
48
- trigger<T>(name: string, payload?: object, trigger_document_id?: string): Promise<T | undefined>;
44
+ }>(payload: Partial<T>): Promise<R>;
45
+ update<R = {
46
+ data: {
47
+ item: T;
48
+ };
49
+ }>({ id: update_payload_id, ...payload }: Partial<T>): Promise<R | undefined>;
50
+ remove<R = {
51
+ data: {
52
+ item: T;
53
+ };
54
+ }>(remove_document_id?: string): Promise<void>;
55
+ trigger<R>(name: string, payload?: object, trigger_document_id?: string, query?: {
56
+ [key: string]: string | number | boolean;
57
+ }): Promise<R | undefined>;
49
58
  }
50
59
  export {};
@@ -1,4 +1,4 @@
1
- import { Subject, Observable, merge } from 'rxjs';
1
+ import { Subject, Observable, merge, ReplaySubject } from 'rxjs';
2
2
  import { bufferTime, filter, map } from 'rxjs/operators';
3
3
  export class CollectionObservable extends Observable {
4
4
  ref;
@@ -14,7 +14,7 @@ export class CollectionObservable extends Observable {
14
14
  options: {},
15
15
  loading: false
16
16
  };
17
- $ = new Subject();
17
+ $ = new ReplaySubject(1);
18
18
  constructor(ref, collection_options) {
19
19
  super(o => {
20
20
  const subscription = this.$.subscribe(o);
@@ -27,6 +27,7 @@ export class CollectionObservable extends Observable {
27
27
  });
28
28
  this.ref = ref;
29
29
  this.collection_options = collection_options;
30
+ this.collection_options.filters = this.collection_options.filters || {};
30
31
  if (collection_options.filters)
31
32
  this.value.options = collection_options.filters;
32
33
  if (ref && (ref.startsWith('/') || ref.endsWith('/')))
@@ -49,7 +50,7 @@ export class CollectionObservable extends Observable {
49
50
  set_realtime(realtime) {
50
51
  this.collection_options.realtime = realtime;
51
52
  }
52
- sync(stream, from_local = false) {
53
+ #sync(stream, from_local = false) {
53
54
  const realtime = this.collection_options.realtime ?? true;
54
55
  const actions = { update: false, reindex: false };
55
56
  for (const { data, error, ref } of stream) {
@@ -164,6 +165,8 @@ export class CollectionObservable extends Observable {
164
165
  actions.update && this.$.next(this.value);
165
166
  }
166
167
  fetch_data(options = {}, flush = false) {
168
+ if (!this.ref)
169
+ return;
167
170
  if (this.#refs.length == 0)
168
171
  return;
169
172
  if (flush) {
@@ -173,7 +176,6 @@ export class CollectionObservable extends Observable {
173
176
  this.#IdMap.clear();
174
177
  }
175
178
  const has_more_data_refs = this.#refs.filter(ref => this.#next_cursor[ref] === undefined || (this.#next_cursor[ref] && this.#next_cursor[ref] != '#'));
176
- // Load more but no more data || loading
177
179
  if (!flush && (has_more_data_refs.length == 0 || this.value.loading))
178
180
  return;
179
181
  this.value = {
@@ -189,7 +191,7 @@ export class CollectionObservable extends Observable {
189
191
  .transporter
190
192
  .query(ref, { ...options, _cursor: this.#next_cursor[ref] })));
191
193
  const reload = () => queries.map(q => q.reload());
192
- const $ = merge(...queries.map((q, index) => q.pipe(map(data => ({ ...data, ref: has_more_data_refs[index] }))))).pipe(bufferTime(500), filter(list => list.length > 0), map(data => this.sync(data)));
194
+ const $ = merge(...queries.map((q, index) => q.pipe(map(data => ({ ...data, ref: has_more_data_refs[index] }))))).pipe(bufferTime(500), filter(list => list.length > 0), map(data => this.#sync(data)));
193
195
  const subscription = Object.assign($.subscribe(), { reload });
194
196
  this.#queries.add(subscription);
195
197
  }
@@ -227,7 +229,7 @@ export class CollectionObservable extends Observable {
227
229
  if (!ref)
228
230
  return;
229
231
  // Trigger local update
230
- this.sync([{
232
+ this.#sync([{
231
233
  ref,
232
234
  data: {
233
235
  changes: [{
@@ -241,7 +243,7 @@ export class CollectionObservable extends Observable {
241
243
  return await this.collection_options.transporter.update(ref, payload);
242
244
  }
243
245
  catch (e) {
244
- this.sync([{
246
+ this.#sync([{
245
247
  ref,
246
248
  data: {
247
249
  changes: [{
@@ -258,7 +260,7 @@ export class CollectionObservable extends Observable {
258
260
  const { id, ref } = this.#find_ref_by_id(remove_document_id);
259
261
  if (!ref)
260
262
  return;
261
- this.sync([{
263
+ this.#sync([{
262
264
  ref,
263
265
  data: {
264
266
  changes: [{
@@ -273,7 +275,7 @@ export class CollectionObservable extends Observable {
273
275
  return await this.collection_options.transporter.remove(ref);
274
276
  }
275
277
  catch (e) {
276
- this.sync([{
278
+ this.#sync([{
277
279
  ref,
278
280
  data: {
279
281
  changes: [{
@@ -286,10 +288,10 @@ export class CollectionObservable extends Observable {
286
288
  throw e;
287
289
  }
288
290
  }
289
- async trigger(name, payload, trigger_document_id) {
291
+ async trigger(name, payload, trigger_document_id, query = {}) {
290
292
  const { ref } = this.#find_ref_by_id(trigger_document_id);
291
293
  if (!ref)
292
294
  return;
293
- return await this.collection_options.transporter.trigger(ref, name, {}, payload);
295
+ return await this.collection_options.transporter.trigger(ref, name, query, payload);
294
296
  }
295
297
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "repository": {
5
5
  "url": "https://github.com/livequery/client"
6
6
  },
7
- "version": "1.0.80",
7
+ "version": "1.0.92",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",