From d3e7b3eae8b44f1b6443f950edba97df34e62374 Mon Sep 17 00:00:00 2001 From: Brenton Simpson Date: Sun, 8 Oct 2017 20:00:51 -0700 Subject: [PATCH] [refactored] startWith to have shorthand signature Summary: Part of https://github.com/material-motion/material-motion-js/issues/230 Reviewers: O2 Material Motion, O3 Material JavaScript platform reviewers, #material_motion, vietanh Reviewed By: #material_motion, vietanh Subscribers: vietanh Tags: #material_motion Differential Revision: http://codereview.cc/D3427 --- packages/core/src/interactions/Tossable.ts | 2 +- .../src/operators/__tests__/startWith.test.ts | 17 +++++++++++++++++ packages/core/src/operators/startWith.ts | 16 +++++++++++++++- packages/demos-react/src/SwipeableDemo.tsx | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/core/src/interactions/Tossable.ts b/packages/core/src/interactions/Tossable.ts index fd3a3732..9f559236 100644 --- a/packages/core/src/interactions/Tossable.ts +++ b/packages/core/src/interactions/Tossable.ts @@ -230,7 +230,7 @@ export class Tossable { this.velocity$ = getVelocity$({ // Since drag starts at rest, whenDragIsAtRest$ emits immediately. Thus, // we start with { 0, 0 } to ensure velocity doesn't emit undefined. - value$: this.draggedLocation$.startWith({ value: { x: 0, y: 0 } }), + value$: this.draggedLocation$.startWith({ x: 0, y: 0 }), pulse$: whenDragIsAtRest$, }); diff --git a/packages/core/src/operators/__tests__/startWith.test.ts b/packages/core/src/operators/__tests__/startWith.test.ts index 15e0b323..7a5dd66b 100644 --- a/packages/core/src/operators/__tests__/startWith.test.ts +++ b/packages/core/src/operators/__tests__/startWith.test.ts @@ -81,5 +81,22 @@ describe('motionObservable.startWith', expect(listener).to.have.been.calledWith(20); } ); + + it('should have a shorthand signature', + () => { + stream.startWith(10).subscribe(listener); + + expect(listener).to.have.been.calledWith(10); + } + ); + + it('should prefer the shorthand signature if there are an incorrect number of named arguments', + () => { + const expected = { value: 10, timestamp: 12345 }; + stream.startWith(expected).subscribe(listener); + + expect(listener).to.have.been.calledWith(expected); + } + ); } ); diff --git a/packages/core/src/operators/startWith.ts b/packages/core/src/operators/startWith.ts index 41d66163..ea701136 100644 --- a/packages/core/src/operators/startWith.ts +++ b/packages/core/src/operators/startWith.ts @@ -25,11 +25,16 @@ import { Observer, } from '../types'; +import { + isDefined, +} from '../typeGuards'; + export type StartWithArgs = { value: T, }; export interface MotionSeedable { + startWith(value: T): ObservableWithMotionOperators; startWith(kwargs: StartWithArgs): ObservableWithMotionOperators; } @@ -41,7 +46,16 @@ export function withStartWith): ObservableWithMotionOperators { + startWith(value: T): ObservableWithMotionOperators; + startWith(kwargs: StartWithArgs): ObservableWithMotionOperators; + startWith({ value }: StartWithArgs & T): ObservableWithMotionOperators { + const hasValue = isDefined(value); + // Only destructure if the supplied argument has the correct number of + // members. + if (!hasValue || (hasValue && Object.keys(arguments[0]).length > 1)) { + value = arguments[0]; + } + return new MotionObservable( (observer: Observer) => { observer.next(value); diff --git a/packages/demos-react/src/SwipeableDemo.tsx b/packages/demos-react/src/SwipeableDemo.tsx index 7d582219..b631c61f 100644 --- a/packages/demos-react/src/SwipeableDemo.tsx +++ b/packages/demos-react/src/SwipeableDemo.tsx @@ -113,7 +113,7 @@ class SwipeableCard extends React.Component<{}, {}> { combineStyleStreams({ filter: 'invert()', - scale: swipeable.styleStreamsByTargetName.icon.scale$.startWith({ value: 0 }), + scale: swipeable.styleStreamsByTargetName.icon.scale$.startWith(0), willChange: swipeable.styleStreamsByTargetName.icon.willChange$, }).subscribe(this.iconStyle$); }