Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typings for combineLatest #871

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
},
"scripts": {
"build_all": "npm run build_es6 && npm run build_amd && npm run build_cjs && npm run build_global && npm run generate_packages",
"build_amd": "rm -rf dist/amd && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts -m amd --outDir dist/amd --sourcemap --target ES5 --diagnostics --pretty",
"build_cjs": "rm -rf dist/cjs && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts src/Rx.KitchenSink.ts -m commonjs --outDir dist/cjs --sourcemap --target ES5 -d --diagnostics --pretty",
"build_es6": "rm -rf dist/es6 && tsc src/Rx.ts src/Rx.KitchenSink.ts --outDir dist/es6 --sourceMap --target ES6 -d --diagnostics --pretty",
"build_amd": "npm run build_operators && rm -rf dist/amd && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts -m amd --outDir dist/amd --sourcemap --target ES5 --diagnostics --pretty",
"build_cjs": "npm run build_operators && rm -rf dist/cjs && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts src/Rx.KitchenSink.ts -m commonjs --outDir dist/cjs --sourcemap --target ES5 -d --diagnostics --pretty",
"build_es6": "npm run build_operators && rm -rf dist/es6 && tsc src/Rx.ts src/Rx.KitchenSink.ts --outDir dist/es6 --sourceMap --target ES6 -d --diagnostics --pretty",
"build_closure": "java -jar ./node_modules/google-closure-compiler/compiler.jar ./dist/global/Rx.js --language_in ECMASCRIPT5 --create_source_map ./dist/global/Rx.min.js.map --js_output_file ./dist/global/Rx.min.js",
"build_global": "rm -rf dist/global && mkdir \"dist/global\" && browserify src/Rx.global.js --outfile dist/global/Rx.js && npm run build_closure",
"build_global": "npm run build_operators && rm -rf dist/global && mkdir \"dist/global\" && browserify src/Rx.global.js --outfile dist/global/Rx.js && npm run build_closure",
"build_perf": "npm run build_cjs && npm run build_global && webdriver-manager update && npm run perf",
"build_test": "rm -rf dist/ && npm run lint && npm run build_cjs && jasmine",
"build_cover": "rm -rf dist/ && npm run lint && npm run build_cjs && npm run cover",
Expand All @@ -37,7 +37,8 @@
"generate_packages": "node .make-packages.js",
"generate_operator_patches": "tsc -outDir dist/ ./tools/generate-operator-patches.ts && node ./dist/generate-operator-patches.js --exec",
"commit": "git-cz",
"check_circular_dependencies": "madge ./dist/cjs --circular"
"check_circular_dependencies": "madge ./dist/cjs --circular",
"build_operators": "node typingsgen.js"
},
"repository": {
"type": "git",
Expand Down
5 changes: 4 additions & 1 deletion src/CoreOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {ConnectableObservable} from './observable/ConnectableObservable';
import {Subject} from './Subject';
import {GroupedObservable} from './operator/groupBy-support';
import {Notification} from './Notification';
/* tslint:disable */
import * as operator from './operator-typings';
/* tslint:enable */

export interface CoreOperators<T> {
buffer?: (closingNotifier: Observable<any>) => Observable<T[]>;
Expand All @@ -13,7 +16,7 @@ export interface CoreOperators<T> {
bufferWhen?: (closingSelector: () => Observable<any>) => Observable<T[]>;
catch?: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
combineAll?: <R>(project?: (...values: Array<any>) => R) => Observable<R>;
combineLatest?: <R>(...observables: Array<Observable<any> | ((...values: Array<any>) => R)>) => Observable<R>;
combineLatest: operator.operator_proto_combineLatest<T>;
concat?: <R>(...observables: (Observable<any> | Scheduler)[]) => Observable<R>;
concatAll?: () => Observable<T>;
concatMap?: <R>(project: ((x: T, ix: number) => Observable<any>), projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable<R>;
Expand Down
76 changes: 44 additions & 32 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ import {Subject} from './Subject';
import {Notification} from './Notification';
import {rxSubscriber} from'./symbol/rxSubscriber';

/* tslint:disable */
import * as operator from './operator-typings';
/* tslint:enable */
import {combineLatest as combineLatestStatic} from './operator/combineLatest-static';
import {concat as concatStatic} from './operator/concat-static';
import {merge as mergeStatic} from './operator/merge-static';
import {zip as zipStatic} from './operator/zip-static';
import {DeferObservable} from './observable/defer';
import {EmptyObservable} from './observable/empty';
import {ForkJoinObservable} from './observable/forkJoin';
import {FromObservable} from './observable/from';
import {CallbackObservable} from './observable/fromCallback';
import {ArrayObservable} from './observable/fromArray';
import {FromEventObservable} from './observable/fromEvent';
import {FromEventPatternObservable} from './observable/fromEventPattern';
import {PromiseObservable} from './observable/fromPromise';
import {IntervalObservable} from './observable/interval';
import {TimerObservable} from './observable/timer';
import {RangeObservable} from './observable/range';
import {InfiniteObservable} from './observable/never';
import {ErrorObservable} from './observable/throw';

/**
* A representation of any set of values over any amount of time. This the most basic building block
* of RxJS.
Expand Down Expand Up @@ -156,33 +178,25 @@ export class Observable<T> implements CoreOperators<T> {
}

// static method stubs
static bindCallback: <T>(callbackFunc: Function, selector?: Function, scheduler?: Scheduler) => Function;
static combineLatest: <T>(...observables: Array<Observable<any> |
Array<Observable<any>> |
((...values: Array<any>) => T) |
Scheduler>) => Observable<T>;
static concat: <T>(...observables: Array<Observable<any> | Scheduler>) => Observable<T>;
static defer: <T>(observableFactory: () => Observable<T>) => Observable<T>;
static empty: <T>(scheduler?: Scheduler) => Observable<T>;
static forkJoin: (...sources: Array<Observable<any> |
Array<Observable<any>> |
Promise<any> |
((...values: Array<any>) => any)>) => Observable<any>;
static from: <T>(iterable: any, scheduler?: Scheduler) => Observable<T>;
static fromArray: <T>(array: T[], scheduler?: Scheduler) => Observable<T>;
static fromEvent: <T>(element: any, eventName: string, selector?: (...args: Array<any>) => T) => Observable<T>;
static fromEventPattern: <T>(addHandler: (handler: Function) => void,
removeHandler: (handler: Function) => void,
selector?: (...args: Array<any>) => T) => Observable<T>;
static fromPromise: <T>(promise: Promise<T>, scheduler?: Scheduler) => Observable<T>;
static interval: (interval: number, scheduler?: Scheduler) => Observable<number>;
static merge: <T>(...observables: Array<Observable<any> | Scheduler | number>) => Observable<T>;
static never: <T>() => Observable<T>;
static of: <T>(...values: Array<T | Scheduler>) => Observable<T>;
static range: (start: number, end: number, scheduler?: Scheduler) => Observable<number>;
static throw: <T>(error: T) => Observable<T>;
static timer: (dueTime?: number | Date, period?: number | Scheduler, scheduler?: Scheduler) => Observable<number>;
static zip: <T>(...observables: Array<Observable<any> | ((...values: Array<any>) => T)>) => Observable<T>;
static bindCallback: typeof CallbackObservable.create;
static combineLatest: typeof combineLatestStatic;
static concat: typeof concatStatic;
static defer: typeof DeferObservable.create;
static empty: typeof EmptyObservable.create;
static forkJoin: typeof ForkJoinObservable.create;
static from: typeof FromObservable.create;
static fromArray: typeof ArrayObservable.create;
static fromEvent: typeof FromEventObservable.create;
static fromEventPattern: typeof FromEventPatternObservable.create;
static fromPromise: typeof PromiseObservable.create;
static interval: typeof IntervalObservable.create;
static merge: typeof mergeStatic;
static never: typeof InfiniteObservable.create;
static of: typeof ArrayObservable.of;
static range: typeof RangeObservable.create;
static throw: typeof ErrorObservable.create;
static timer: typeof TimerObservable.create;
static zip: typeof zipStatic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the typeof trick is pretty cool. I didn't know that one. I'm a little concerned about the implications of importing all of the other Observable types in this file, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the imports are just use for types, they dissolve when TypeScript is emitting the results.

For example the cjs output for Observable.ts starts with

var Subscriber_1 = require('./Subscriber');
var root_1 = require('./util/root');
var SymbolShim_1 = require('./util/SymbolShim');
/**
 * A representation of any set of values over any amount of time. This the most basic building block
 * of RxJS.
 *
 * @class Observable<T>
 */
var Observable = (function () {


// core operators
buffer: (closingNotifier: Observable<any>) => Observable<T[]>;
Expand All @@ -192,9 +206,7 @@ export class Observable<T> implements CoreOperators<T> {
bufferWhen: (closingSelector: () => Observable<any>) => Observable<T[]>;
catch: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
combineAll: <R>(project?: (...values: Array<any>) => R) => Observable<R>;
combineLatest: <R>(...observables: Array<Observable<any> |
Array<Observable<any>> |
((...values: Array<any>) => R)>) => Observable<R>;
combineLatest: operator.operator_proto_combineLatest<T>;
concat: <R>(...observables: (Observable<any> | Scheduler)[]) => Observable<R>;
concatAll: () => Observable<any>;
concatMap: <R>(project: ((x: T, ix: number) => Observable<any>), projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable<R>;
Expand Down Expand Up @@ -255,9 +267,9 @@ export class Observable<T> implements CoreOperators<T> {
startWith: (x: T) => Observable<T>;
subscribeOn: (scheduler: Scheduler, delay?: number) => Observable<T>;
switch: <R>() => Observable<R>;
exhaust: <T>() => Observable<T>;
switchFirst: <T>() => Observable<T>;
switchMap: <R>(project: ((x: T, ix: number) => Observable<any>), projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable<R>;
exhaustMap: <T, R, R2>(project: (x: T, ix: number) => Observable<R>, rSelector?: (x: T, y: R, ix: number, iy: number) => R2) => Observable<R>;
switchFirstMap: <T, R, R2>(project: (x: T, ix: number) => Observable<R>, rSelector?: (x: T, y: R, ix: number, iy: number) => R2) => Observable<R>;
switchMapTo: <R>(observable: Observable<any>, projectResult?: (x: T, y: any, ix: number, iy: number) => R) => Observable<R>;
take: (count: number) => Observable<T>;
takeUntil: (notifier: Observable<any>) => Observable<T>;
Expand Down
41 changes: 41 additions & 0 deletions src/operator-typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* tslint:disable:class-name */ /* tslint:disable:no-unused-variable */ /* tslint:disable:max-line-length */
import {Observable} from './Observable';
import {ConnectableObservable} from './observable/ConnectableObservable';
import {Scheduler} from './Scheduler';
import {Notification} from './Notification';
import {Subject} from './Subject';
import {Observer} from './Observer';
import {GroupedObservable} from './operator/groupBy-support';
import {GroupByObservable} from './operator/groupBy';
import {TimeInterval} from './operator/extended/timeInterval';
import {ObservableInput, ObservableOrPromise, ArrayOrIterator, _Selector, _IndexSelector, _SwitchMapResultSelector, _ObservableMergeMapProjector, _IteratorMergeMapProjector, _Predicate, _PredicateObservable, _Comparer, _Accumulator, _MergeAccumulator} from './types';

/* ||| MARKER ||| */
export interface operator_proto_combineLatest<T> {
<TResult>(project: (v1: T) => TResult): Observable<TResult>;
<TResult>(project: (v1: T) => TResult): Observable<TResult>;
<T2>(v2: ObservableInput<T2>): Observable<[T, T2]>;
<T2, T3>(v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<[T, T2, T3]>;
<T2, T3, T4>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<[T, T2, T3, T4]>;
<T2, T3, T4, T5>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<[T, T2, T3, T4, T5]>;
<T2, T3, T4, T5, T6>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<[T, T2, T3, T4, T5, T6]>;
<T2>(array: [ObservableInput<T2>]): Observable<[T, T2]>;
<T2, T3>(array: [ObservableInput<T2>, ObservableInput<T3>]): Observable<[T, T2, T3]>;
<T2, T3, T4>(array: [ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>]): Observable<[T, T2, T3, T4]>;
<T2, T3, T4, T5>(array: [ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>]): Observable<[T, T2, T3, T4, T5]>;
<T2, T3, T4, T5, T6>(array: [ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>, ObservableInput<T6>]): Observable<[T, T2, T3, T4, T5, T6]>;
<T2, TResult>(v2: ObservableInput<T2>, project: (v1: T, v2: T2) => TResult): Observable<TResult>;
<T2, T3, TResult>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, project: (v1: T, v2: T2, v3: T3) => TResult): Observable<TResult>;
<T2, T3, T4, TResult>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, project: (v1: T, v2: T2, v3: T3, v4: T4) => TResult): Observable<TResult>;
<T2, T3, T4, T5, TResult>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => TResult): Observable<TResult>;
<T2, T3, T4, T5, T6, TResult>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => TResult): Observable<TResult>;
<T2, TResult>(array: [ObservableInput<T2>], project: (v1: T, v2: T2) => TResult): Observable<TResult>;
<T2, T3, TResult>(array: [ObservableInput<T2>, ObservableInput<T3>], project: (v1: T, v2: T2, v3: T3) => TResult): Observable<TResult>;
<T2, T3, T4, TResult>(array: [ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>], project: (v1: T, v2: T2, v3: T3, v4: T4) => TResult): Observable<TResult>;
<T2, T3, T4, T5, TResult>(array: [ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>], project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => TResult): Observable<TResult>;
<T2, T3, T4, T5, T6, TResult>(array: [ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>, ObservableInput<T6>], project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => TResult): Observable<TResult>;
<TResult>(array: ObservableInput<any>[], project?: Function): Observable<TResult[]>;
(...observables: Array<ObservableInput<T>>): Observable<T[]>;
<R>(...observables: Array<ObservableInput<T> | ((...values: Array<T>) => R)>): Observable<R>;
}
/* ||| MARKER ||| */
18 changes: 15 additions & 3 deletions src/operator/combineLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Observable} from '../Observable';
import {ArrayObservable} from '../observable/fromArray';
import {CombineLatestOperator} from './combineLatest-support';
import {isArray} from '../util/isArray';
import {ObservableInput} from '../types';

/**
* Combines the values from this observable with values from observables passed as arguments. This is done by subscribing
Expand All @@ -13,18 +14,29 @@ import {isArray} from '../util/isArray';
* @returns {Observable} an observable of other projected values from the most recent values from each observable, or an array of each of
* the most recent values from each observable.
*/
export function combineLatest<R>(...observables: Array<Observable<any> |
export function combineLatest<T, TResult>(project: (v1: T) => TResult): Observable<TResult>;
export function combineLatest<T, TResult>(project: (v1: T) => TResult): Observable<TResult>;
/*-- *compute 2-6* export function combineLatest<T, {|X|}>({|v|: ObservableInput<|X|>}): Observable<[T, {|X|}]>; --*/
/*-- *compute 2-6* export function combineLatest<T, {|X|}>(array: [{ObservableInput<|X|>}]): Observable<[T, {|X|}]>; --*/
/*-- *compute 2-6* export function combineLatest<T, {|X|}, TResult>({|v|: ObservableInput<|X|>},
project: (v1: T, {|v|: |X|}) => TResult): Observable<TResult>; --*/
/*-- *compute 2-6* export function combineLatest<T, {|X|}, TResult>(array: [{ObservableInput<|X|>}],
project: (v1: T, {|v|: |X|}) => TResult): Observable<TResult>; --*/
export function combineLatest<T, TResult>(array: ObservableInput<any>[], project?: Function): Observable<TResult[]>;
export function combineLatest<T>(...observables: Array<ObservableInput<T>>): Observable<T[]>;
export function combineLatest<T, R>(...observables: Array<ObservableInput<T> | ((...values: Array<T>) => R)>): Observable<R>;
export function combineLatest<R>(...observables: Array<any | Observable<any> |
Array<Observable<any>> |
((...values: Array<any>) => R)>): Observable<R> {
let project: (...values: Array<any>) => R = null;
let project: (...values: Array<any>) => R = null;
if (typeof observables[observables.length - 1] === 'function') {
project = <(...values: Array<any>) => R>observables.pop();
}

// if the first and only other argument besides the resultSelector is an array
// assume it's been called with `combineLatest([obs1, obs2, obs3], project)`
if (observables.length === 1 && isArray(observables[0])) {
observables = <Array<Observable<any>>>observables[0];
observables = <any>observables[0];
}

observables.unshift(this);
Expand Down
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Observable} from './Observable';
export type ObservableOrPromise<T> = Observable<T> | Promise<T>;
export type ArrayOrIterator<T> = Iterator<T> | ArrayLike<T> | Array<T>;
export type ObservableInput<T> = Observable<T> | Promise<T> | Iterator<T> | ArrayLike<T>;

export type _Selector<T, TResult> = (value: T) => TResult;
export type _IndexSelector<T, TResult> = (value: T, index: number) => TResult;
export type _SwitchMapResultSelector<T1, T2, TResult> = (outerValue: T1, innerValue: T2, outerIndex: number, innerIndex: number) => TResult;
export type _ObservableMergeMapProjector<T, R> = (value: T, index: number) => ObservableOrPromise<R>;
export type _IteratorMergeMapProjector<T, R> = (value: T, index: number) => ArrayOrIterator<R>;

export type _Predicate<T> = _Selector<T, boolean>;
export type _PredicateObservable<T> = (value: T, index: number, observable: Observable<T>) => boolean;

export type _Comparer<T, TResult> = (value1: T, value2: T) => TResult;
export type _Accumulator<T, TAcc> = (acc: TAcc, value: T) => TAcc;
export type _MergeAccumulator<T, TAcc> = (acc: TAcc, value: T) => Observable<TAcc>;
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"indentSize": 2,
"tabSize": 2
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

"files": [
"src/Rx.ts",
"src/Rx.KitchenSink.ts"
]
"files": [
"typings/es6-build-shim.d.ts",
"src/Rx.ts",
"src/Rx.KitchenSink.ts"
]
}
1 change: 1 addition & 0 deletions typings/es6-build-shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare type IterableShim<T> = Iterable<T>;
Loading