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
|
|
|
|
*/
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Subject, Subscription} from 'rxjs';
|
2015-11-30 20:22:52 -05:00
|
|
|
|
2015-04-01 13:45:56 -04:00
|
|
|
/**
|
2015-09-30 23:59:23 -04:00
|
|
|
* Use by directives and components to emit custom Events.
|
2015-09-03 19:17:23 -04:00
|
|
|
*
|
2018-05-18 11:13:00 -04:00
|
|
|
* @usageNotes
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Examples
|
2015-09-03 19:17:23 -04:00
|
|
|
*
|
|
|
|
* In the following example, `Zippy` alternatively emits `open` and `close` events when its
|
|
|
|
* title gets clicked:
|
|
|
|
*
|
|
|
|
* ```
|
2015-10-11 10:41:19 -04:00
|
|
|
* @Component({
|
|
|
|
* selector: 'zippy',
|
|
|
|
* template: `
|
2015-09-03 19:17:23 -04:00
|
|
|
* <div class="zippy">
|
|
|
|
* <div (click)="toggle()">Toggle</div>
|
|
|
|
* <div [hidden]="!visible">
|
|
|
|
* <ng-content></ng-content>
|
|
|
|
* </div>
|
|
|
|
* </div>`})
|
|
|
|
* export class Zippy {
|
|
|
|
* visible: boolean = true;
|
2015-11-19 17:10:50 -05:00
|
|
|
* @Output() open: EventEmitter<any> = new EventEmitter();
|
|
|
|
* @Output() close: EventEmitter<any> = new EventEmitter();
|
2015-09-03 19:17:23 -04:00
|
|
|
*
|
|
|
|
* toggle() {
|
|
|
|
* this.visible = !this.visible;
|
|
|
|
* if (this.visible) {
|
2015-11-16 02:58:59 -05:00
|
|
|
* this.open.emit(null);
|
2015-09-03 19:17:23 -04:00
|
|
|
* } else {
|
2015-11-16 02:58:59 -05:00
|
|
|
* this.close.emit(null);
|
2015-09-03 19:17:23 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2016-06-08 19:38:52 -04:00
|
|
|
* The events payload can be accessed by the parameter `$event` on the components output event
|
|
|
|
* handler:
|
2016-03-02 15:34:45 -05:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <zippy (open)="onOpen($event)" (close)="onClose($event)"></zippy>
|
|
|
|
* ```
|
|
|
|
*
|
2018-05-18 11:13:00 -04:00
|
|
|
* ### Notes
|
|
|
|
*
|
2016-03-02 15:34:45 -05:00
|
|
|
* Uses Rx.Observable but provides an adapter to make it work as specified here:
|
2015-04-01 13:45:56 -04:00
|
|
|
* https://github.com/jhusain/observable-spec
|
|
|
|
*
|
|
|
|
* Once a reference implementation of the spec is available, switch to it.
|
2018-04-05 17:31:44 -04:00
|
|
|
*
|
2015-04-01 13:45:56 -04:00
|
|
|
*/
|
2015-10-24 21:48:43 -04:00
|
|
|
export class EventEmitter<T> extends Subject<T> {
|
2016-05-19 18:29:50 -04:00
|
|
|
// TODO: mark this as internal once all the facades are gone
|
|
|
|
// we can't mark it as internal now because EventEmitter exported via @angular/core would not
|
|
|
|
// contain this property making it incompatible with all the code that uses EventEmitter via
|
|
|
|
// facades, which are local to the code and do not have this property stripped.
|
2016-05-27 12:16:46 -04:00
|
|
|
// tslint:disable-next-line
|
2016-05-19 18:29:50 -04:00
|
|
|
__isAsync: boolean;
|
2015-10-19 17:41:15 -04:00
|
|
|
|
|
|
|
/**
|
2017-05-08 13:47:53 -04:00
|
|
|
* Creates an instance of {@link EventEmitter}, which depending on `isAsync`,
|
2015-10-19 17:41:15 -04:00
|
|
|
* delivers events synchronously or asynchronously.
|
2017-05-08 13:47:53 -04:00
|
|
|
*
|
|
|
|
* @param isAsync By default, events are delivered synchronously (default value: `false`).
|
|
|
|
* Set to `true` for asynchronous event delivery.
|
2015-10-19 17:41:15 -04:00
|
|
|
*/
|
2016-05-26 12:34:04 -04:00
|
|
|
constructor(isAsync: boolean = false) {
|
2015-10-19 17:41:15 -04:00
|
|
|
super();
|
2016-05-19 18:29:50 -04:00
|
|
|
this.__isAsync = isAsync;
|
2015-10-19 17:41:15 -04:00
|
|
|
}
|
2015-09-11 18:38:09 -04:00
|
|
|
|
2016-07-22 13:19:57 -04:00
|
|
|
emit(value?: T) { super.next(value); }
|
2015-11-16 02:58:59 -05:00
|
|
|
|
2015-10-24 21:48:43 -04:00
|
|
|
subscribe(generatorOrNext?: any, error?: any, complete?: any): any {
|
2016-10-07 20:36:08 -04:00
|
|
|
let schedulerFn: (t: any) => any;
|
|
|
|
let errorFn = (err: any): any => null;
|
|
|
|
let completeFn = (): any => null;
|
2015-10-29 22:57:28 -04:00
|
|
|
|
2015-10-24 21:48:43 -04:00
|
|
|
if (generatorOrNext && typeof generatorOrNext === 'object') {
|
2016-10-07 20:36:08 -04:00
|
|
|
schedulerFn = this.__isAsync ? (value: any) => {
|
2016-06-08 19:38:52 -04:00
|
|
|
setTimeout(() => generatorOrNext.next(value));
|
2016-10-07 20:36:08 -04:00
|
|
|
} : (value: any) => { generatorOrNext.next(value); };
|
2015-10-29 22:57:28 -04:00
|
|
|
|
|
|
|
if (generatorOrNext.error) {
|
2016-05-19 18:29:50 -04:00
|
|
|
errorFn = this.__isAsync ? (err) => { setTimeout(() => generatorOrNext.error(err)); } :
|
2016-06-08 19:38:52 -04:00
|
|
|
(err) => { generatorOrNext.error(err); };
|
2015-10-29 22:57:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (generatorOrNext.complete) {
|
2016-05-19 18:29:50 -04:00
|
|
|
completeFn = this.__isAsync ? () => { setTimeout(() => generatorOrNext.complete()); } :
|
2016-06-08 19:38:52 -04:00
|
|
|
() => { generatorOrNext.complete(); };
|
2015-10-29 22:57:28 -04:00
|
|
|
}
|
2015-10-24 21:48:43 -04:00
|
|
|
} else {
|
2016-10-07 20:36:08 -04:00
|
|
|
schedulerFn = this.__isAsync ? (value: any) => { setTimeout(() => generatorOrNext(value)); } :
|
|
|
|
(value: any) => { generatorOrNext(value); };
|
2015-10-29 22:57:28 -04:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
errorFn =
|
2016-05-19 18:29:50 -04:00
|
|
|
this.__isAsync ? (err) => { setTimeout(() => error(err)); } : (err) => { error(err); };
|
2015-10-29 22:57:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (complete) {
|
|
|
|
completeFn =
|
2016-05-19 18:29:50 -04:00
|
|
|
this.__isAsync ? () => { setTimeout(() => complete()); } : () => { complete(); };
|
2015-10-29 22:57:28 -04:00
|
|
|
}
|
2015-10-24 21:48:43 -04:00
|
|
|
}
|
2015-10-29 22:57:28 -04:00
|
|
|
|
2018-02-04 11:58:02 -05:00
|
|
|
const sink = super.subscribe(schedulerFn, errorFn, completeFn);
|
|
|
|
|
|
|
|
if (generatorOrNext instanceof Subscription) {
|
|
|
|
generatorOrNext.add(sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sink;
|
2015-04-14 17:34:41 -04:00
|
|
|
}
|
2016-02-01 13:29:42 -05:00
|
|
|
}
|