1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var globals = require('../../utilities/globals');
|
6 | var utilities = require('../../utilities');
|
7 | var utils = require('../utils');
|
8 |
|
9 | function passthrough(op, forward) {
|
10 | return (forward ? forward(op) : utilities.Observable.of());
|
11 | }
|
12 | function toLink(handler) {
|
13 | return typeof handler === "function" ? new ApolloLink(handler) : handler;
|
14 | }
|
15 | function isTerminating(link) {
|
16 | return link.request.length <= 1;
|
17 | }
|
18 | var ApolloLink = (function () {
|
19 | function ApolloLink(request) {
|
20 | if (request)
|
21 | this.request = request;
|
22 | }
|
23 | ApolloLink.empty = function () {
|
24 | return new ApolloLink(function () { return utilities.Observable.of(); });
|
25 | };
|
26 | ApolloLink.from = function (links) {
|
27 | if (links.length === 0)
|
28 | return ApolloLink.empty();
|
29 | return links.map(toLink).reduce(function (x, y) { return x.concat(y); });
|
30 | };
|
31 | ApolloLink.split = function (test, left, right) {
|
32 | var leftLink = toLink(left);
|
33 | var rightLink = toLink(right || new ApolloLink(passthrough));
|
34 | var ret;
|
35 | if (isTerminating(leftLink) && isTerminating(rightLink)) {
|
36 | ret = new ApolloLink(function (operation) {
|
37 | return test(operation) ?
|
38 | leftLink.request(operation) || utilities.Observable.of()
|
39 | : rightLink.request(operation) || utilities.Observable.of();
|
40 | });
|
41 | }
|
42 | else {
|
43 | ret = new ApolloLink(function (operation, forward) {
|
44 | return test(operation) ?
|
45 | leftLink.request(operation, forward) || utilities.Observable.of()
|
46 | : rightLink.request(operation, forward) || utilities.Observable.of();
|
47 | });
|
48 | }
|
49 | return Object.assign(ret, { left: leftLink, right: rightLink });
|
50 | };
|
51 | ApolloLink.execute = function (link, operation) {
|
52 | return (link.request(utils.createOperation(operation.context, utils.transformOperation(utils.validateOperation(operation)))) || utilities.Observable.of());
|
53 | };
|
54 | ApolloLink.concat = function (first, second) {
|
55 | var firstLink = toLink(first);
|
56 | if (isTerminating(firstLink)) {
|
57 | globalThis.__DEV__ !== false && globals.invariant.warn(36, firstLink);
|
58 | return firstLink;
|
59 | }
|
60 | var nextLink = toLink(second);
|
61 | var ret;
|
62 | if (isTerminating(nextLink)) {
|
63 | ret = new ApolloLink(function (operation) {
|
64 | return firstLink.request(operation, function (op) { return nextLink.request(op) || utilities.Observable.of(); }) || utilities.Observable.of();
|
65 | });
|
66 | }
|
67 | else {
|
68 | ret = new ApolloLink(function (operation, forward) {
|
69 | return (firstLink.request(operation, function (op) {
|
70 | return nextLink.request(op, forward) || utilities.Observable.of();
|
71 | }) || utilities.Observable.of());
|
72 | });
|
73 | }
|
74 | return Object.assign(ret, { left: firstLink, right: nextLink });
|
75 | };
|
76 | ApolloLink.prototype.split = function (test, left, right) {
|
77 | return this.concat(ApolloLink.split(test, left, right || new ApolloLink(passthrough)));
|
78 | };
|
79 | ApolloLink.prototype.concat = function (next) {
|
80 | return ApolloLink.concat(this, next);
|
81 | };
|
82 | ApolloLink.prototype.request = function (operation, forward) {
|
83 | throw globals.newInvariantError(37);
|
84 | };
|
85 | ApolloLink.prototype.onError = function (error, observer) {
|
86 | if (observer && observer.error) {
|
87 | observer.error(error);
|
88 | return false;
|
89 | }
|
90 | throw error;
|
91 | };
|
92 | ApolloLink.prototype.setOnError = function (fn) {
|
93 | this.onError = fn;
|
94 | return this;
|
95 | };
|
96 | return ApolloLink;
|
97 | }());
|
98 |
|
99 | var empty = ApolloLink.empty;
|
100 |
|
101 | var from = ApolloLink.from;
|
102 |
|
103 | var split = ApolloLink.split;
|
104 |
|
105 | var concat = ApolloLink.concat;
|
106 |
|
107 | var execute = ApolloLink.execute;
|
108 |
|
109 | exports.ApolloLink = ApolloLink;
|
110 | exports.concat = concat;
|
111 | exports.empty = empty;
|
112 | exports.execute = execute;
|
113 | exports.from = from;
|
114 | exports.split = split;
|
115 |
|