UNPKG

11.4 kBJavaScriptView Raw
1import { NetworkStatus } from '@apollo/client/core';
2import { Observable, from } from 'rxjs';
3import { wrapWithZone, fixObservable } from './utils';
4function useInitialLoading(obsQuery) {
5 return function useInitialLoadingOperator(source) {
6 return new Observable(function useInitialLoadingSubscription(subscriber) {
7 const currentResult = obsQuery.getCurrentResult();
8 const { loading, errors, error, partial, data } = currentResult;
9 const { partialRefetch, fetchPolicy } = obsQuery.options;
10 const hasError = errors || error;
11 if (partialRefetch &&
12 partial &&
13 (!data || Object.keys(data).length === 0) &&
14 fetchPolicy !== 'cache-only' &&
15 !loading &&
16 !hasError) {
17 subscriber.next({
18 ...currentResult,
19 loading: true,
20 networkStatus: NetworkStatus.loading,
21 });
22 }
23 return source.subscribe(subscriber);
24 });
25 };
26}
27export class QueryRef {
28 constructor(obsQuery, ngZone, options) {
29 this.obsQuery = obsQuery;
30 const wrapped = wrapWithZone(from(fixObservable(this.obsQuery)), ngZone);
31 this.valueChanges = options.useInitialLoading
32 ? wrapped.pipe(useInitialLoading(this.obsQuery))
33 : wrapped;
34 this.queryId = this.obsQuery.queryId;
35 }
36 // ObservableQuery's methods
37 get options() {
38 return this.obsQuery.options;
39 }
40 get variables() {
41 return this.obsQuery.variables;
42 }
43 result() {
44 return this.obsQuery.result();
45 }
46 getCurrentResult() {
47 return this.obsQuery.getCurrentResult();
48 }
49 getLastResult() {
50 return this.obsQuery.getLastResult();
51 }
52 getLastError() {
53 return this.obsQuery.getLastError();
54 }
55 resetLastResults() {
56 return this.obsQuery.resetLastResults();
57 }
58 refetch(variables) {
59 return this.obsQuery.refetch(variables);
60 }
61 fetchMore(fetchMoreOptions) {
62 return this.obsQuery.fetchMore(fetchMoreOptions);
63 }
64 subscribeToMore(options) {
65 // XXX: there's a bug in apollo-client typings
66 // it should not inherit types from ObservableQuery
67 return this.obsQuery.subscribeToMore(options);
68 }
69 updateQuery(mapFn) {
70 return this.obsQuery.updateQuery(mapFn);
71 }
72 stopPolling() {
73 return this.obsQuery.stopPolling();
74 }
75 startPolling(pollInterval) {
76 return this.obsQuery.startPolling(pollInterval);
77 }
78 setOptions(opts) {
79 return this.obsQuery.setOptions(opts);
80 }
81 setVariables(variables) {
82 return this.obsQuery.setVariables(variables);
83 }
84}
85//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicXVlcnktcmVmLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3F1ZXJ5LXJlZi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFVQSxPQUFPLEVBQUMsYUFBYSxFQUFDLE1BQU0scUJBQXFCLENBQUM7QUFDbEQsT0FBTyxFQUFDLFVBQVUsRUFBRSxJQUFJLEVBQUMsTUFBTSxNQUFNLENBQUM7QUFFdEMsT0FBTyxFQUFDLFlBQVksRUFBRSxhQUFhLEVBQUMsTUFBTSxTQUFTLENBQUM7QUFHcEQsU0FBUyxpQkFBaUIsQ0FBTyxRQUErQjtJQUM5RCxPQUFPLFNBQVMseUJBQXlCLENBQ3ZDLE1BQXFCO1FBRXJCLE9BQU8sSUFBSSxVQUFVLENBQUMsU0FBUyw2QkFBNkIsQ0FBQyxVQUFVO1lBQ3JFLE1BQU0sYUFBYSxHQUFHLFFBQVEsQ0FBQyxnQkFBZ0IsRUFBRSxDQUFDO1lBQ2xELE1BQU0sRUFBQyxPQUFPLEVBQUUsTUFBTSxFQUFFLEtBQUssRUFBRSxPQUFPLEVBQUUsSUFBSSxFQUFDLEdBQUcsYUFBYSxDQUFDO1lBQzlELE1BQU0sRUFBQyxjQUFjLEVBQUUsV0FBVyxFQUFDLEdBQUcsUUFBUSxDQUFDLE9BQU8sQ0FBQztZQUV2RCxNQUFNLFFBQVEsR0FBRyxNQUFNLElBQUksS0FBSyxDQUFDO1lBRWpDLElBQ0UsY0FBYztnQkFDZCxPQUFPO2dCQUNQLENBQUMsQ0FBQyxJQUFJLElBQUksTUFBTSxDQUFDLElBQU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