This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -848,7 +848,7 @@ function $RootScopeProvider() {
848
848
849
849
clearPhase ( ) ;
850
850
851
- while ( postDigestQueue . length ) {
851
+ while ( ! postDigestQueue . isEmpty ( ) ) {
852
852
try {
853
853
postDigestQueue . shift ( ) ( ) ;
854
854
} catch ( e ) {
@@ -1306,7 +1306,7 @@ function $RootScopeProvider() {
1306
1306
1307
1307
//The internal queues. Expose them on the $rootScope for debugging/testing purposes.
1308
1308
var asyncQueue = $rootScope . $$asyncQueue = [ ] ;
1309
- var postDigestQueue = $rootScope . $$postDigestQueue = [ ] ;
1309
+ var postDigestQueue = $rootScope . $$postDigestQueue = new Queue ( ) ;
1310
1310
var applyAsyncQueue = $rootScope . $$applyAsyncQueue = [ ] ;
1311
1311
1312
1312
return $rootScope ;
@@ -1364,5 +1364,28 @@ function $RootScopeProvider() {
1364
1364
} ) ;
1365
1365
}
1366
1366
}
1367
+
1368
+ // A simple queue implementation used for postDigestQueue,
1369
+ // taken from http://code.stephenmorley.org/javascript/queues/
1370
+ // See https://github.com/angular/angular.js/issues/14534
1371
+ function Queue ( ) {
1372
+ var queue = [ ] ;
1373
+ var offset = 0 ;
1374
+ this . isEmpty = function ( ) {
1375
+ return queue . length === 0 ;
1376
+ } ;
1377
+ this . push = function ( item ) {
1378
+ queue . push ( item ) ;
1379
+ } ;
1380
+ this . shift = function ( ) {
1381
+ if ( queue . length === 0 ) return undefined ;
1382
+ var item = queue [ offset ] ;
1383
+ if ( ++ offset * 2 >= queue . length ) {
1384
+ queue = queue . slice ( offset ) ;
1385
+ offset = 0 ;
1386
+ }
1387
+ return item ;
1388
+ } ;
1389
+ }
1367
1390
} ] ;
1368
1391
}
Original file line number Diff line number Diff line change @@ -1320,6 +1320,29 @@ describe('Scope', function() {
1320
1320
expect ( externalWatchCount ) . toEqual ( 0 ) ;
1321
1321
} ) ) ;
1322
1322
1323
+ it ( 'should process $$postDigest callbacks as a queue (FIFO) when the scope is digested' , inject ( function ( $rootScope ) {
1324
+ var result = '' ;
1325
+
1326
+ $rootScope . $$postDigest ( function ( ) {
1327
+ result += 'a' ;
1328
+ $rootScope . $$postDigest ( function ( ) {
1329
+ result += 'd' ;
1330
+ } ) ;
1331
+ } ) ;
1332
+
1333
+ $rootScope . $$postDigest ( function ( ) {
1334
+ result += 'b' ;
1335
+ } ) ;
1336
+
1337
+ $rootScope . $$postDigest ( function ( ) {
1338
+ result += 'c' ;
1339
+ } ) ;
1340
+
1341
+ expect ( result ) . toBe ( '' ) ;
1342
+ $rootScope . $digest ( ) ;
1343
+ expect ( result ) . toBe ( 'abcd' ) ;
1344
+ } ) ) ;
1345
+
1323
1346
it ( 'should run a $$postDigest call on all child scopes when a parent scope is digested' , inject ( function ( $rootScope ) {
1324
1347
var parent = $rootScope . $new ( ) ,
1325
1348
child = parent . $new ( ) ,
You can’t perform that action at this time.
0 commit comments