Skip to content

Commit e00f10c

Browse files
committed
DO NOT MERGE alternative to angular#13128 wrt tests
1 parent 7c792f4 commit e00f10c

File tree

3 files changed

+30
-64
lines changed

3 files changed

+30
-64
lines changed

src/ng/http.js

+4-42
Original file line numberDiff line numberDiff line change
@@ -308,34 +308,6 @@ function $HttpProvider() {
308308
paramSerializer: '$httpParamSerializer'
309309
};
310310

311-
var useApplyAsync = false;
312-
/**
313-
* @ngdoc method
314-
* @name $httpProvider#useApplyAsync
315-
* @description
316-
*
317-
* Configure $http service to combine processing of multiple http responses received at around
318-
* the same time via {@link ng.$rootScope.Scope#$applyAsync $rootScope.$applyAsync}. This can result in
319-
* significant performance improvement for bigger applications that make many HTTP requests
320-
* concurrently (common during application bootstrap).
321-
*
322-
* Defaults to false. If no value is specified, returns the current configured value.
323-
*
324-
* @param {boolean=} value If true, when requests are loaded, they will schedule a deferred
325-
* "apply" on the next tick, giving time for subsequent requests in a roughly ~10ms window
326-
* to load and share the same digest cycle.
327-
*
328-
* @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining.
329-
* otherwise, returns the current configured value.
330-
**/
331-
this.useApplyAsync = function(value) {
332-
if (isDefined(value)) {
333-
useApplyAsync = !!value;
334-
return this;
335-
}
336-
return useApplyAsync;
337-
};
338-
339311
var useLegacyPromise = true;
340312
/**
341313
* @ngdoc method
@@ -375,8 +347,8 @@ function $HttpProvider() {
375347
**/
376348
var interceptorFactories = this.interceptors = [];
377349

378-
this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
379-
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {
350+
this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$q', '$injector',
351+
function($httpBackend, $$cookieReader, $cacheFactory, $q, $injector) {
380352

381353
var defaultCache = $cacheFactory('$http');
382354

@@ -404,7 +376,6 @@ function $HttpProvider() {
404376
* @name $http
405377
* @requires ng.$httpBackend
406378
* @requires $cacheFactory
407-
* @requires $rootScope
408379
* @requires $q
409380
* @requires $injector
410381
*
@@ -1186,7 +1157,7 @@ function $HttpProvider() {
11861157
* Makes the request.
11871158
*
11881159
* !!! ACCESSES CLOSURE VARS:
1189-
* $httpBackend, defaults, $log, $rootScope, defaultCache, $http.pendingRequests
1160+
* $httpBackend, defaults, $log, defaultCache, $http.pendingRequests
11901161
*/
11911162
function sendReq(config, reqData) {
11921163
var deferred = $q.defer(),
@@ -1261,16 +1232,7 @@ function $HttpProvider() {
12611232
}
12621233
}
12631234

1264-
function resolveHttpPromise() {
1265-
resolvePromise(response, status, headersString, statusText);
1266-
}
1267-
1268-
if (useApplyAsync) {
1269-
$rootScope.$applyAsync(resolveHttpPromise);
1270-
} else {
1271-
resolveHttpPromise();
1272-
if (!$rootScope.$$phase) $rootScope.$apply();
1273-
}
1235+
resolvePromise(response, status, headersString, statusText);
12741236
}
12751237

12761238

test/ng/directive/ngIncludeSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ describe('ngInclude', function() {
372372
$httpBackend.expectGET('include.svg').respond('<rect></rect><rect></rect>');
373373
element = $compile('<svg><test></test></svg>')($rootScope);
374374
$httpBackend.flush();
375+
$httpBackend.flush();
375376
var child = element.find('rect');
376377
expect(child.length).toBe(2);
377378
expect(child[0] instanceof SVGRectElement).toBe(true);

test/ng/httpSpec.js

+25-22
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe('$http', function() {
293293
$httpBackend = $hb;
294294
$http = $h;
295295
$rootScope = $rs;
296-
spyOn($rootScope, '$apply').andCallThrough();
296+
spyOn($rootScope, '$evalAsync').andCallThrough();
297297
}]));
298298

299299
it('should throw error if the request configuration is not an object', function() {
@@ -1012,31 +1012,37 @@ describe('$http', function() {
10121012
});
10131013

10141014

1015-
describe('scope.$apply', function() {
1015+
describe('$rootScope.$evalAsync', function() {
10161016

1017-
it('should $apply after success callback', function() {
1018-
$httpBackend.when('GET').respond(200);
1019-
$http({method: 'GET', url: '/some'});
1020-
$httpBackend.flush();
1021-
expect($rootScope.$apply).toHaveBeenCalledOnce();
1017+
it('should $evalAsync after success callback', function() {
1018+
$httpBackend.whenGET().respond(200);
1019+
$http.get('/some');
1020+
$rootScope.$digest(); // Send the request
1021+
$rootScope.$evalAsync.reset();
1022+
$httpBackend.flush(null, false);
1023+
expect($rootScope.$evalAsync).toHaveBeenCalledOnce();
10221024
});
10231025

10241026

1025-
it('should $apply after error callback', function() {
1026-
$httpBackend.when('GET').respond(404);
1027-
$http({method: 'GET', url: '/some'});
1028-
$httpBackend.flush();
1029-
expect($rootScope.$apply).toHaveBeenCalledOnce();
1027+
it('should $evalAsync after error callback', function() {
1028+
$httpBackend.whenGET().respond(404);
1029+
$http.get('/some');
1030+
$rootScope.$digest(); // Send the request
1031+
$rootScope.$evalAsync.reset();
1032+
$httpBackend.flush(null, false);
1033+
expect($rootScope.$evalAsync).toHaveBeenCalledOnce();
10301034
});
10311035

10321036

1033-
it('should $apply even if exception thrown during callback', inject(function($exceptionHandler) {
1034-
$httpBackend.when('GET').respond(200);
1037+
it('should $evalAsync even if exception thrown during callback', inject(function($exceptionHandler) {
1038+
$httpBackend.whenGET().respond(200);
10351039
callback.andThrow('error in callback');
10361040

1037-
$http({method: 'GET', url: '/some'}).then(callback);
1038-
$httpBackend.flush();
1039-
expect($rootScope.$apply).toHaveBeenCalledOnce();
1041+
$http.get('/some');
1042+
$rootScope.$digest(); // Send the request
1043+
$rootScope.$evalAsync.reset();
1044+
$httpBackend.flush(null, false);
1045+
expect($rootScope.$evalAsync).toHaveBeenCalledOnce();
10401046

10411047
$exceptionHandler.errors = [];
10421048
}));
@@ -1921,9 +1927,7 @@ describe('$http', function() {
19211927

19221928
describe('$http with $applyAsync', function() {
19231929
var $http, $httpBackend, $rootScope, $browser, log;
1924-
beforeEach(module(function($httpProvider) {
1925-
$httpProvider.useApplyAsync(true);
1926-
}, provideLog));
1930+
beforeEach(module(provideLog));
19271931

19281932

19291933
beforeEach(inject(['$http', '$httpBackend', '$rootScope', '$browser', 'log', function(http, backend, scope, browser, logger) {
@@ -1939,15 +1943,14 @@ describe('$http with $applyAsync', function() {
19391943
}]));
19401944

19411945

1942-
it('should schedule coalesced apply on response', function() {
1946+
it('should defer response handling', function() {
19431947
var handler = jasmine.createSpy('handler');
19441948
$httpBackend.expect('GET', '/template1.html').respond(200, '<h1>Header!</h1>', {});
19451949
$http.get('/template1.html').then(handler);
19461950
// Ensure requests are sent
19471951
$rootScope.$digest();
19481952

19491953
$httpBackend.flush(null, false);
1950-
expect($rootScope.$applyAsync).toHaveBeenCalledOnce();
19511954
expect(handler).not.toHaveBeenCalled();
19521955

19531956
$browser.defer.flush();

0 commit comments

Comments
 (0)