@livequery/client 1.0.80 → 1.0.85

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);
@@ -49,7 +49,7 @@ export class CollectionObservable extends Observable {
49
49
  set_realtime(realtime) {
50
50
  this.collection_options.realtime = realtime;
51
51
  }
52
- sync(stream, from_local = false) {
52
+ #sync(stream, from_local = false) {
53
53
  const realtime = this.collection_options.realtime ?? true;
54
54
  const actions = { update: false, reindex: false };
55
55
  for (const { data, error, ref } of stream) {
@@ -164,6 +164,8 @@ export class CollectionObservable extends Observable {
164
164
  actions.update && this.$.next(this.value);
165
165
  }
166
166
  fetch_data(options = {}, flush = false) {
167
+ if (!this.ref)
168
+ return;
167
169
  if (this.#refs.length == 0)
168
170
  return;
169
171
  if (flush) {
@@ -173,7 +175,6 @@ export class CollectionObservable extends Observable {
173
175
  this.#IdMap.clear();
174
176
  }
175
177
  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
178
  if (!flush && (has_more_data_refs.length == 0 || this.value.loading))
178
179
  return;
179
180
  this.value = {
@@ -189,7 +190,7 @@ export class CollectionObservable extends Observable {
189
190
  .transporter
190
191
  .query(ref, { ...options, _cursor: this.#next_cursor[ref] })));
191
192
  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)));
193
+ 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
194
  const subscription = Object.assign($.subscribe(), { reload });
194
195
  this.#queries.add(subscription);
195
196
  }
@@ -227,7 +228,7 @@ export class CollectionObservable extends Observable {
227
228
  if (!ref)
228
229
  return;
229
230
  // Trigger local update
230
- this.sync([{
231
+ this.#sync([{
231
232
  ref,
232
233
  data: {
233
234
  changes: [{
@@ -241,7 +242,7 @@ export class CollectionObservable extends Observable {
241
242
  return await this.collection_options.transporter.update(ref, payload);
242
243
  }
243
244
  catch (e) {
244
- this.sync([{
245
+ this.#sync([{
245
246
  ref,
246
247
  data: {
247
248
  changes: [{
@@ -258,7 +259,7 @@ export class CollectionObservable extends Observable {
258
259
  const { id, ref } = this.#find_ref_by_id(remove_document_id);
259
260
  if (!ref)
260
261
  return;
261
- this.sync([{
262
+ this.#sync([{
262
263
  ref,
263
264
  data: {
264
265
  changes: [{
@@ -273,7 +274,7 @@ export class CollectionObservable extends Observable {
273
274
  return await this.collection_options.transporter.remove(ref);
274
275
  }
275
276
  catch (e) {
276
- this.sync([{
277
+ this.#sync([{
277
278
  ref,
278
279
  data: {
279
280
  changes: [{
@@ -286,10 +287,10 @@ export class CollectionObservable extends Observable {
286
287
  throw e;
287
288
  }
288
289
  }
289
- async trigger(name, payload, trigger_document_id) {
290
+ async trigger(name, payload, trigger_document_id, query = {}) {
290
291
  const { ref } = this.#find_ref_by_id(trigger_document_id);
291
292
  if (!ref)
292
293
  return;
293
- return await this.collection_options.transporter.trigger(ref, name, {}, payload);
294
+ return await this.collection_options.transporter.trigger(ref, name, query, payload);
294
295
  }
295
296
  }
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.85",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",