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

refactor(bindCallback): readd resultSelector as deprecated #3495

Merged
merged 1 commit into from Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 50 additions & 8 deletions spec/observables/bindCallback-spec.ts
Expand Up @@ -25,11 +25,53 @@ describe('bindCallback', () => {
expect(results).to.deep.equal(['undefined', 'done']);
});

it('should emit one value from a callback', () => {
it('should still support deprecated resultSelector', () => {
function callback(datum: number, cb: Function) {
cb(datum);
}
const boundCallback = bindCallback<number>(callback);

const boundCallback = bindCallback(
callback,
(datum: any) => datum + 1,
);

const results: Array<string|number> = [];

boundCallback(42)
.subscribe({
next(value) { results.push(value); },
complete() { results.push('done'); },
});

expect(results).to.deep.equal([43, 'done']);
});

it('should still support deprecated resultSelector if its void', () => {
function callback(datum: number, cb: Function) {
cb(datum);
}

const boundCallback = bindCallback(
callback,
void 0,
);

const results: Array<string|number> = [];

boundCallback(42)
.subscribe({
next(value) { results.push(value); },
complete() { results.push('done'); },
});

expect(results).to.deep.equal([42, 'done']);
});

it('should emit one value from a callback', () => {
function callback(datum: number, cb: (result: number) => void) {
cb(datum);
}
const boundCallback = bindCallback(callback);
const results: Array<string|number> = [];

boundCallback(42)
Expand Down Expand Up @@ -107,10 +149,10 @@ describe('bindCallback', () => {
});

it('should emit one value from a callback', () => {
function callback(datum: number, cb: Function) {
function callback(datum: number, cb: (result: number) => void) {
cb(datum);
}
const boundCallback = bindCallback<number>(callback, rxTestScheduler);
const boundCallback = bindCallback(callback, rxTestScheduler);
const results: Array<string|number> = [];

boundCallback(42)
Expand Down Expand Up @@ -165,10 +207,10 @@ describe('bindCallback', () => {
});

it('should pass multiple inner arguments as an array', () => {
function callback(datum: number, cb: Function) {
function callback(datum: number, cb: (a: number, b: number, c: number, d: number) => void) {
cb(datum, 1, 2, 3);
}
const boundCallback = bindCallback<number[]>(callback, rxTestScheduler);
const boundCallback = bindCallback(callback, rxTestScheduler);
const results: Array<string|number[]> = [];

boundCallback(42)
Expand All @@ -185,11 +227,11 @@ describe('bindCallback', () => {

it('should cache value for next subscription and not call callbackFunc again', () => {
let calls = 0;
function callback(datum: number, cb: Function) {
function callback(datum: number, cb: (x: number) => void) {
calls++;
cb(datum);
}
const boundCallback = bindCallback<number>(callback, rxTestScheduler);
const boundCallback = bindCallback(callback, rxTestScheduler);
const results1: Array<number|string> = [];
const results2: Array<number|string> = [];

Expand Down
24 changes: 22 additions & 2 deletions src/internal/observable/bindCallback.ts
Expand Up @@ -2,8 +2,14 @@ import { SchedulerLike, SchedulerAction } from '../types';
import { Observable } from '../Observable';
import { AsyncSubject } from '../AsyncSubject';
import { Subscriber } from '../Subscriber';
import { map } from '../operators/map';
import { isArray } from '../util/isArray';
import { isScheduler } from '../util/isScheduler';

// tslint:disable:max-line-length
/** @deprecated resultSelector is no longer supported, use a mapping function. */
export function bindCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;

export function bindCallback<R1, R2, R3, R4>(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): () => Observable<any[]>;
export function bindCallback<R1, R2, R3>(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>;
export function bindCallback<R1, R2>(callbackFunc: (callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>;
Expand Down Expand Up @@ -157,8 +163,22 @@ export function bindCallback(callbackFunc: Function, scheduler?: SchedulerLike):
* Observable that delivers the same values the callback would deliver.
* @name bindCallback
*/
export function bindCallback<T>(callbackFunc: Function,
scheduler?: SchedulerLike): (...args: any[]) => Observable<T> {
export function bindCallback<T>(
callbackFunc: Function,
resultSelector?: Function|SchedulerLike,
scheduler?: SchedulerLike
): (...args: any[]) => Observable<T> {
if (resultSelector) {
if (isScheduler(resultSelector)) {
scheduler = resultSelector;
} else {
// DEPRECATED PATH
return (...args: any[]) => bindCallback(callbackFunc, scheduler)(...args).pipe(
map((args) => isArray(args) ? resultSelector(...args) : resultSelector(args)),
);
}
}

return function (this: any, ...args: any[]): Observable<T> {
const context = this;
let subject: AsyncSubject<T>;
Expand Down
4 changes: 2 additions & 2 deletions src/internal/util/isScheduler.ts
@@ -1,4 +1,4 @@
import { Scheduler } from '../Scheduler';
export function isScheduler(value: any): value is Scheduler {
import { SchedulerLike } from '../types';
export function isScheduler(value: any): value is SchedulerLike {
return value && typeof (<any>value).schedule === 'function';
}