@livequery/client 2.0.4 → 2.0.6
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.
- package/build/Collection.d.ts +3 -3
- package/build/Collection.js +28 -26
- package/package.json +1 -1
package/build/Collection.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Subject,
|
|
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
|
|
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
|
-
|
|
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;
|
package/build/Collection.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Subject,
|
|
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
|
|
3
|
+
export class CollectionObservable extends BehaviorSubject {
|
|
4
4
|
ref;
|
|
5
5
|
collection_options;
|
|
6
6
|
$changes = new Subject();
|
|
@@ -9,20 +9,22 @@ export class CollectionObservable extends Observable {
|
|
|
9
9
|
#sorters = new Array;
|
|
10
10
|
#IdMap = new Map();
|
|
11
11
|
#refs = [];
|
|
12
|
-
|
|
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
|
+
}
|
|
13
22
|
constructor(ref, collection_options) {
|
|
14
|
-
super(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
paging: {}
|
|
20
|
-
});
|
|
21
|
-
const linker = this.$.subscribe(o);
|
|
22
|
-
return () => {
|
|
23
|
-
linker.unsubscribe();
|
|
24
|
-
this.#queries.forEach(s => s.unsubscribe());
|
|
25
|
-
};
|
|
23
|
+
super({
|
|
24
|
+
items: [],
|
|
25
|
+
loading: false,
|
|
26
|
+
paging: {},
|
|
27
|
+
options: collection_options.options || {}
|
|
26
28
|
});
|
|
27
29
|
this.ref = ref;
|
|
28
30
|
this.collection_options = collection_options;
|
|
@@ -47,7 +49,7 @@ export class CollectionObservable extends Observable {
|
|
|
47
49
|
this.collection_options.realtime = realtime;
|
|
48
50
|
}
|
|
49
51
|
#sync(stream, from_local = false, direction) {
|
|
50
|
-
const state = this
|
|
52
|
+
const state = this.getValue();
|
|
51
53
|
const realtime = this.collection_options.realtime ?? true;
|
|
52
54
|
const actions = { update: false, reindex: false };
|
|
53
55
|
for (const { data, error, code, message } of stream) {
|
|
@@ -204,14 +206,14 @@ export class CollectionObservable extends Observable {
|
|
|
204
206
|
}
|
|
205
207
|
};
|
|
206
208
|
}
|
|
207
|
-
actions.update && this
|
|
209
|
+
actions.update && this.next(state);
|
|
208
210
|
}
|
|
209
211
|
fetch_data(options = {}, loading, flush = false) {
|
|
210
212
|
if (!this.ref)
|
|
211
213
|
return;
|
|
212
214
|
if (this.#refs.length == 0)
|
|
213
215
|
return;
|
|
214
|
-
if (this
|
|
216
|
+
if (this.getValue().loading)
|
|
215
217
|
return;
|
|
216
218
|
this.collection_options.options = options;
|
|
217
219
|
this.#sorters = Object.keys(options).filter(k => k.endsWith(':sort')).map(k => {
|
|
@@ -221,11 +223,11 @@ export class CollectionObservable extends Observable {
|
|
|
221
223
|
});
|
|
222
224
|
this.#sorters.every(a => a.key != 'id') && this.#sorters.push({ key: 'id', order: -1 });
|
|
223
225
|
const state = {
|
|
224
|
-
...this
|
|
225
|
-
items: flush ? [] : this
|
|
226
|
+
...this.getValue(),
|
|
227
|
+
items: flush ? [] : this.getValue().items,
|
|
226
228
|
loading,
|
|
227
229
|
options: {
|
|
228
|
-
...this
|
|
230
|
+
...this.getValue().options || {},
|
|
229
231
|
...options
|
|
230
232
|
}
|
|
231
233
|
};
|
|
@@ -241,10 +243,10 @@ export class CollectionObservable extends Observable {
|
|
|
241
243
|
return true;
|
|
242
244
|
return loading == 'forward' ? paging.has.next : paging.has.prev;
|
|
243
245
|
});
|
|
244
|
-
const no_more_data = !flush && (remain_data_refs.length == 0 || this
|
|
246
|
+
const no_more_data = !flush && (remain_data_refs.length == 0 || this.getValue().loading);
|
|
245
247
|
if (no_more_data)
|
|
246
248
|
return;
|
|
247
|
-
this
|
|
249
|
+
this.next(state);
|
|
248
250
|
const queries = remain_data_refs.map((ref, index) => {
|
|
249
251
|
const cursor = this.#pages.get(ref)?.cursor;
|
|
250
252
|
const opts = {
|
|
@@ -268,11 +270,11 @@ export class CollectionObservable extends Observable {
|
|
|
268
270
|
this.fetch_data({}, 'both', true);
|
|
269
271
|
}
|
|
270
272
|
fetch_more() {
|
|
271
|
-
const { options } = this
|
|
273
|
+
const { options } = this.getValue();
|
|
272
274
|
this.fetch_data(options, 'forward');
|
|
273
275
|
}
|
|
274
276
|
fetch_prev() {
|
|
275
|
-
const { options } = this
|
|
277
|
+
const { options } = this.getValue();
|
|
276
278
|
this.fetch_data(options, 'backward');
|
|
277
279
|
}
|
|
278
280
|
// public fetch_around_cursor(cursor: string) {
|
|
@@ -288,7 +290,7 @@ export class CollectionObservable extends Observable {
|
|
|
288
290
|
const index = this.#IdMap.get(id);
|
|
289
291
|
if (index == undefined)
|
|
290
292
|
return {};
|
|
291
|
-
const origin_ref = this
|
|
293
|
+
const origin_ref = this.getValue().items[index].__ref;
|
|
292
294
|
if (!origin_ref)
|
|
293
295
|
throw 'COLLECTION_REF_NOT_FOUND';
|
|
294
296
|
const refs = origin_ref.split('/');
|