Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
chore: low-hanging bundle size improvements in apollo-link (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored and benjamn committed Mar 5, 2019
1 parent ef1c2d1 commit 70f3423
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions packages/apollo-link/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ import {
createOperation,
} from './linkUtils';

const passthrough = (op, forward) => (forward ? forward(op) : Observable.of());
function passthrough(op, forward) {
return forward ? forward(op) : Observable.of();
}

const toLink = (handler: RequestHandler | ApolloLink) =>
typeof handler === 'function' ? new ApolloLink(handler) : handler;
function toLink(handler: RequestHandler | ApolloLink) {
return typeof handler === 'function' ? new ApolloLink(handler) : handler;
}

export const empty = (): ApolloLink =>
new ApolloLink((op, forward) => Observable.of());
export function empty(): ApolloLink {
return new ApolloLink(() => Observable.of());
}

export const from = (links: ApolloLink[]): ApolloLink => {
export function from(links: ApolloLink[]): ApolloLink {
if (links.length === 0) return empty();

return links.map(toLink).reduce((x, y) => x.concat(y));
};
}

export const split = (
export function split(
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right: ApolloLink | RequestHandler = new ApolloLink(passthrough),
): ApolloLink => {
right?: ApolloLink | RequestHandler,
): ApolloLink {
const leftLink = toLink(left);
const rightLink = toLink(right);
const rightLink = toLink(right || new ApolloLink(passthrough));

if (isTerminating(leftLink) && isTerminating(rightLink)) {
return new ApolloLink(operation => {
Expand All @@ -52,7 +55,7 @@ export const split = (
: rightLink.request(operation, forward) || Observable.of();
});
}
};
}

// join two Links together
export const concat = (
Expand Down Expand Up @@ -103,9 +106,9 @@ export class ApolloLink {
public split(
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right: ApolloLink | RequestHandler = new ApolloLink(passthrough),
right?: ApolloLink | RequestHandler,
): ApolloLink {
return this.concat(split(test, left, right));
return this.concat(split(test, left, right || new ApolloLink(passthrough)));
}

public concat(next: ApolloLink | RequestHandler): ApolloLink {
Expand Down

0 comments on commit 70f3423

Please sign in to comment.