Skip to content

Instantly share code, notes, and snippets.

@eyston
Created January 16, 2016 02:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save eyston/ce723b38b1756cb5f81e to your computer and use it in GitHub Desktop.
sendSubscription(request: RelaySubscriptionRequest): Subscription {
console.log('sending subscription');
console.log(request.getQueryString());
console.log(request.getVariables());
let count = 0;
let interval;
let timeout = setTimeout(() => {
interval = setInterval(() => {
count += 1;
request.onNext({
response: {
addTodoSubscribe: {
todoEdge: {
__typename: 'TodoEdge',
node: {
id: (100 + count).toString(),
__typename: 'Todo',
text: `Subscribed Todo #${count}`,
complete: false
},
cursor: `abc123${count}`
},
viewer: {
id: "VXNlcjptZQ==",
totalCount: 2 + count
}
}
}
});
if (Math.random() > 0.95) {
request.onError('omg an error');
}
if (count > 20) {
request.onCompleted();
}
}, 500);
}, 2000);
// the idea is that you return a function that cleans up / ends / closes / whatever
// the subscription. Idea stolen from Observable.create in RxJS:
//
// https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/create.md
//
// can be a function, or an object with a dispose method, or nothing at all.
return {
dispose() {
clearTimeout(timeout);
clearInterval(interval);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment