Skip to content

Commit

Permalink
[refactored] startWith to have shorthand signature
Browse files Browse the repository at this point in the history
Summary: Part of #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
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent a6898e3 commit d3e7b3e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/interactions/Tossable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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$,
});

Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/operators/__tests__/startWith.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
);
16 changes: 15 additions & 1 deletion packages/core/src/operators/startWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ import {
Observer,
} from '../types';

import {
isDefined,
} from '../typeGuards';

export type StartWithArgs<T> = {
value: T,
};

export interface MotionSeedable<T> {
startWith(value: T): ObservableWithMotionOperators<T>;
startWith(kwargs: StartWithArgs<T>): ObservableWithMotionOperators<T>;
}

Expand All @@ -41,7 +46,16 @@ export function withStartWith<T, S extends Constructor<ObservableWithFoundationa
* Returns a remembered stream, so each new observer will synchronously
* receive the most recent value.
*/
startWith({ value }: StartWithArgs<T>): ObservableWithMotionOperators<T> {
startWith(value: T): ObservableWithMotionOperators<T>;
startWith(kwargs: StartWithArgs<T>): ObservableWithMotionOperators<T>;
startWith({ value }: StartWithArgs<T> & T): ObservableWithMotionOperators<T> {
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<T>) => {
observer.next(value);
Expand Down
2 changes: 1 addition & 1 deletion packages/demos-react/src/SwipeableDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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$);
}
Expand Down

0 comments on commit d3e7b3e

Please sign in to comment.