Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b24bfae

Browse files
committed
Revert: ngAnimate changes for listening on visibilitychange
Reverts d3e123b, bf1acf7 and aa28e48. Backporting the changes is complicated because we don't destroy the rootScope after each test in 1.4.x (#14574)
1 parent d3e123b commit b24bfae

File tree

8 files changed

+50
-101
lines changed

8 files changed

+50
-101
lines changed

src/AngularPublic.js

-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
$CacheFactoryProvider,
6565
$ControllerProvider,
6666
$DocumentProvider,
67-
$$IsDocumentHiddenProvider,
6867
$ExceptionHandlerProvider,
6968
$FilterProvider,
7069
$$ForceReflowProvider,
@@ -227,7 +226,6 @@ function publishExternalAPI(angular) {
227226
$cacheFactory: $CacheFactoryProvider,
228227
$controller: $ControllerProvider,
229228
$document: $DocumentProvider,
230-
$$isDocumentHidden: $$IsDocumentHiddenProvider,
231229
$exceptionHandler: $ExceptionHandlerProvider,
232230
$filter: $FilterProvider,
233231
$$forceReflow: $$ForceReflowProvider,

src/ng/animateRunner.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ var $$AnimateAsyncRunFactoryProvider = function() {
2828
};
2929

3030
var $$AnimateRunnerFactoryProvider = function() {
31-
this.$get = ['$q', '$sniffer', '$$animateAsyncRun', '$$isDocumentHidden', '$timeout',
32-
function($q, $sniffer, $$animateAsyncRun, $$isDocumentHidden, $timeout) {
31+
this.$get = ['$q', '$sniffer', '$$animateAsyncRun', '$document', '$timeout',
32+
function($q, $sniffer, $$animateAsyncRun, $document, $timeout) {
3333

3434
var INITIAL_STATE = 0;
3535
var DONE_PENDING_STATE = 1;
@@ -81,7 +81,11 @@ var $$AnimateRunnerFactoryProvider = function() {
8181

8282
this._doneCallbacks = [];
8383
this._tick = function(fn) {
84-
if ($$isDocumentHidden()) {
84+
var doc = $document[0];
85+
86+
// the document may not be ready or attached
87+
// to the module for some internal tests
88+
if (doc && doc.hidden) {
8589
timeoutTick(fn);
8690
} else {
8791
rafTick(fn);

src/ng/document.js

-26
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,3 @@ function $DocumentProvider() {
3030
return jqLite(window.document);
3131
}];
3232
}
33-
34-
35-
/**
36-
* @private
37-
* Listens for document visibility change and makes the current status accessible.
38-
*/
39-
function $$IsDocumentHiddenProvider() {
40-
this.$get = ['$document', '$rootScope', function($document, $rootScope) {
41-
var doc = $document[0];
42-
var hidden = doc && doc.hidden;
43-
44-
$document.on('visibilitychange', changeListener);
45-
46-
$rootScope.$on('$destroy', function() {
47-
$document.off('visibilitychange', changeListener);
48-
});
49-
50-
function changeListener() {
51-
hidden = doc.hidden;
52-
}
53-
54-
return function() {
55-
return hidden;
56-
};
57-
}];
58-
}

src/ngAnimate/animateQueue.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
9797

9898
this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap',
9999
'$$animation', '$$AnimateRunner', '$templateRequest', '$$jqLite', '$$forceReflow',
100-
'$$isDocumentHidden',
101100
function($$rAF, $rootScope, $rootElement, $document, $$HashMap,
102-
$$animation, $$AnimateRunner, $templateRequest, $$jqLite, $$forceReflow,
103-
$$isDocumentHidden) {
101+
$$animation, $$AnimateRunner, $templateRequest, $$jqLite, $$forceReflow) {
104102

105103
var activeAnimationsLookup = new $$HashMap();
106104
var disabledElementsLookup = new $$HashMap();
@@ -333,7 +331,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
333331

334332
var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
335333

336-
var documentHidden = $$isDocumentHidden();
334+
var documentHidden = $document[0].hidden;
337335

338336
// this is a hard disable of all animations for the application or on
339337
// the element itself, therefore there is no need to continue further

test/ng/animateRunnerSpec.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ describe("$$AnimateRunner", function() {
163163
}));
164164

165165
it("should use timeouts to trigger async operations when the document is hidden", function() {
166-
var hidden = true;
166+
var doc;
167167

168168
module(function($provide) {
169-
170-
$provide.value('$$isDocumentHidden', function() {
171-
return hidden;
169+
doc = jqLite({
170+
body: document.body,
171+
hidden: true
172172
});
173+
$provide.value('$document', doc);
173174
});
174175

175176
inject(function($$AnimateRunner, $rootScope, $$rAF, $timeout) {
@@ -183,7 +184,7 @@ describe("$$AnimateRunner", function() {
183184
$timeout.flush();
184185
expect(spy).toHaveBeenCalled();
185186

186-
hidden = false;
187+
doc[0].hidden = false;
187188

188189
spy = jasmine.createSpy();
189190
runner = new $$AnimateRunner();

test/ng/compileSpec.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -6087,22 +6087,22 @@ describe('$compile', function() {
60876087
});
60886088

60896089
inject(function($compile, $rootScope) {
6090-
var cacheSize = jqLiteCacheSize();
6090+
expect(jqLiteCacheSize()).toEqual(0);
60916091

60926092
element = $compile('<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>')($rootScope);
6093-
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
6093+
expect(jqLiteCacheSize()).toEqual(1);
60946094

60956095
$rootScope.$apply('xs = [0,1]');
6096-
expect(jqLiteCacheSize()).toEqual(cacheSize + 2);
6096+
expect(jqLiteCacheSize()).toEqual(2);
60976097

60986098
$rootScope.$apply('xs = [0]');
6099-
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
6099+
expect(jqLiteCacheSize()).toEqual(1);
61006100

61016101
$rootScope.$apply('xs = []');
6102-
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
6102+
expect(jqLiteCacheSize()).toEqual(1);
61036103

61046104
element.remove();
6105-
expect(jqLiteCacheSize()).toEqual(cacheSize + 0);
6105+
expect(jqLiteCacheSize()).toEqual(0);
61066106
});
61076107
});
61086108

@@ -6119,22 +6119,22 @@ describe('$compile', function() {
61196119
});
61206120

61216121
inject(function($compile, $rootScope) {
6122-
var cacheSize = jqLiteCacheSize();
6122+
expect(jqLiteCacheSize()).toEqual(0);
61236123

61246124
element = $compile('<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>')($rootScope);
6125-
expect(jqLiteCacheSize()).toEqual(cacheSize);
6125+
expect(jqLiteCacheSize()).toEqual(0);
61266126

61276127
$rootScope.$apply('xs = [0,1]');
6128-
expect(jqLiteCacheSize()).toEqual(cacheSize);
6128+
expect(jqLiteCacheSize()).toEqual(0);
61296129

61306130
$rootScope.$apply('xs = [0]');
6131-
expect(jqLiteCacheSize()).toEqual(cacheSize);
6131+
expect(jqLiteCacheSize()).toEqual(0);
61326132

61336133
$rootScope.$apply('xs = []');
6134-
expect(jqLiteCacheSize()).toEqual(cacheSize);
6134+
expect(jqLiteCacheSize()).toEqual(0);
61356135

61366136
element.remove();
6137-
expect(jqLiteCacheSize()).toEqual(cacheSize);
6137+
expect(jqLiteCacheSize()).toEqual(0);
61386138
});
61396139
});
61406140

@@ -6150,26 +6150,26 @@ describe('$compile', function() {
61506150
});
61516151

61526152
inject(function($compile, $rootScope) {
6153-
var cacheSize = jqLiteCacheSize();
6153+
expect(jqLiteCacheSize()).toEqual(0);
61546154
element = $compile('<div><div ng-repeat="x in xs" ng-if="val">{{x}}</div></div>')($rootScope);
61556155

61566156
$rootScope.$apply('xs = [0,1]');
61576157
// At this point we have a bunch of comment placeholders but no real transcluded elements
61586158
// So the cache only contains the root element's data
6159-
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
6159+
expect(jqLiteCacheSize()).toEqual(1);
61606160

61616161
$rootScope.$apply('val = true');
61626162
// Now we have two concrete transcluded elements plus some comments so two more cache items
6163-
expect(jqLiteCacheSize()).toEqual(cacheSize + 3);
6163+
expect(jqLiteCacheSize()).toEqual(3);
61646164

61656165
$rootScope.$apply('val = false');
61666166
// Once again we only have comments so no transcluded elements and the cache is back to just
61676167
// the root element
6168-
expect(jqLiteCacheSize()).toEqual(cacheSize + 1);
6168+
expect(jqLiteCacheSize()).toEqual(1);
61696169

61706170
element.remove();
61716171
// Now we've even removed the root element along with its cache
6172-
expect(jqLiteCacheSize()).toEqual(cacheSize + 0);
6172+
expect(jqLiteCacheSize()).toEqual(0);
61736173
});
61746174
});
61756175

@@ -6206,7 +6206,6 @@ describe('$compile', function() {
62066206
});
62076207

62086208
inject(function($compile, $rootScope, $httpBackend, $timeout, $templateCache) {
6209-
var cacheSize = jqLiteCacheSize();
62106209
$httpBackend.whenGET('red.html').respond('<p>red.html</p>');
62116210
var template = $compile(
62126211
'<div ng-controller="Leak">' +
@@ -6221,7 +6220,7 @@ describe('$compile', function() {
62216220
$timeout.flush();
62226221
$httpBackend.flush();
62236222
expect(linkFn).not.toHaveBeenCalled();
6224-
expect(jqLiteCacheSize()).toEqual(cacheSize + 2);
6223+
expect(jqLiteCacheSize()).toEqual(2);
62256224

62266225
$templateCache.removeAll();
62276226
var destroyedScope = $rootScope.$new();
@@ -6984,7 +6983,9 @@ describe('$compile', function() {
69846983

69856984
it('should not leak memory with nested transclusion', function() {
69866985
inject(function($compile, $rootScope) {
6987-
var size, initialSize = jqLiteCacheSize();
6986+
var size;
6987+
6988+
expect(jqLiteCacheSize()).toEqual(0);
69886989

69896990
element = jqLite('<div><ul><li ng-repeat="n in nums">{{n}} => <i ng-if="0 === n%2">Even</i><i ng-if="1 === n%2">Odd</i></li></ul></div>');
69906991
$compile(element)($rootScope.$new());
@@ -6998,7 +6999,7 @@ describe('$compile', function() {
69986999
expect(jqLiteCacheSize()).toEqual(size);
69997000

70007001
element.remove();
7001-
expect(jqLiteCacheSize()).toEqual(initialSize);
7002+
expect(jqLiteCacheSize()).toEqual(0);
70027003
});
70037004
});
70047005
});

test/ng/documentSpec.js

-28
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,3 @@ describe('$document', function() {
2727
});
2828
});
2929
});
30-
31-
32-
describe('$$isDocumentHidden', function() {
33-
it('should return false by default', inject(function($$isDocumentHidden, $document) {
34-
expect($$isDocumentHidden()).toBeFalsy(); // undefined in browsers that don't support visibility
35-
}));
36-
37-
it('should listen on the visibilitychange event', function() {
38-
var spy = spyOn(document, 'addEventListener').andCallThrough();
39-
40-
inject(function($$isDocumentHidden, $document) {
41-
expect(spy.mostRecentCall.args[0]).toBe('visibilitychange');
42-
expect(spy.mostRecentCall.args[1]).toEqual(jasmine.any(Function));
43-
expect($$isDocumentHidden()).toBeFalsy(); // undefined in browsers that don't support visibility
44-
});
45-
46-
});
47-
48-
it('should remove the listener when the $rootScope is destroyed', function() {
49-
var spy = spyOn(document, 'removeEventListener').andCallThrough();
50-
51-
inject(function($$isDocumentHidden, $rootScope) {
52-
$rootScope.$destroy();
53-
expect(spy.mostRecentCall.args[0]).toBe('visibilitychange');
54-
expect(spy.mostRecentCall.args[1]).toEqual(jasmine.any(Function));
55-
});
56-
});
57-
});

test/ngAnimate/animateSpec.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,14 @@ describe("animations", function() {
156156
}));
157157

158158
it("should skip animations entirely if the document is hidden", function() {
159-
var hidden = true;
159+
var doc;
160160

161161
module(function($provide) {
162-
$provide.value('$$isDocumentHidden', function() {
163-
return hidden;
162+
doc = jqLite({
163+
body: document.body,
164+
hidden: true
164165
});
166+
$provide.value('$document', doc);
165167
});
166168

167169
inject(function($animate, $rootScope) {
@@ -170,7 +172,7 @@ describe("animations", function() {
170172
expect(capturedAnimation).toBeFalsy();
171173
expect(element[0].parentNode).toEqual(parent[0]);
172174

173-
hidden = false;
175+
doc[0].hidden = false;
174176

175177
$animate.leave(element);
176178
$rootScope.$digest();
@@ -2282,19 +2284,18 @@ describe("animations", function() {
22822284

22832285

22842286
describe('because the document is hidden', function() {
2285-
var hidden = true;
2286-
2287-
beforeEach(function() {
2288-
module(function($provide) {
2289-
$provide.value('$$isDocumentHidden', function() {
2290-
return hidden;
2291-
});
2287+
beforeEach(module(function($provide) {
2288+
var doc = jqLite({
2289+
body: document.body,
2290+
hidden: true
22922291
});
2293-
});
2292+
$provide.value('$document', doc);
2293+
}));
22942294

22952295
it('should trigger callbacks for an enter animation',
22962296
inject(function($animate, $rootScope, $rootElement, $document) {
22972297

2298+
var callbackTriggered = false;
22982299
var spy = jasmine.createSpy();
22992300
$animate.on('enter', jqLite($document[0].body), spy);
23002301

0 commit comments

Comments
 (0)