Created
November 6, 2017 16:35
-
-
Save jbaxleyiii/0c4edda1588268c0cb20056df5cc07ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ApolloLink, Observable } from 'apollo-link' | |
| export class TimeoutLink extends ApolloLink { | |
| constructor({ interval } = { interval: 1000 }) { | |
| this.interval = interval; | |
| } | |
| request(operation, forward){ | |
| return new Observable(obs, => { | |
| const handler = { | |
| next: obs.next.bind(obs), | |
| error: obs.error.bind(obs), | |
| complete: obs.complete.bind(obs) | |
| } | |
| let attempt = forward(operation); | |
| let sub = attempt.subscribe(handler); | |
| const timeout = setTimeout(() => { | |
| if (sub) sub.unsubscribe(); | |
| attempt = forward(operation); | |
| let sub = attempt.subscribe(handler); | |
| }, this.interval) | |
| return () => { | |
| if (sub) sub.unsubscribe() | |
| if (timeout) clearTimeout(timeout) | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment