Giter Site home page Giter Site logo

Comments (5)

ericclemmons avatar ericclemmons commented on September 17, 2024

Why not do:

@resolve({
  propA, function() { ... },
  propB, function() { ... }
})

That's what's happening internally:

const asyncProps = (arguments.length === 1) ? prop : { [prop]: promise };

💩 on me for not having this in the README or docs.

from react-resolver.

ericclemmons avatar ericclemmons commented on September 17, 2024

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:

https://github.com/matthiasak/universal-utils/blob/master/src/fetch.js

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];
  }
  ...

from react-resolver.

ericclemmons avatar ericclemmons commented on September 17, 2024

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 @resolve to their immediate components makes sense & works, but it's difficult to solve the duplicate request issue with Redux & in-flight requests.

If I figure this out, it'll probably solve it for several people.

from react-resolver.

nettofarah avatar nettofarah commented on September 17, 2024

This all makes sense, @ericclemmons.

re: Caching
It is definitely a solution, but it feels a little brittle. Mostly because it adds cache invalidation to the mix.

re: backwards compatible API
I hacked something together that basically just allows you to use a hash as opposed to an array. But I see the value of taking your approach to it.

I'd love to help out and try to come up with an implementation for this last suggestion 😄

My current solution basically hijacks resolve.js and checks if a complex has been returned from Resolver and then just deconstructs it.

Would resolve.js be the place to make this changes? Or does this sound more like something Resolver should handle?

from react-resolver.

MatviiStelmakh avatar MatviiStelmakh commented on September 17, 2024

Why not do:

@resolve({
  propA, function() { ... },
  propB, function() { ... }
})

small correction : instead of ,
@resolve({
propA: function() { ... },
propB: function() { ... }
})

from react-resolver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.