-
Notifications
You must be signed in to change notification settings - Fork 52
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
Resolve multiple props with one callback #98
Comments
Why not do: @resolve({
propA, function() { ... },
propB, function() { ... }
}) That's what's happening internally:
💩 on me for not having this in the README or docs. |
Misread it. You're right, there's not a way to return an object for multiple props from a single call. I think this makes sense, especially for something like: @resolve(function(props) {
return lookup(props.something).then(result => {
return {
user: result.data,user,
place: result.data.place,
});
}) This makes sense from a signature standpoint because the props aren't defined until the promise resolves. However, this is a problem because it means the resolver doesn't know if the props are pre-existing or not. TBH, reducing duplicate in-flight requests should be handled via your request mechanism. Annoyingly, there are few implementations I found of this:
Or my own: export default class HttpActions extends Actions {
constructor(...args) {
super(...args);
this.requests = {};
}
get(url, params) {
const existing = this.cached(arguments);
if (existing) {
return existing;
}
const request = this.request("get", url, params);
return this.cache(request, arguments);
}
cache(request, signature) {
const key = JSON.stringify(Array.prototype.slice.call(signature));
this.requests[key] = request;
return request;
}
cached(signature) {
const key = JSON.stringify(Array.prototype.slice.call(signature));
return this.requests[key];
}
... |
Another backwards-compatible API would be something like: @resolve(["propA", "propB"], function(props) {
return promise(something).then((result) => [result.a, result.b]);
}) Because they're positional params, it's basically describing the required response signature should any of the props not exist. Again, I feel this warrants mentioning...In my own side-projects using Redux (meh), co-locating data requirements via If I figure this out, it'll probably solve it for several people. |
This all makes sense, @ericclemmons. re: Caching re: backwards compatible API I'd love to help out and try to come up with an implementation for this last suggestion 😄 My current solution basically hijacks Would |
small correction |
I got into this situation where my React component requires more than one prop.
One way to solve it was through adding two
@resolve
calls with two different callbacks.@resolve("propA", function() { remoteCall() .... return response.propA; })
@resolve("propB", function() { remoteCall().... return response.propB; })
However, I'd like to be able to collect multiple props in one single callback, and hence save one remote call.
I hacked something together that allows me to do something like:
@ericclemmons: I can put together a Pull Request if you think this makes sense.
The text was updated successfully, but these errors were encountered: