chore(facade): remove most facade/async functions
This commit is contained in:

committed by
Alex Rickabaugh

parent
6baf3baedd
commit
99989f5d3f
@ -6,65 +6,12 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {Subject} from 'rxjs/Subject';
|
||||
import {PromiseObservable} from 'rxjs/observable/PromiseObservable';
|
||||
import {toPromise} from 'rxjs/operator/toPromise';
|
||||
|
||||
import {global, noop} from './lang';
|
||||
|
||||
export {Observable} from 'rxjs/Observable';
|
||||
export {Subject} from 'rxjs/Subject';
|
||||
export {PromiseCompleter, PromiseWrapper} from './promise';
|
||||
|
||||
export class TimerWrapper {
|
||||
static setTimeout(fn: (...args: any[]) => void, millis: number): number {
|
||||
return global.setTimeout(fn, millis);
|
||||
}
|
||||
static clearTimeout(id: number): void { global.clearTimeout(id); }
|
||||
|
||||
static setInterval(fn: (...args: any[]) => void, millis: number): number {
|
||||
return global.setInterval(fn, millis);
|
||||
}
|
||||
static clearInterval(id: number): void { global.clearInterval(id); }
|
||||
}
|
||||
|
||||
export class ObservableWrapper {
|
||||
// TODO(vsavkin): when we use rxnext, try inferring the generic type from the first arg
|
||||
static subscribe<T>(
|
||||
emitter: any, onNext: (value: T) => void, onError?: (exception: any) => void,
|
||||
onComplete: () => void = () => {}): Object {
|
||||
onError = (typeof onError === 'function') && onError || noop;
|
||||
onComplete = (typeof onComplete === 'function') && onComplete || noop;
|
||||
return emitter.subscribe({next: onNext, error: onError, complete: onComplete});
|
||||
}
|
||||
|
||||
static isObservable(obs: any): boolean { return !!obs.subscribe; }
|
||||
|
||||
/**
|
||||
* Returns whether `obs` has any subscribers listening to events.
|
||||
*/
|
||||
static hasSubscribers(obs: EventEmitter<any>): boolean { return obs.observers.length > 0; }
|
||||
|
||||
static dispose(subscription: any) { subscription.unsubscribe(); }
|
||||
|
||||
/**
|
||||
* @deprecated - use callEmit() instead
|
||||
*/
|
||||
static callNext(emitter: EventEmitter<any>, value: any) { emitter.emit(value); }
|
||||
|
||||
static callEmit(emitter: EventEmitter<any>, value: any) { emitter.emit(value); }
|
||||
|
||||
static callError(emitter: EventEmitter<any>, error: any) { emitter.error(error); }
|
||||
|
||||
static callComplete(emitter: EventEmitter<any>) { emitter.complete(); }
|
||||
|
||||
static fromPromise(promise: Promise<any>): Observable<any> {
|
||||
return PromiseObservable.create(promise);
|
||||
}
|
||||
|
||||
static toPromise(obj: Observable<any>): Promise<any> { return toPromise.call(obj); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Use by directives and components to emit custom Events.
|
||||
|
@ -1,61 +0,0 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
export class PromiseCompleter<R> {
|
||||
promise: Promise<R>;
|
||||
resolve: (value?: R|PromiseLike<R>) => void;
|
||||
reject: (error?: any, stackTrace?: string) => void;
|
||||
|
||||
constructor() {
|
||||
this.promise = new Promise((res, rej) => {
|
||||
this.resolve = res;
|
||||
this.reject = rej;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class PromiseWrapper {
|
||||
static resolve<T>(obj: T): Promise<T> { return Promise.resolve(obj); }
|
||||
|
||||
static reject(obj: any, _: any): Promise<any> { return Promise.reject(obj); }
|
||||
|
||||
// Note: We can't rename this method into `catch`, as this is not a valid
|
||||
// method name in Dart.
|
||||
static catchError<T>(promise: Promise<T>, onError: (error: any) => T | PromiseLike<T>):
|
||||
Promise<T> {
|
||||
return promise.catch(onError);
|
||||
}
|
||||
|
||||
static all<T>(promises: (T|Promise<T>)[]): Promise<T[]> {
|
||||
if (promises.length == 0) return Promise.resolve([]);
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
static then<T, U>(
|
||||
promise: Promise<T>, success: (value: T) => U | PromiseLike<U>,
|
||||
rejection?: (error: any, stack?: any) => U | PromiseLike<U>): Promise<U> {
|
||||
return promise.then(success, rejection);
|
||||
}
|
||||
|
||||
static wrap<T>(computation: () => T): Promise<T> {
|
||||
return new Promise((res, rej) => {
|
||||
try {
|
||||
res(computation());
|
||||
} catch (e) {
|
||||
rej(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static scheduleMicrotask(computation: () => any): void {
|
||||
PromiseWrapper.then(PromiseWrapper.resolve(null), computation, (_) => {});
|
||||
}
|
||||
|
||||
static completer<T>(): PromiseCompleter<T> { return new PromiseCompleter<T>(); }
|
||||
}
|
@ -6,9 +6,9 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {AsyncTestCompleter, describe, it, expect, beforeEach, ddescribe, iit, xit, inject,} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
import {browserDetection} from '@angular/platform-browser/testing/browser_util';
|
||||
import {ObservableWrapper, Observable, Subject, EventEmitter, PromiseWrapper} from '../src/async';
|
||||
import {EventEmitter, Observable, Subject} from '../src/async';
|
||||
|
||||
export function main() {
|
||||
describe('EventEmitter', () => {
|
||||
@ -18,82 +18,88 @@ export function main() {
|
||||
|
||||
it('should call the next callback',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
ObservableWrapper.subscribe(emitter, (value) => {
|
||||
expect(value).toEqual(99);
|
||||
async.done();
|
||||
emitter.subscribe({
|
||||
next: (value: any) => {
|
||||
expect(value).toEqual(99);
|
||||
async.done();
|
||||
}
|
||||
});
|
||||
|
||||
ObservableWrapper.callEmit(emitter, 99);
|
||||
emitter.emit(99);
|
||||
}));
|
||||
|
||||
it('should call the throw callback',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
ObservableWrapper.subscribe(emitter, (_) => {}, (error) => {
|
||||
expect(error).toEqual('Boom');
|
||||
async.done();
|
||||
emitter.subscribe({
|
||||
next: () => {},
|
||||
error: (error: any) => {
|
||||
expect(error).toEqual('Boom');
|
||||
async.done();
|
||||
}
|
||||
});
|
||||
ObservableWrapper.callError(emitter, 'Boom');
|
||||
emitter.error('Boom');
|
||||
}));
|
||||
|
||||
it('should work when no throw callback is provided',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
ObservableWrapper.subscribe(emitter, (_) => {}, (_) => { async.done(); });
|
||||
ObservableWrapper.callError(emitter, 'Boom');
|
||||
emitter.subscribe({next: () => {}, error: (_: any) => { async.done(); }});
|
||||
emitter.error('Boom');
|
||||
}));
|
||||
|
||||
it('should call the return callback',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
ObservableWrapper.subscribe(emitter, (_) => {}, (_) => {}, () => { async.done(); });
|
||||
|
||||
ObservableWrapper.callComplete(emitter);
|
||||
emitter.subscribe(
|
||||
{next: () => {}, error: (_: any) => {}, complete: () => { async.done(); }});
|
||||
emitter.complete();
|
||||
}));
|
||||
|
||||
it('should subscribe to the wrapper synchronously', () => {
|
||||
var called = false;
|
||||
ObservableWrapper.subscribe(emitter, (value) => { called = true; });
|
||||
emitter.subscribe({next: (value: any) => { called = true; }});
|
||||
emitter.emit(99);
|
||||
|
||||
ObservableWrapper.callEmit(emitter, 99);
|
||||
expect(called).toBe(true);
|
||||
});
|
||||
|
||||
it('delivers next and error events synchronously',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
let log: any[] /** TODO #9100 */ = [];
|
||||
ObservableWrapper.subscribe(
|
||||
emitter,
|
||||
(x) => {
|
||||
log.push(x);
|
||||
expect(log).toEqual([1, 2]);
|
||||
},
|
||||
(err) => {
|
||||
log.push(err);
|
||||
expect(log).toEqual([1, 2, 3, 4]);
|
||||
async.done();
|
||||
});
|
||||
|
||||
emitter.subscribe({
|
||||
next: (x: any) => {
|
||||
log.push(x);
|
||||
expect(log).toEqual([1, 2]);
|
||||
},
|
||||
error: (err: any) => {
|
||||
log.push(err);
|
||||
expect(log).toEqual([1, 2, 3, 4]);
|
||||
async.done();
|
||||
}
|
||||
});
|
||||
log.push(1);
|
||||
ObservableWrapper.callEmit(emitter, 2);
|
||||
emitter.emit(2);
|
||||
log.push(3);
|
||||
ObservableWrapper.callError(emitter, 4);
|
||||
emitter.error(4);
|
||||
log.push(5);
|
||||
}));
|
||||
|
||||
it('delivers next and complete events synchronously', () => {
|
||||
let log: any[] /** TODO #9100 */ = [];
|
||||
ObservableWrapper.subscribe(
|
||||
emitter,
|
||||
(x) => {
|
||||
log.push(x);
|
||||
expect(log).toEqual([1, 2]);
|
||||
},
|
||||
null,
|
||||
() => {
|
||||
log.push(4);
|
||||
expect(log).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
emitter.subscribe({
|
||||
next: (x: any) => {
|
||||
log.push(x);
|
||||
expect(log).toEqual([1, 2]);
|
||||
},
|
||||
error: null,
|
||||
complete: () => {
|
||||
log.push(4);
|
||||
expect(log).toEqual([1, 2, 3, 4]);
|
||||
}
|
||||
});
|
||||
log.push(1);
|
||||
ObservableWrapper.callEmit(emitter, 2);
|
||||
emitter.emit(2);
|
||||
log.push(3);
|
||||
ObservableWrapper.callComplete(emitter);
|
||||
emitter.complete();
|
||||
log.push(5);
|
||||
expect(log).toEqual([1, 2, 3, 4, 5]);
|
||||
});
|
||||
@ -102,22 +108,22 @@ export function main() {
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var e = new EventEmitter(true);
|
||||
var log: any[] /** TODO #9100 */ = [];
|
||||
ObservableWrapper.subscribe(e, (x) => {
|
||||
e.subscribe((x: any) => {
|
||||
log.push(x);
|
||||
expect(log).toEqual([1, 3, 2]);
|
||||
async.done();
|
||||
});
|
||||
log.push(1);
|
||||
ObservableWrapper.callEmit(e, 2);
|
||||
e.emit(2);
|
||||
log.push(3);
|
||||
|
||||
}));
|
||||
|
||||
it('reports whether it has subscribers', () => {
|
||||
var e = new EventEmitter(false);
|
||||
expect(ObservableWrapper.hasSubscribers(e)).toBe(false);
|
||||
ObservableWrapper.subscribe(e, (_) => {});
|
||||
expect(ObservableWrapper.hasSubscribers(e)).toBe(true);
|
||||
expect(e.observers.length > 0).toBe(false);
|
||||
e.subscribe({next: () => {}});
|
||||
expect(e.observers.length > 0).toBe(true);
|
||||
});
|
||||
|
||||
// TODO: vsavkin: add tests cases
|
||||
@ -125,71 +131,4 @@ export function main() {
|
||||
// should call dispose on the subscription on throw
|
||||
// should call dispose on the subscription on return
|
||||
});
|
||||
|
||||
describe('ObservableWrapper', () => {
|
||||
|
||||
it('should correctly check isObservable for EventEmitter', () => {
|
||||
var e = new EventEmitter(false);
|
||||
expect(ObservableWrapper.isObservable(e)).toBe(true);
|
||||
});
|
||||
|
||||
it('should correctly check isObservable for Subject', () => {
|
||||
var e = new Subject();
|
||||
expect(ObservableWrapper.isObservable(e)).toBe(true);
|
||||
});
|
||||
|
||||
it('should subscribe to EventEmitters', () => {
|
||||
let e = new EventEmitter(false);
|
||||
|
||||
ObservableWrapper.subscribe(e, (val) => {});
|
||||
|
||||
ObservableWrapper.callEmit(e, 1);
|
||||
ObservableWrapper.callComplete(e);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// See ECMAScript 6 Spec 25.4.4.1
|
||||
describe('PromiseWrapper', () => {
|
||||
describe('#all', () => {
|
||||
it('should combine lists of Promises',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var one = PromiseWrapper.completer();
|
||||
var two = PromiseWrapper.completer();
|
||||
|
||||
var all = PromiseWrapper.all([one.promise, two.promise]);
|
||||
var allCalled = false;
|
||||
|
||||
PromiseWrapper.then(one.promise, (_) => {
|
||||
expect(allCalled).toBe(false);
|
||||
two.resolve('two');
|
||||
return null;
|
||||
});
|
||||
|
||||
PromiseWrapper.then(all, (_) => {
|
||||
allCalled = true;
|
||||
async.done();
|
||||
return null;
|
||||
});
|
||||
|
||||
one.resolve('one');
|
||||
}));
|
||||
|
||||
[null, true, false, 10, 'thing', {}, []].forEach(abruptCompletion => {
|
||||
it(`should treat "${abruptCompletion}" as an "abrupt completion"`,
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var one = PromiseWrapper.completer();
|
||||
|
||||
var all = PromiseWrapper.all([one.promise, abruptCompletion]);
|
||||
|
||||
PromiseWrapper.then(all, (val) => {
|
||||
expect(val[1]).toEqual(abruptCompletion);
|
||||
async.done();
|
||||
});
|
||||
|
||||
one.resolve('one');
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user