2016-06-23 12:47:54 -04: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-08-16 14:15:01 -04:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
2016-08-22 20:17:23 -04:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Subscriber} from 'rxjs/Subscriber';
|
2015-11-02 18:46:59 -05:00
|
|
|
|
2016-05-31 21:23:29 -04:00
|
|
|
// #docregion AsyncPipePromise
|
2015-11-02 18:46:59 -05:00
|
|
|
@Component({
|
2016-09-09 00:41:09 -04:00
|
|
|
selector: 'async-promise-pipe',
|
2015-11-02 18:46:59 -05:00
|
|
|
template: `<div>
|
2016-09-09 00:41:09 -04:00
|
|
|
<code>promise|async</code>:
|
2015-12-10 11:28:29 -05:00
|
|
|
<button (click)="clicked()">{{ arrived ? 'Reset' : 'Resolve' }}</button>
|
2016-09-09 00:41:09 -04:00
|
|
|
<span>Wait for it... {{ greeting | async }}</span>
|
2015-11-02 18:46:59 -05:00
|
|
|
</div>`
|
|
|
|
})
|
2016-09-09 00:41:09 -04:00
|
|
|
export class AsyncPromisePipeComponent {
|
2015-11-19 18:17:35 -05:00
|
|
|
greeting: Promise<string> = null;
|
|
|
|
arrived: boolean = false;
|
|
|
|
|
|
|
|
private resolve: Function = null;
|
2015-11-02 18:46:59 -05:00
|
|
|
|
|
|
|
constructor() { this.reset(); }
|
|
|
|
|
|
|
|
reset() {
|
2015-11-19 18:17:35 -05:00
|
|
|
this.arrived = false;
|
|
|
|
this.greeting = new Promise<string>((resolve, reject) => { this.resolve = resolve; });
|
2015-11-02 18:46:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clicked() {
|
2015-11-19 18:17:35 -05:00
|
|
|
if (this.arrived) {
|
2015-11-02 18:46:59 -05:00
|
|
|
this.reset();
|
|
|
|
} else {
|
2016-06-08 19:38:52 -04:00
|
|
|
this.resolve('hi there!');
|
2015-11-19 18:17:35 -05:00
|
|
|
this.arrived = true;
|
2015-11-02 18:46:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion AsyncPipeObservable
|
2016-09-09 00:41:09 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'async-observable-pipe',
|
|
|
|
template: '<div><code>observable|async</code>: Time: {{ time | async }}</div>'
|
|
|
|
})
|
|
|
|
export class AsyncObservablePipeComponent {
|
|
|
|
time = new Observable<string>((observer: Subscriber<string>) => {
|
|
|
|
setInterval(() => observer.next(new Date().toString()), 1000);
|
2016-02-11 20:01:17 -05:00
|
|
|
});
|
2015-11-02 18:46:59 -05:00
|
|
|
}
|
|
|
|
// #enddocregion
|
2016-09-09 00:41:09 -04: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) {
|
|
|
|
var zone = Zone.current;
|
|
|
|
var rootZone = zone;
|
|
|
|
while (rootZone.parent) {
|
|
|
|
rootZone = rootZone.parent;
|
|
|
|
}
|
|
|
|
rootZone.run(
|
|
|
|
() => { window.setInterval(function() { zone.run(fn, this, arguments as any); }, delay); });
|
|
|
|
}
|