Skip to content

Commit

Permalink
fix(switchScan): fix typings for union types, remove duplicate dts test
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsik committed Apr 8, 2020
1 parent 1246686 commit 9ea12f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions spec-dtslint/operators/switchScan-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ it('should infer correctly when using a single type', () => {
const o = of(1, 2, 3).pipe(switchScan((acc, v) => of(acc + v), 0)); // $ExpectType Observable<number>
});

it('should infer correctly when using a seed', () => {
const o = of(1, 2, 3).pipe(switchScan((acc, v) => of(acc + v), 0)); // $ExpectType Observable<number>
});

it('should infer correctly when using seed of a different type', () => {
const o = of(1, 2, 3).pipe(switchScan((acc: string, v: number) => of(acc + v), '0')); // $ExpectType Observable<string>
const o = of(1, 2, 3).pipe(switchScan((acc, v) => of(acc + v), '0')); // $ExpectType Observable<string>
});

it('should support a projector that takes an index', () => {
const o = of(1, 2, 3).pipe(switchScan((acc: boolean, v: number, index: number) => of(Boolean(v)), false)); // $ExpectType Observable<boolean>
const o = of(1, 2, 3).pipe(switchScan((acc, v, index) => of(Boolean(v)), false)); // $ExpectType Observable<boolean>
});

it('should support projecting to union types', () => {
const o = of(Math.random()).pipe(switchScan(n => n > 0.5 ? of(123) : of('test'), 0)); // $ExpectType Observable<string | number>
});

it('should use the inferred accumulator return type over the seed type', () => {
const o = of(1, 2, 3).pipe(switchScan(p => of(1), [])); // $ExpectType Observable<number>
});

it('should enforce types', () => {
Expand All @@ -29,10 +33,6 @@ it('should enforce the return type to be Observable', () => {
const o = of(1, 2, 3).pipe(switchScan(p => p)); // $ExpectError
});

it('should enforce seed and the return type from accumulator', () => {
const o = of(1, 2, 3).pipe(switchScan(p => of(1), [])); // $ExpectError
});

it('should enforce seed and accumulator to have the same type', () => {
const o = of(1, 2, 3).pipe(switchScan((acc, p) => of([...acc, p]))); // $ExpectError
});
6 changes: 3 additions & 3 deletions spec/operators/switchScan-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ declare function asDiagram(arg: string): Function;
/** @test {switchScan} */
describe('switchScan', () => {
asDiagram('switchScan(i => 10*i\u2014\u201410*i\u2014\u201410*i\u2014|, 0)')
('should map-and-flatten each item to an Observable while passing the seed', () => {
('should map-and-flatten each item to an Observable while passing the accumulated value', () => {
const e1 = hot('--1-----3--5-------|');
const e1subs = '^ !';
const e2 = cold('x-x-x| ', {x: 10});
const expected = '--x-x-x-y-yz-z-z---|';
const values = {x: 10, y: 40, z: 90};

const result = e1.pipe(switchScan((acc: number, x: string) => e2.pipe(map(i => i * Number(x) + acc)), 0));
const result = e1.pipe(switchScan((acc, x) => e2.pipe(map(i => i * Number(x) + acc)), 0));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('switchScan', () => {
const e1subs = '^ ! ';
const expected = '-----------a--b--c----f---g---h---i--|';

const observableLookup = { x: x, y: y };
const observableLookup = { x, y };

const result = e1.pipe(switchScan((acc, value) => observableLookup[value], null));

Expand Down
16 changes: 8 additions & 8 deletions src/internal/operators/switchScan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable } from '../Observable';
import { ObservableInput, OperatorFunction } from '../types';
import { ObservableInput, ObservedValueOf, OperatorFunction } from '../types';
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { switchMap } from './switchMap';
Expand All @@ -25,21 +25,21 @@ import { tap } from './tap';
* @method switchScan
* @owner Observable
*/
export function switchScan<T, R>(
accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
export function switchScan<T, R, O extends ObservableInput<any>>(
accumulator: (acc: R, value: T, index: number) => O,
seed: R
): OperatorFunction<T, R> {
): OperatorFunction<T, ObservedValueOf<O>> {
return (source: Observable<T>) => source.lift(new SwitchScanOperator(accumulator, seed));
}

class SwitchScanOperator<T, R> implements Operator<T, R> {
constructor(private accumulator: (acc: R, value: T, index: number) => ObservableInput<R>, private seed: R) { }
class SwitchScanOperator<T, R, O extends ObservableInput<any>> implements Operator<T, O> {
constructor(private accumulator: (acc: R, value: T, index: number) => ObservableInput<O>, private seed: R) { }

call(subscriber: Subscriber<R>, source: any): any {
call(subscriber: Subscriber<O>, source: any): any {
let seed: R = this.seed;

return source.pipe(
switchMap((value: T, index: number): ObservableInput<R> => this.accumulator(seed, value, index)),
switchMap((value: T, index: number) => this.accumulator(seed, value, index)),
tap((value: R) => seed = value),
).subscribe(subscriber);
}
Expand Down

0 comments on commit 9ea12f9

Please sign in to comment.