1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = createRequestHandler;
|
7 |
|
8 | var _relayRuntime = require("relay-runtime");
|
9 |
|
10 | var _helpers = require("./helpers");
|
11 |
|
12 | function createRequestHandler(customFetcher) {
|
13 | var burstCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new _relayRuntime.QueryResponseCache({
|
14 | size: 250,
|
15 | ttl: 2 * 1000
|
16 |
|
17 | });
|
18 |
|
19 | function cleanup() {
|
20 | }
|
21 |
|
22 | return function handleRequest(requestNode, variables, cacheConfig, uploadables) {
|
23 | return _relayRuntime.Observable.create(function (sink) {
|
24 | var queryID = requestNode.text;
|
25 |
|
26 | if ((0, _helpers.isMutation)(requestNode)) {
|
27 |
|
28 | burstCache.clear();
|
29 | } else {
|
30 |
|
31 |
|
32 | var fromCache = burstCache.get(queryID, variables);
|
33 |
|
34 | if ((0, _helpers.isQuery)(requestNode) && fromCache !== null && !(0, _helpers.forceFetch)(cacheConfig)) {
|
35 | sink.next(fromCache);
|
36 | sink.complete();
|
37 | return cleanup;
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 | customFetcher(requestNode, variables, uploadables).then(function (response) {
|
44 | if (response.errors) {
|
45 |
|
46 |
|
47 | response.errors.map(function (error) {
|
48 | return console.error(error.message, error);
|
49 | });
|
50 | } else {
|
51 |
|
52 | burstCache.set(queryID, variables, response);
|
53 | }
|
54 |
|
55 | sink.next(response);
|
56 | sink.complete();
|
57 | }).catch(function (error) {
|
58 | sink.error(error);
|
59 | sink.complete();
|
60 | });
|
61 | return cleanup;
|
62 | });
|
63 | };
|
64 | } |
\ | No newline at end of file |