Skip to content

Commit

Permalink
[refactored] distanceFrom to have a 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/D3422
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent cb16ebe commit ac4f518
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 1 addition & 3 deletions packages/core/src/interactions/Swipeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ export class Swipeable {
// `resistanceProgress`. Thus, we independently calculate the threshold
// here.

this.isThresholdMet$ = draggedX$.distanceFrom({
origin$: 0,
}).threshold({ limit$: Swipeable.VISUAL_THRESHOLD }).isAnyOf({
this.isThresholdMet$ = draggedX$.distanceFrom(0).threshold({ limit$: Swipeable.VISUAL_THRESHOLD }).isAnyOf({
candidates: [ThresholdRegion.ABOVE, ThresholdRegion.WITHIN]
});
this.whenThresholdCrossed$ = when(this.isThresholdMet$.dedupe());
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/operators/__tests__/distanceFrom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,27 @@ describe('motionObservable.distanceFrom',
expect(valueInLastCall).to.be.closeTo(hypoteneuse, 1);
}
);

it('should accept a shorthand signature for numbers',
() => {
stream.distanceFrom(10).subscribe(listener);

mockObserver.next(15);

expect(listener).to.have.been.calledWith(5);
}
);

it('should accept a shorthand signature for points',
() => {
stream.distanceFrom({ x: 100, y: 100 }).subscribe(listener);

mockObserver.next({ x: 0, y: 0 });

const hypoteneuse = 100 / Math.cos(Math.PI / 4);
const valueInLastCall = listener.lastCall.args[0];
expect(valueInLastCall).to.be.closeTo(hypoteneuse, 1);
}
);
}
);
20 changes: 15 additions & 5 deletions packages/core/src/operators/distanceFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@
import {
Constructor,
Dict,
MaybeReactive,
MotionReactiveMappable,
Observable,
ObservableWithMotionOperators,
Point2D,
} from '../types';

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

export type DistanceFromArgs<T> = MaybeReactive<{
origin$: T & (Point2D | number),
}>;
export type DistanceFromOrigin<T> = (T & (Point2D | number)) | Observable<T & (Point2D | number)>;

export type DistanceFromArgs<T> = {
origin$: DistanceFromOrigin<T>,
};

export interface MotionMeasurable<T> {
distanceFrom(origin$: DistanceFromOrigin<T>): ObservableWithMotionOperators<number>;
distanceFrom(kwargs: DistanceFromArgs<T>): ObservableWithMotionOperators<number>;
}

Expand All @@ -42,7 +46,13 @@ export function withDistanceFrom<T, S extends Constructor<MotionReactiveMappable
* The origin may be a number or a point, but the dispatched value will
* always be a number; distance is computed using Pythagorean theorem.
*/
distanceFrom({ origin$ }: DistanceFromArgs<T>): ObservableWithMotionOperators<number> {
distanceFrom(origin$: DistanceFromOrigin<T>): ObservableWithMotionOperators<number>;
distanceFrom({ origin$ }: DistanceFromArgs<T>): ObservableWithMotionOperators<number>;
distanceFrom({ origin$ }: DistanceFromArgs<T> & DistanceFromOrigin<T>): ObservableWithMotionOperators<number> {
if (!isDefined(origin$)) {
origin$ = arguments[0] as DistanceFromOrigin<T>;
}

return this._reactiveMap({
transform({ upstream, origin }: Dict<(T & number) | (T & Point2D)>) {
if (isPoint2D(upstream)) {
Expand Down

0 comments on commit ac4f518

Please sign in to comment.