link.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var zen_observable_ts_1 = tslib_1.__importDefault(require("zen-observable-ts"));
  5. var ts_invariant_1 = require("ts-invariant");
  6. var linkUtils_1 = require("./linkUtils");
  7. function passthrough(op, forward) {
  8. return forward ? forward(op) : zen_observable_ts_1.default.of();
  9. }
  10. function toLink(handler) {
  11. return typeof handler === 'function' ? new ApolloLink(handler) : handler;
  12. }
  13. function empty() {
  14. return new ApolloLink(function () { return zen_observable_ts_1.default.of(); });
  15. }
  16. exports.empty = empty;
  17. function from(links) {
  18. if (links.length === 0)
  19. return empty();
  20. return links.map(toLink).reduce(function (x, y) { return x.concat(y); });
  21. }
  22. exports.from = from;
  23. function 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. }
  41. exports.split = split;
  42. exports.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. };
  62. var 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. }());
  82. exports.ApolloLink = ApolloLink;
  83. function 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. }
  86. exports.execute = execute;
  87. //# sourceMappingURL=link.js.map