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

Allow Request Cancelation #287

Closed
wants to merge 2 commits into from
Closed

Conversation

hellsan631
Copy link

Description

Adds the cancellable: true field to all generated $request entities, allowing for request cancelation.

After adding this field means all returned resource objects have a function called $cancelRequest() which can be used to cancel a request in transit.

Previously, you were not able to cancel a request generated by lbservices. This is a common pattern that should be supported, but with the addition of this PR, it is.

Example:

let request = false;

function findByName(name) {
  if (request) {
    request.$cancelRequest();
  }
  request = Users.find({
    filter: {
      where: { name },
    }
  });
  return request.$promise;
}

...
// Then Somewhere in your logic, fire 2 requests
findByName('Thomas')
  .then((user) => $scope.user = user);

// The previous request gets canceled, allowing this one to resolve
findByName('Dave')
  .then((user) => $scope.user = user);

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

@hellsan631 hellsan631 requested a review from bajtos as a code owner June 1, 2018 21:46
@slnode
Copy link

slnode commented Jun 1, 2018

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@bajtos bajtos self-assigned this Jun 4, 2018
@bajtos
Copy link
Member

bajtos commented Jun 4, 2018

Thank you for the pull request! I agree with you that cancellable mode is something our SDK should support.

However, I am not familiar with Angular internals and I am concerned about possible impact on existing users. Why isn't cancellable already enabled by default by Angular? What are the downsides of cancellable requests - are there situations where we don't wont the request to be cancellable?

@bajtos
Copy link
Member

bajtos commented Jun 4, 2018

Also, shouldn't we enable cancellable for GET requests only?

@hellsan631
Copy link
Author

hellsan631 commented Jun 4, 2018

The reason cancellable isn't enabled by default it seems, is due to backwards compatibility. Cancellable requests were added in v1.5 of angular's ngResource module. Prior to that, they only supported canceling requests by timeout value. This value "could" be a promise but its behavior was buggy. Since v1.4.9, $timeout has to be a pure numeric value, while promise based cancelation is supported only via cancellable: true.

Post requests to end-points can be canceled, as its a part of the XHR spec, and in modern-day popular libraries (like axios) they support cancelation via promises as well. I think the behavior should support this. Adding cancellable: true to all ngResources is the only way to generate promises for them.

Ideally, i think, i'd love to see an option per request to enable/disable this feature. Since there is an "options" param on most endpoints, we could use that. It might look something like this:

request = Users.find({
  filter: {
    where: { name },
  }
  options: {
    cancellable: true,
  }
});

But i'm not entirely familiar with how to go about doing this. If you could point me in the right direction, i'd love to tackle this (and maybe add timeout configuration support as well).

And to note, since the cancellable: true is only supported in angular.js#1.5.0-rc2 and above, if the PR is accepted, it would require a version bump to 1.5 for tests to pass.

@bajtos
Copy link
Member

bajtos commented Jun 5, 2018

Thank you @hellsan631 for detailed explanation. I am ok to proceed with this pull request then!

And to note, since the cancellable: true is only supported in angular.js#1.5.0-rc2 and above, if the PR is accepted, it would require a version bump to 1.5 for tests to pass.

Could you please do that as part of this pull request? Here is the place where we are defining which Angular version to fetch:

"angular": "^1.4.9",
"angular-mocks": "^1.4.9",
"angular-resource": "^1.4.9",

Please check why the tests are failing on Travis. Apparently, the build is installing the latest Angular version 1.7.0 (as specified in our package.json file, see build log), I think there is some other problem causing your patch to not work as intended.

Ideally, i think, i'd love to see an option per request to enable/disable this feature. Since there is an "options" param on most endpoints, we could use that. It might look something like this:

Unfortunately, there are no options AFAICT, there are only parameters and postData. See https://docs.angularjs.org/api/ngResource/service/$resource:

The action methods on the class object or instance object can be invoked with the following parameters:

  • "class" actions without a body: Resource.action([parameters], [success], [error])
  • "class" actions with a body: Resource.action([parameters], postData, [success], [error])
  • instance actions: instance.$action([parameters], [success], [error])

it('has a $cancelRequest property', function() {
var list = MyModel.find(
function() {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty line.

var list = MyModel.find
  function() {},
  util.throwHttpError
);

@bajtos
Copy link
Member

bajtos commented Oct 11, 2018

With the release of LoopBack 4.0 GA (see the announcement), this SDK has entered Active LTS mode which means we are no longer accepting new features (see our LTS policy).

I have to close this pull request as rejected, sorry for that.

@bajtos bajtos closed this Oct 11, 2018
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

Successfully merging this pull request may close these issues.

3 participants