Skip to content
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

Non-duplication of concurrent requests #51

Closed
brianmhunt opened this issue Apr 14, 2012 · 3 comments
Closed

Non-duplication of concurrent requests #51

brianmhunt opened this issue Apr 14, 2012 · 3 comments

Comments

@brianmhunt
Copy link

When multiple concurrent requests occur, it would be valuable for amplify.js to reduce these requests to one ajax call.

For example, given:

amplify.request.define("X", "ajax", {
         url: "/x/{id}",
         dataType: "json",
         cache: true,
         type: "GET"
     });

amplify.request("X", { id: 1}, function () {});
amplify.request("X", { id: 1}, function () {});

Right now when I run this code (e.g. http://jsfiddle.net/Xg9gn/) two AJAX requests occur.

Obviously caching cannot occur until a request completes.

However, I believe a preferable model would be for the first request to block all future identical requests; those future requests would however subscribe to the results of the first request.

An option to force an AJAX request may be prudent (though I cannot think of a use case offhand).

Just a thought! :)

@scottgonzalez
Copy link
Contributor

Requests should not block. What you probably want is throttling that will put concurrent requests on hold and then respond with the cached response when the first request completes.

@brianmhunt
Copy link
Author

Sorry; "block" was a misleading word. The idea is that the request is not being 'blocked' in the async sense, but rather the request is added to a queue rather than submitting multiple duplicate AJAX requests. Here's some rough code:

function request(resource_name, callback) {
   this_request = some_lookup_of_requests.get(resource_name);

   if this_request.is_outstanding(resource_name) {
      // by outstanding - the request has been made, and a response not
      // yet received

     subscribe(this_request, callback); 
     // when the ajax returns, the results are published
   } else {
      amplify.request(resource_name, function (...) { 
        this_request.no_longer_outstanding();
        // make sure that subsequent requests do not subscribe when
        // no ajax request is outstanding

        callback(...); // tell the caller

        publish(this_request, ...); // notify any subscribers
      });
   }
}

This is not really caching in the traditional sense; it may be better to describe it as a subscriber/publisher alternative to multiple concurrent requests.

I am not sure if this is something amplify.js would want - but since it reduces the need for developers to think about the need for multiple concurrent requests I tend to think it is helpful - but in any case I hope the above clarifies what I had intended.

Plus it's fun. :)

Cheers.

@mariusfilipowski
Copy link

I am facing the same problem. It would be very cool to have such a mechanisms. Or how it be implemented on our own? Any hints?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants