-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
subscribeToMore for observableQuery #797
Conversation
@@ -24,6 +24,7 @@ Expect active development and potentially significant breaking changes in the `0 | |||
- Fix multidimentional array handling. [Issue #776](https://github.com/apollostack/apollo-client/issues/776) [PR #785](https://github.com/apollostack/apollo-client/pull/785) | |||
- Add support for Enum inline arguments [Issue #183](https://github.com/apollostack/apollo-client/issues/183) [PR #788](https://github.com/apollostack/apollo-client/pull/788) | |||
- Make it possible to subscribe to the same observable query multiple times. The query is initialized on the first subscription, and torn down after the last. Now, QueryManager is only aware of one subscription from the ObservableQuery, no matter how many were actually registered. This fixes issues with `result()` and other ObservableQuery features that relied on subscribing multiple times to work. This should remove the need for the workaround in `0.4.21`. [Repro in PR #694](https://github.com/apollostack/apollo-client/pull/694) [PR #791](https://github.com/apollostack/apollo-client/pull/791) | |||
- ** new Feature **: Add fetchMore-style subscribeToMore function which updates a query result based on a subscription. [PR #797](https://github.com/apollostack/apollo-client/pull/797) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you're allowed to put spaces between the **
like this in markdown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's funny, because I think I copy-pasted it from another entry :D Will fix both.
@@ -165,7 +168,8 @@ export class ObservableQuery extends Observable<ApolloQueryResult> { | |||
const reducer = fetchMoreOptions.updateQuery; | |||
const mapFn = (previousResult: any, { variables }: {variables: any }) => { | |||
|
|||
// TODO REFACTOR: reached max recursion depth (figuratively). Continue renaming to variables further down when we have time. | |||
// TODO REF: reached max recursion depth (fig) when renaming queryVariables to variables. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think using abbreviations in comments is a good idea.
@@ -178,6 +182,49 @@ export class ObservableQuery extends Observable<ApolloQueryResult> { | |||
}); | |||
} | |||
|
|||
// XXX the subscription variables are separate from the query variables. | |||
// if you want to update subscription variables, right now you have to do that separately, | |||
// and you can only do it by stopping the subscription and then subscribing again with new variables. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
// XXX technically we should also remove it from this.subscriptionHandles | ||
const i = this.subscriptionHandles.indexOf(subscription); | ||
if (i >= 0) { | ||
this.subscriptionHandles.splice(i, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the above comment about removing it out of date?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is indeed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't splice removing it?
maybe there is a typo in the comment?
|
||
this.subscriptionHandles.push(subscription); | ||
|
||
return () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we later want to return more stuff like the errors, we will wish we didn't just return a function here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the convention is to return observable, then right after use observer.error(error)
document: Document; | ||
variables?: { [key: string]: any }; | ||
updateQuery: (previousQueryResult: Object, options: { | ||
subscriptionData: any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we just wrap this in a data: ...
to keep it consistent with fetchMore
? I feel like that would be better than having two different but very similar APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree with you that both should be consistent..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I wasn't sure where to do it. I actually did that in another place, but I'd rather solve it in the subscriptions themselves. Will do the "patching" for now.
@@ -80,6 +82,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> { | |||
this.queryId = queryId; | |||
this.shouldSubscribe = shouldSubscribe; | |||
this.observers = []; | |||
this.subscriptionHandles = []; | |||
} | |||
|
|||
public result(): Promise<ApolloQueryResult> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When converting Observables to promises the convention is usually to take last value emitted, not first value emitted.
which means you should save last value emitted, and only when complete reach resolve it for the promise.
also, i would have a global util function to do that task, and then just call it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that should go as a comment on this PR, since that code was not modified at all. Perhaps open a new issue or a PR? But I'm not sure how following that convention will improve the result
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah you are right. (regarding commenting here)
it will be improved because:
- Observable to promise is something that alot of people do, there is a way to do it.
- it should be one logic used towards the whole project, doing it over and over is redundant.
want me to open an issue for it and later on check that?
@@ -342,6 +389,9 @@ export class ObservableQuery extends Observable<ApolloQueryResult> { | |||
this.scheduler.stopPollingQuery(this.queryId); | |||
} | |||
|
|||
// stop all active GraphQL subscriptions | |||
this.subscriptionHandles.forEach( sub => sub.unsubscribe() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would re-init subscriptionHandlers into an empty array at that point...
i mean, technicality, it is not needed as the teardown function cleans it.
but for people that are not familiar with observables i would add an explicit re-init
so it will be clear that it's empty at that point.
document: Document; | ||
variables?: { [key: string]: any }; | ||
updateQuery: (previousQueryResult: Object, options: { | ||
subscriptionData: any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree with you that both should be consistent..
|
||
setTimeout(() => { | ||
sub.unsubscribe(); | ||
assert.equal(counter, 3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure exceptions will raise (and not just print) that way (setTimeout, annon func)
did you verify that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it worked alright.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, i have bad experience with timeouts and exceptions hehe..
better safe then sorry ;)
// XXX the subscription variables are separate from the query variables. | ||
// if you want to update subscription variables, right now you have to do that separately, | ||
// and you can only do it by stopping the subscription and then subscribing again with new variables. | ||
public subscribeToMore( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it should return observable not just teardown function.
which means, wrap it with: return new Observable((observer) => {
observable.subscribe(map...., observer.error, observer.complete)
return () => ....
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it but decided against it, because I think if people want access to the subscription, they should use the client.susbcribe
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the whole point of this function is to update the store so the orginal observable will get updates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that's pretty much it :)
I filed an issue for the one big outstanding task: #803 |
TODO: