@@ -10,8 +10,10 @@ import {
1010 HostViewFactoryRef ,
1111 Provider ,
1212 Type ,
13+ Testability ,
1314 APPLICATION_COMMON_PROVIDERS
1415} from 'angular2/core' ;
16+ import { global } from 'angular2/src/facade/lang' ;
1517import { ObservableWrapper } from 'angular2/src/facade/async' ;
1618import { BROWSER_PROVIDERS , BROWSER_APP_PROVIDERS } from 'angular2/platform/browser' ;
1719
@@ -23,6 +25,7 @@ import {
2325 NG1_PARSE ,
2426 NG1_ROOT_SCOPE ,
2527 NG1_SCOPE ,
28+ NG1_TESTABILITY ,
2629 NG2_APP_VIEW_MANAGER ,
2730 NG2_COMPILER ,
2831 NG2_INJECTOR ,
@@ -309,6 +312,7 @@ export class UpgradeAdapter {
309312 var rootScope : angular . IRootScopeService ;
310313 var hostViewFactoryRefMap : HostViewFactoryRefMap = { } ;
311314 var ng1Module = angular . module ( this . idPrefix , modules ) ;
315+ var ng1BootstrapPromise : Promise < any > = null ;
312316 var ng1compilePromise : Promise < any > = null ;
313317 ng1Module . value ( NG2_INJECTOR , injector )
314318 . value ( NG2_ZONE , ngZone )
@@ -331,23 +335,68 @@ export class UpgradeAdapter {
331335 return rootScope = rootScopeDelegate ;
332336 }
333337 ] ) ;
334- }
335- ] )
336- . run ( [
337- '$injector' ,
338- '$rootScope' ,
339- ( injector : angular . IInjectorService , rootScope : angular . IRootScopeService ) => {
340- ng1Injector = injector ;
341- ObservableWrapper . subscribe ( ngZone . onMicrotaskEmpty ,
342- ( _ ) => ngZone . runOutsideAngular ( ( ) => rootScope . $apply ( ) ) ) ;
343- ng1compilePromise =
344- UpgradeNg1ComponentAdapterBuilder . resolve ( this . downgradedComponents , injector ) ;
338+ provide . decorator ( NG1_TESTABILITY , [
339+ '$delegate' ,
340+ function ( testabilityDelegate : angular . ITestabilityService ) {
341+ var ng2Testability : Testability = injector . get ( Testability ) ;
342+
343+ var origonalWhenStable : Function = testabilityDelegate . whenStable ;
344+ var newWhenStable = ( callback : Function ) : void => {
345+ var whenStableContext : any = this ;
346+ origonalWhenStable . call ( this , function ( ) {
347+ if ( ng2Testability . isStable ( ) ) {
348+ callback . apply ( this , arguments ) ;
349+ } else {
350+ ng2Testability . whenStable ( newWhenStable . bind ( whenStableContext , callback ) ) ;
351+ }
352+ } ) ;
353+ } ;
354+
355+ testabilityDelegate . whenStable = newWhenStable ;
356+ return testabilityDelegate ;
357+ }
358+ ] ) ;
345359 }
346360 ] ) ;
347361
362+ ng1compilePromise = new Promise ( ( resolve , reject ) => {
363+ ng1Module . run ( [
364+ '$injector' ,
365+ '$rootScope' ,
366+ ( injector : angular . IInjectorService , rootScope : angular . IRootScopeService ) => {
367+ ng1Injector = injector ;
368+ ObservableWrapper . subscribe ( ngZone . onMicrotaskEmpty ,
369+ ( _ ) => ngZone . runOutsideAngular ( ( ) => rootScope . $apply ( ) ) ) ;
370+ UpgradeNg1ComponentAdapterBuilder . resolve ( this . downgradedComponents , injector )
371+ . then ( resolve , reject ) ;
372+ }
373+ ] ) ;
374+ } ) ;
375+
376+ // Make sure resumeBootstrap() only exists if the current bootstrap is deferred
377+ var windowAngular = ( < any > global ) . angular ;
378+ windowAngular . resumeBootstrap = undefined ;
379+
348380 angular . element ( element ) . data ( controllerKey ( NG2_INJECTOR ) , injector ) ;
349381 ngZone . run ( ( ) => { angular . bootstrap ( element , [ this . idPrefix ] , config ) ; } ) ;
350- Promise . all ( [ this . compileNg2Components ( compiler , hostViewFactoryRefMap ) , ng1compilePromise ] )
382+ ng1BootstrapPromise = new Promise ( ( resolve , reject ) => {
383+ if ( windowAngular . resumeBootstrap ) {
384+ var originalResumeBootstrap : ( ) => void = windowAngular . resumeBootstrap ;
385+ windowAngular . resumeBootstrap = function ( ) {
386+ windowAngular . resumeBootstrap = originalResumeBootstrap ;
387+ windowAngular . resumeBootstrap . apply ( this , arguments ) ;
388+ resolve ( ) ;
389+ } ;
390+ } else {
391+ resolve ( ) ;
392+ }
393+ } ) ;
394+
395+ Promise . all ( [
396+ this . compileNg2Components ( compiler , hostViewFactoryRefMap ) ,
397+ ng1BootstrapPromise ,
398+ ng1compilePromise
399+ ] )
351400 . then ( ( ) => {
352401 ngZone . run ( ( ) => {
353402 if ( rootScopePrototype ) {
0 commit comments