This file contains 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
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