1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var apollo_utilities_1 = require("apollo-utilities");
|
4 | var ts_invariant_1 = require("ts-invariant");
|
5 | var networkStatus_1 = require("../core/networkStatus");
|
6 | var arrays_1 = require("../util/arrays");
|
7 | var QueryStore = (function () {
|
8 | function QueryStore() {
|
9 | this.store = {};
|
10 | }
|
11 | QueryStore.prototype.getStore = function () {
|
12 | return this.store;
|
13 | };
|
14 | QueryStore.prototype.get = function (queryId) {
|
15 | return this.store[queryId];
|
16 | };
|
17 | QueryStore.prototype.initQuery = function (query) {
|
18 | var previousQuery = this.store[query.queryId];
|
19 | ts_invariant_1.invariant(!previousQuery ||
|
20 | previousQuery.document === query.document ||
|
21 | apollo_utilities_1.isEqual(previousQuery.document, query.document), 'Internal Error: may not update existing query string in store');
|
22 | var isSetVariables = false;
|
23 | var previousVariables = null;
|
24 | if (query.storePreviousVariables &&
|
25 | previousQuery &&
|
26 | previousQuery.networkStatus !== networkStatus_1.NetworkStatus.loading) {
|
27 | if (!apollo_utilities_1.isEqual(previousQuery.variables, query.variables)) {
|
28 | isSetVariables = true;
|
29 | previousVariables = previousQuery.variables;
|
30 | }
|
31 | }
|
32 | var networkStatus;
|
33 | if (isSetVariables) {
|
34 | networkStatus = networkStatus_1.NetworkStatus.setVariables;
|
35 | }
|
36 | else if (query.isPoll) {
|
37 | networkStatus = networkStatus_1.NetworkStatus.poll;
|
38 | }
|
39 | else if (query.isRefetch) {
|
40 | networkStatus = networkStatus_1.NetworkStatus.refetch;
|
41 | }
|
42 | else {
|
43 | networkStatus = networkStatus_1.NetworkStatus.loading;
|
44 | }
|
45 | var graphQLErrors = [];
|
46 | if (previousQuery && previousQuery.graphQLErrors) {
|
47 | graphQLErrors = previousQuery.graphQLErrors;
|
48 | }
|
49 | this.store[query.queryId] = {
|
50 | document: query.document,
|
51 | variables: query.variables,
|
52 | previousVariables: previousVariables,
|
53 | networkError: null,
|
54 | graphQLErrors: graphQLErrors,
|
55 | networkStatus: networkStatus,
|
56 | metadata: query.metadata,
|
57 | };
|
58 | if (typeof query.fetchMoreForQueryId === 'string' &&
|
59 | this.store[query.fetchMoreForQueryId]) {
|
60 | this.store[query.fetchMoreForQueryId].networkStatus =
|
61 | networkStatus_1.NetworkStatus.fetchMore;
|
62 | }
|
63 | };
|
64 | QueryStore.prototype.markQueryResult = function (queryId, result, fetchMoreForQueryId) {
|
65 | if (!this.store || !this.store[queryId])
|
66 | return;
|
67 | this.store[queryId].networkError = null;
|
68 | this.store[queryId].graphQLErrors = arrays_1.isNonEmptyArray(result.errors) ? result.errors : [];
|
69 | this.store[queryId].previousVariables = null;
|
70 | this.store[queryId].networkStatus = networkStatus_1.NetworkStatus.ready;
|
71 | if (typeof fetchMoreForQueryId === 'string' &&
|
72 | this.store[fetchMoreForQueryId]) {
|
73 | this.store[fetchMoreForQueryId].networkStatus = networkStatus_1.NetworkStatus.ready;
|
74 | }
|
75 | };
|
76 | QueryStore.prototype.markQueryError = function (queryId, error, fetchMoreForQueryId) {
|
77 | if (!this.store || !this.store[queryId])
|
78 | return;
|
79 | this.store[queryId].networkError = error;
|
80 | this.store[queryId].networkStatus = networkStatus_1.NetworkStatus.error;
|
81 | if (typeof fetchMoreForQueryId === 'string') {
|
82 | this.markQueryResultClient(fetchMoreForQueryId, true);
|
83 | }
|
84 | };
|
85 | QueryStore.prototype.markQueryResultClient = function (queryId, complete) {
|
86 | var storeValue = this.store && this.store[queryId];
|
87 | if (storeValue) {
|
88 | storeValue.networkError = null;
|
89 | storeValue.previousVariables = null;
|
90 | if (complete) {
|
91 | storeValue.networkStatus = networkStatus_1.NetworkStatus.ready;
|
92 | }
|
93 | }
|
94 | };
|
95 | QueryStore.prototype.stopQuery = function (queryId) {
|
96 | delete this.store[queryId];
|
97 | };
|
98 | QueryStore.prototype.reset = function (observableQueryIds) {
|
99 | var _this = this;
|
100 | Object.keys(this.store).forEach(function (queryId) {
|
101 | if (observableQueryIds.indexOf(queryId) < 0) {
|
102 | _this.stopQuery(queryId);
|
103 | }
|
104 | else {
|
105 | _this.store[queryId].networkStatus = networkStatus_1.NetworkStatus.loading;
|
106 | }
|
107 | });
|
108 | };
|
109 | return QueryStore;
|
110 | }());
|
111 | exports.QueryStore = QueryStore;
|
112 |
|
\ | No newline at end of file |