| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,7 @@ angularFiles = { | |||
| 11 | 11 | ||
| 12 | 12 | 'src/ng/anchorScroll.js', | |
| 13 | 13 | 'src/ng/animate.js', | |
| 14 | + 'src/ng/asyncCallback.js', | ||
| 14 | 15 | 'src/ng/browser.js', | |
| 15 | 16 | 'src/ng/cacheFactory.js', | |
| 16 | 17 | 'src/ng/compile.js', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,7 +73,7 @@ | |||
| 73 | 73 | $TemplateCacheProvider, | |
| 74 | 74 | $TimeoutProvider, | |
| 75 | 75 | $$RAFProvider, | |
| 76 | - $AsyncCallbackProvider, | ||
| 76 | + $$AsyncCallbackProvider, | ||
| 77 | 77 | $WindowProvider | |
| 78 | 78 | */ | |
| 79 | 79 | ||
@@ -214,7 +214,8 @@ function publishExternalAPI(angular){ | |||
| 214 | 214 | $templateCache: $TemplateCacheProvider, | |
| 215 | 215 | $timeout: $TimeoutProvider, | |
| 216 | 216 | $window: $WindowProvider, | |
| 217 | - $$rAF: $$RAFProvider | ||
| 217 | + $$rAF: $$RAFProvider, | ||
| 218 | + $$asyncCallback : $$AsyncCallbackProvider | ||
| 218 | 219 | }); | |
| 219 | 220 | } | |
| 220 | 221 | ]); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + function $$AsyncCallbackProvider(){ | ||
| 4 | + this.$get = ['$$rAF', '$timeout', function($$rAF, $timeout) { | ||
| 5 | + return $$rAF.supported | ||
| 6 | + ? function(fn) { return $$rAF(fn); } | ||
| 7 | + : function(fn) { | ||
| 8 | + return $timeout(fn, 0, false); | ||
| 9 | + }; | ||
| 10 | + }]; | ||
| 11 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1684,6 +1684,20 @@ angular.mock.$RAFDecorator = function($delegate) { | |||
| 1684 | 1684 | return rafFn; | |
| 1685 | 1685 | }; | |
| 1686 | 1686 | ||
| 1687 | + angular.mock.$AsyncCallbackDecorator = function($delegate) { | ||
| 1688 | + var callbacks = []; | ||
| 1689 | + var addFn = function(fn) { | ||
| 1690 | + callbacks.push(fn); | ||
| 1691 | + }; | ||
| 1692 | + addFn.flush = function() { | ||
| 1693 | + angular.forEach(callbacks, function(fn) { | ||
| 1694 | + fn(); | ||
| 1695 | + }); | ||
| 1696 | + callbacks = []; | ||
| 1697 | + }; | ||
| 1698 | + return addFn; | ||
| 1699 | + }; | ||
| 1700 | + | ||
| 1687 | 1701 | /** | |
| 1688 | 1702 | * | |
| 1689 | 1703 | */ | |
@@ -1718,6 +1732,7 @@ angular.module('ngMock', ['ng']).provider({ | |||
| 1718 | 1732 | }).config(['$provide', function($provide) { | |
| 1719 | 1733 | $provide.decorator('$timeout', angular.mock.$TimeoutDecorator); | |
| 1720 | 1734 | $provide.decorator('$$rAF', angular.mock.$RAFDecorator); | |
| 1735 | + $provide.decorator('$$asyncCallback', angular.mock.$AsyncCallbackDecorator); | ||
| 1721 | 1736 | }]); | |
| 1722 | 1737 | ||
| 1723 | 1738 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + describe('$$asyncCallback', function() { | ||
| 3 | + it('should perform a callback asynchronously', inject(function($$asyncCallback) { | ||
| 4 | + var message = 'hello there '; | ||
| 5 | + $$asyncCallback(function() { | ||
| 6 | + message += 'Angular'; | ||
| 7 | + }); | ||
| 8 | + | ||
| 9 | + expect(message).toBe('hello there '); | ||
| 10 | + $$asyncCallback.flush(); | ||
| 11 | + expect(message).toBe('hello there Angular'); | ||
| 12 | + })); | ||
| 13 | + | ||
| 14 | + describe('mocks', function() { | ||
| 15 | + it('should queue up all async callbacks', inject(function($$asyncCallback) { | ||
| 16 | + var callback = jasmine.createSpy('callback'); | ||
| 17 | + $$asyncCallback(callback); | ||
| 18 | + $$asyncCallback(callback); | ||
| 19 | + $$asyncCallback(callback); | ||
| 20 | + expect(callback.callCount).toBe(0); | ||
| 21 | + | ||
| 22 | + $$asyncCallback.flush(); | ||
| 23 | + expect(callback.callCount).toBe(3); | ||
| 24 | + | ||
| 25 | + $$asyncCallback(callback); | ||
| 26 | + $$asyncCallback(callback); | ||
| 27 | + expect(callback.callCount).toBe(3); | ||
| 28 | + | ||
| 29 | + $$asyncCallback.flush(); | ||
| 30 | + expect(callback.callCount).toBe(5); | ||
| 31 | + })); | ||
| 32 | + }); | ||
| 33 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments