@livequery/client 2.0.5 → 2.0.7

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, BehaviorSubject } from 'rxjs';
1
+ import { Subject, BehaviorSubject } from 'rxjs';
2
2
  import { LivequeryBaseEntity, QueryOption, Transporter, UpdatedData, Paging, Response } from '@livequery/types';
3
3
  export type LoadingIndicator = false | 'backward' | 'forward' | 'both';
4
4
  export type CollectionOption<T extends LivequeryBaseEntity = LivequeryBaseEntity> = {
@@ -25,12 +25,12 @@ export type CollectionStream<T extends LivequeryBaseEntity = LivequeryBaseEntity
25
25
  code?: string;
26
26
  message?: string;
27
27
  };
28
- export declare class CollectionObservable<T extends LivequeryBaseEntity = LivequeryBaseEntity> extends Observable<CollectionStream<T>> {
28
+ export declare class CollectionObservable<T extends LivequeryBaseEntity = LivequeryBaseEntity> extends BehaviorSubject<CollectionStream<T>> {
29
29
  #private;
30
30
  private ref;
31
31
  private collection_options;
32
32
  readonly $changes: Subject<UpdatedData<T>>;
33
- $: BehaviorSubject<CollectionStream<T>>;
33
+ unsubscribe(): void;
34
34
  constructor(ref: string | false | null | '' | undefined, collection_options: CollectionOption<T>);
35
35
  set_realtime(realtime: boolean): void;
36
36
  private fetch_data;
@@ -1,6 +1,6 @@
1
- import { Subject, Observable, merge, BehaviorSubject } from 'rxjs';
1
+ import { Subject, merge, BehaviorSubject } from 'rxjs';
2
2
  import { bufferTime, filter, finalize, first, map, share, skip, tap, toArray } from 'rxjs/operators';
3
- export class CollectionObservable extends Observable {
3
+ export class CollectionObservable extends BehaviorSubject {
4
4
  ref;
5
5
  collection_options;
6
6
  $changes = new Subject();
@@ -9,23 +9,22 @@ export class CollectionObservable extends Observable {
9
9
  #sorters = new Array;
10
10
  #IdMap = new Map();
11
11
  #refs = [];
12
- $ = new BehaviorSubject({
13
- items: [],
14
- loading: false,
15
- options: {},
16
- paging: {}
17
- });
12
+ // $: BehaviorSubject<CollectionStream<T>> = new BehaviorSubject<CollectionStream<T>>({
13
+ // items: [] as SmartQueryItem<T>[],
14
+ // loading: false,
15
+ // options: {},
16
+ // paging: {}
17
+ // })
18
+ unsubscribe() {
19
+ super.unsubscribe();
20
+ this.#queries.forEach(s => s.unsubscribe());
21
+ }
18
22
  constructor(ref, collection_options) {
19
- super(o => {
20
- this.$.next({
21
- ...this.$.getValue(),
22
- options: collection_options.options || {}
23
- });
24
- const linker = this.$.subscribe(o);
25
- return () => {
26
- linker.unsubscribe();
27
- this.#queries.forEach(s => s.unsubscribe());
28
- };
23
+ super({
24
+ items: [],
25
+ loading: false,
26
+ paging: {},
27
+ options: collection_options.options || {}
29
28
  });
30
29
  this.ref = ref;
31
30
  this.collection_options = collection_options;
@@ -50,7 +49,7 @@ export class CollectionObservable extends Observable {
50
49
  this.collection_options.realtime = realtime;
51
50
  }
52
51
  #sync(stream, from_local = false, direction) {
53
- const state = this.$.getValue();
52
+ const state = this.getValue();
54
53
  const realtime = this.collection_options.realtime ?? true;
55
54
  const actions = { update: false, reindex: false };
56
55
  for (const { data, error, code, message } of stream) {
@@ -207,14 +206,14 @@ export class CollectionObservable extends Observable {
207
206
  }
208
207
  };
209
208
  }
210
- actions.update && this.$.next(state);
209
+ actions.update && this.next(state);
211
210
  }
212
211
  fetch_data(options = {}, loading, flush = false) {
213
212
  if (!this.ref)
214
213
  return;
215
214
  if (this.#refs.length == 0)
216
215
  return;
217
- if (this.$.getValue().loading)
216
+ if (this.getValue().loading)
218
217
  return;
219
218
  this.collection_options.options = options;
220
219
  this.#sorters = Object.keys(options).filter(k => k.endsWith(':sort')).map(k => {
@@ -224,11 +223,11 @@ export class CollectionObservable extends Observable {
224
223
  });
225
224
  this.#sorters.every(a => a.key != 'id') && this.#sorters.push({ key: 'id', order: -1 });
226
225
  const state = {
227
- ...this.$.getValue(),
228
- items: flush ? [] : this.$.getValue().items,
226
+ ...this.getValue(),
227
+ items: flush ? [] : this.getValue().items,
229
228
  loading,
230
229
  options: {
231
- ...this.$.getValue().options || {},
230
+ ...this.getValue().options || {},
232
231
  ...options
233
232
  }
234
233
  };
@@ -244,10 +243,10 @@ export class CollectionObservable extends Observable {
244
243
  return true;
245
244
  return loading == 'forward' ? paging.has.next : paging.has.prev;
246
245
  });
247
- const no_more_data = !flush && (remain_data_refs.length == 0 || this.$.getValue().loading);
246
+ const no_more_data = !flush && (remain_data_refs.length == 0 || this.getValue().loading);
248
247
  if (no_more_data)
249
248
  return;
250
- this.$.next(state);
249
+ this.next(state);
251
250
  const queries = remain_data_refs.map((ref, index) => {
252
251
  const cursor = this.#pages.get(ref)?.cursor;
253
252
  const opts = {
@@ -271,11 +270,11 @@ export class CollectionObservable extends Observable {
271
270
  this.fetch_data({}, 'both', true);
272
271
  }
273
272
  fetch_more() {
274
- const { options } = this.$.getValue();
273
+ const { options } = this.getValue();
275
274
  this.fetch_data(options, 'forward');
276
275
  }
277
276
  fetch_prev() {
278
- const { options } = this.$.getValue();
277
+ const { options } = this.getValue();
279
278
  this.fetch_data(options, 'backward');
280
279
  }
281
280
  // public fetch_around_cursor(cursor: string) {
@@ -291,7 +290,7 @@ export class CollectionObservable extends Observable {
291
290
  const index = this.#IdMap.get(id);
292
291
  if (index == undefined)
293
292
  return {};
294
- const origin_ref = this.$.getValue().items[index].__ref;
293
+ const origin_ref = this.getValue().items[index].__ref;
295
294
  if (!origin_ref)
296
295
  throw 'COLLECTION_REF_NOT_FOUND';
297
296
  const refs = origin_ref.split('/');
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/collection.ts"],"version":"5.6.3"}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "repository": {
5
5
  "url": "https://github.com/livequery/client"
6
6
  },
7
- "version": "2.0.5",
7
+ "version": "2.0.7",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",
@@ -15,9 +15,9 @@
15
15
  "uuid": "^8.3.2"
16
16
  },
17
17
  "devDependencies": {
18
- "typescript": "^4.9.5",
19
- "rxjs": "^7.1.0",
20
- "@livequery/types": "^2.0.0"
18
+ "typescript": "^5.6.3",
19
+ "rxjs": "^7.8.1",
20
+ "@livequery/types": "^2.0.7"
21
21
  },
22
22
  "scripts": {
23
23
  "test": "echo \"Error: no test specified\" && exit 1",