Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Created November 6, 2017 16:35
Show Gist options
  • Select an option

  • Save jbaxleyiii/0c4edda1588268c0cb20056df5cc07ef to your computer and use it in GitHub Desktop.

Select an option

Save jbaxleyiii/0c4edda1588268c0cb20056df5cc07ef to your computer and use it in GitHub Desktop.
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