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-08 16:31:41 -04:00
|
|
|
selector: 'async-promise',
|
2015-11-02 18:46:59 -05:00
|
|
|
template: `<div>
|
2015-11-19 18:17:35 -05:00
|
|
|
<p>Wait for it... {{ greeting | async }}</p>
|
2015-12-10 11:28:29 -05:00
|
|
|
<button (click)="clicked()">{{ arrived ? 'Reset' : 'Resolve' }}</button>
|
2015-11-02 18:46:59 -05:00
|
|
|
</div>`
|
|
|
|
})
|
2016-09-08 16:31:41 -04:00
|
|
|
export class AsyncPromiseComponent {
|
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-08 16:31:41 -04:00
|
|
|
@Component({selector: 'async-observable', template: '<div>Time: {{ time | async }}</div>'})
|
|
|
|
export class AsyncObservableComponent {
|
2016-02-11 20:01:17 -05:00
|
|
|
time = new Observable<number>((observer: Subscriber<number>) => {
|
2016-03-23 20:34:15 -04:00
|
|
|
setInterval(() => observer.next(new Date().getTime()), 500);
|
2016-02-11 20:01:17 -05:00
|
|
|
});
|
2015-11-02 18:46:59 -05:00
|
|
|
}
|
|
|
|
// #enddocregion
|