UNPKG

3.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var zen_observable_ts_1 = tslib_1.__importDefault(require("zen-observable-ts"));
5var ts_invariant_1 = require("ts-invariant");
6var linkUtils_1 = require("./linkUtils");
7function passthrough(op, forward) {
8 return forward ? forward(op) : zen_observable_ts_1.default.of();
9}
10function toLink(handler) {
11 return typeof handler === 'function' ? new ApolloLink(handler) : handler;
12}
13function empty() {
14 return new ApolloLink(function () { return zen_observable_ts_1.default.of(); });
15}
16exports.empty = empty;
17function from(links) {
18 if (links.length === 0)
19 return empty();
20 return links.map(toLink).reduce(function (x, y) { return x.concat(y); });
21}
22exports.from = from;
23function split(test, left, right) {
24 var leftLink = toLink(left);
25 var rightLink = toLink(right || new ApolloLink(passthrough));
26 if (linkUtils_1.isTerminating(leftLink) && linkUtils_1.isTerminating(rightLink)) {
27 return new ApolloLink(function (operation) {
28 return test(operation)
29 ? leftLink.request(operation) || zen_observable_ts_1.default.of()
30 : rightLink.request(operation) || zen_observable_ts_1.default.of();
31 });
32 }
33 else {
34 return new ApolloLink(function (operation, forward) {
35 return test(operation)
36 ? leftLink.request(operation, forward) || zen_observable_ts_1.default.of()
37 : rightLink.request(operation, forward) || zen_observable_ts_1.default.of();
38 });
39 }
40}
41exports.split = split;
42exports.concat = function (first, second) {
43 var firstLink = toLink(first);
44 if (linkUtils_1.isTerminating(firstLink)) {
45 ts_invariant_1.invariant.warn(new linkUtils_1.LinkError("You are calling concat on a terminating link, which will have no effect", firstLink));
46 return firstLink;
47 }
48 var nextLink = toLink(second);
49 if (linkUtils_1.isTerminating(nextLink)) {
50 return new ApolloLink(function (operation) {
51 return firstLink.request(operation, function (op) { return nextLink.request(op) || zen_observable_ts_1.default.of(); }) || zen_observable_ts_1.default.of();
52 });
53 }
54 else {
55 return new ApolloLink(function (operation, forward) {
56 return (firstLink.request(operation, function (op) {
57 return nextLink.request(op, forward) || zen_observable_ts_1.default.of();
58 }) || zen_observable_ts_1.default.of());
59 });
60 }
61};
62var ApolloLink = (function () {
63 function ApolloLink(request) {
64 if (request)
65 this.request = request;
66 }
67 ApolloLink.prototype.split = function (test, left, right) {
68 return this.concat(split(test, left, right || new ApolloLink(passthrough)));
69 };
70 ApolloLink.prototype.concat = function (next) {
71 return exports.concat(this, next);
72 };
73 ApolloLink.prototype.request = function (operation, forward) {
74 throw new ts_invariant_1.InvariantError('request is not implemented');
75 };
76 ApolloLink.empty = empty;
77 ApolloLink.from = from;
78 ApolloLink.split = split;
79 ApolloLink.execute = execute;
80 return ApolloLink;
81}());
82exports.ApolloLink = ApolloLink;
83function execute(link, operation) {
84 return (link.request(linkUtils_1.createOperation(operation.context, linkUtils_1.transformOperation(linkUtils_1.validateOperation(operation)))) || zen_observable_ts_1.default.of());
85}
86exports.execute = execute;
87//# sourceMappingURL=link.js.map
\No newline at end of file