| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 17:12:25 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2018-02-27 17:06:06 -05:00
										 |  |  | import {Observable, Observer} from 'rxjs'; | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 03:23:29 +02:00
										 |  |  | // #docregion AsyncPipePromise
 | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  |   selector: 'async-promise-pipe', | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  |   template: `<div>
 | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  |     <code>promise|async</code>:  | 
					
						
							| 
									
										
										
										
											2015-12-10 17:28:29 +01:00
										 |  |  |     <button (click)="clicked()">{{ arrived ? 'Reset' : 'Resolve' }}</button> | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  |     <span>Wait for it... {{ greeting | async }}</span> | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  |   </div>`
 | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  | export class AsyncPromisePipeComponent { | 
					
						
							| 
									
										
										
										
											2017-03-24 09:57:05 -07:00
										 |  |  |   greeting: Promise<string>|null = null; | 
					
						
							| 
									
										
										
										
											2015-11-19 15:17:35 -08:00
										 |  |  |   arrived: boolean = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 09:57:05 -07:00
										 |  |  |   private resolve: Function|null = null; | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor() { this.reset(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reset() { | 
					
						
							| 
									
										
										
										
											2015-11-19 15:17:35 -08:00
										 |  |  |     this.arrived = false; | 
					
						
							|  |  |  |     this.greeting = new Promise<string>((resolve, reject) => { this.resolve = resolve; }); | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   clicked() { | 
					
						
							| 
									
										
										
										
											2015-11-19 15:17:35 -08:00
										 |  |  |     if (this.arrived) { | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  |       this.reset(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-03-24 09:57:05 -07:00
										 |  |  |       this.resolve !('hi there!'); | 
					
						
							| 
									
										
										
										
											2015-11-19 15:17:35 -08:00
										 |  |  |       this.arrived = true; | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion AsyncPipeObservable
 | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'async-observable-pipe', | 
					
						
							|  |  |  |   template: '<div><code>observable|async</code>: Time: {{ time | async }}</div>' | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | export class AsyncObservablePipeComponent { | 
					
						
							| 
									
										
										
										
											2018-02-27 17:06:06 -05:00
										 |  |  |   time = new Observable<string>((observer: Observer<string>) => { | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  |     setInterval(() => observer.next(new Date().toString()), 1000); | 
					
						
							| 
									
										
										
										
											2016-02-11 17:01:17 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-11-02 15:46:59 -08:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // For some reason protractor hangs on setInterval. So we will run outside of angular zone so that
 | 
					
						
							|  |  |  | // protractor will not see us. Also we want to have this outside the docregion so as not to confuse
 | 
					
						
							|  |  |  | // the reader.
 | 
					
						
							|  |  |  | function setInterval(fn: Function, delay: number) { | 
					
						
							| 
									
										
										
										
											2019-01-28 21:59:25 +01:00
										 |  |  |   const zone = (window as any)['Zone'].current; | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   let rootZone = zone; | 
					
						
							| 
									
										
										
										
											2016-09-08 21:41:09 -07:00
										 |  |  |   while (rootZone.parent) { | 
					
						
							|  |  |  |     rootZone = rootZone.parent; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   rootZone.run( | 
					
						
							|  |  |  |       () => { window.setInterval(function() { zone.run(fn, this, arguments as any); }, delay); }); | 
					
						
							|  |  |  | } |