saymedia / angularjs-server

Specialized server for AngularJS sites
MIT License
247 stars 49 forks source link

overriding $httpBackend breaks $browser.notifyWhenNoOutstandingRequests #34

Open graingert opened 8 years ago

graingert commented 8 years ago

there seems to be a lot of code in ngOverrides to count async callbacks when angular already does this in $browser.notifyWhenNoOutstandingRequests

However it turns out the overridden $httpBackend is breaking $browser.notifyWhenNoOutstandingRequests

it would be better to override $xhrFactory

graingert commented 8 years ago

I've got a version that just does:

  import { XMLHttpRequest } from 'xmlhttprequest';
  function $xhrFactoryProvider() {
    this.$get = function () {
      return function createXhr() {
        return new XMLHttpRequest();
      };
    };
  }

  module.provider('$xhrFactory', $xhrFactoryProvider);

and it works great

graingert commented 8 years ago

this way you don't need to worry about tracking $timouts and $http requests because it's done for you and you can just use $browser.notifyWhenNoOutstandingRequests

apparentlymart commented 8 years ago

Thanks for the suggestion, @graingert!

It was originally implemented this way because $browser.notifyWhenNoOutstandingRequests was defined as an implementation detail of the testing framework and not something apps should be relying on. We therefore implemented something that we could rely on, assuming that nobody else would be depending on something that was described as an implementation detail. :grinning:

Now that AngularJS 1 development is kinda ramping down in favor of 2, it's probably safer to assume that $browser.notifyWhenNoOutstandingRequests isn't going anywhere, but it still feels funny to me to depend on something that isn't even in the documentation.

Your alternative implementation does seem simpler, though presumably we'd still need to customise $httpBackend a little to get the special-cased JSON-P emulation.

graingert commented 8 years ago

@apparentlymart ah I don't use JSON-P so I'm safe.

graingert commented 8 years ago

the other options is to use:

angular.getTestability(element).whenStable(callback)

where element is the root jsdom document.

graingert commented 8 years ago

but it just calls $browser.notifyWhenNoOutstandingRequests(callback) so I imagine it's safe.