Skip to content

Commit

Permalink
[refactored] threshold 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

Tags: #material_motion

Differential Revision: http://codereview.cc/D3428
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent d3e7b3e commit 6aaa6cd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/interactions/Swipeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class Swipeable {
})
});

this.direction$ = draggedX$.threshold({ limit$: 0 }).isAnyOf([ ThresholdRegion.ABOVE ]).rewrite({
this.direction$ = draggedX$.threshold(0).isAnyOf([ ThresholdRegion.ABOVE ]).rewrite({
mapping: {
true: Direction.RIGHT,
false: Direction.LEFT,
Expand All @@ -174,7 +174,7 @@ export class Swipeable {
// `resistanceProgress`. Thus, we independently calculate the threshold
// here.

this.isThresholdMet$ = draggedX$.distanceFrom(0).threshold({ limit$: Swipeable.VISUAL_THRESHOLD }).isAnyOf([
this.isThresholdMet$ = draggedX$.distanceFrom(0).threshold(Swipeable.VISUAL_THRESHOLD).isAnyOf([
ThresholdRegion.ABOVE,
ThresholdRegion.WITHIN,
]);
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/operators/__tests__/threshold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,15 @@ describe('motionObservable.threshold',
expect(listener).to.have.been.calledTwice.and.to.have.been.calledWith(ThresholdRegion.BELOW);
}
);

it('should have a shorthand signature',
() => {
subject.threshold(7).subscribe(listener);

subject.next(3);

expect(listener).to.have.been.calledWith(ThresholdRegion.BELOW);
}
);
}
);
28 changes: 20 additions & 8 deletions packages/core/src/operators/threshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* under the License.
*/

import {
ThresholdRegion,
} from '../enums';

import {
Constructor,
MaybeReactive,
Expand All @@ -23,24 +27,32 @@ import {
} from '../types';

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

import {
_ReactiveMapOptions,
} from './foundation/_reactiveMap';

export type ThresholdArgs = _ReactiveMapOptions & MaybeReactive<{
limit$: number,
}>;
export type ThresholdLimit = number | Observable<number>;
export type ThresholdArgs = _ReactiveMapOptions & {
limit$: ThresholdLimit,
};

export interface MotionThresholdable {
threshold(limit$: ThresholdLimit): ObservableWithMotionOperators<ThresholdRegion>;
threshold(kwargs: ThresholdArgs): ObservableWithMotionOperators<ThresholdRegion>;
}

export function withThreshold<T, S extends Constructor<MotionReactiveMappable<T>>>(superclass: S): S & Constructor<MotionThresholdable> {
return class extends superclass implements MotionThresholdable {
threshold({ limit$, ...reactiveMapOptions }: ThresholdArgs): ObservableWithMotionOperators<ThresholdRegion> {
threshold(limit$: ThresholdLimit): ObservableWithMotionOperators<ThresholdRegion>;
threshold(kwargs: ThresholdArgs): ObservableWithMotionOperators<ThresholdRegion>;
threshold({ limit$, ...reactiveMapOptions }: any): ObservableWithMotionOperators<ThresholdRegion> {
if (!isDefined(limit$)) {
limit$ = arguments[0];
}

return (this as any as MotionReactiveMappable<number>)._reactiveMap({
transform: ({ upstream, limit }) => {
if (upstream < limit) {
Expand All @@ -53,9 +65,9 @@ export function withThreshold<T, S extends Constructor<MotionReactiveMappable<T>
}
},
inputs: {
limit: limit$,
limit: limit$ as ThresholdLimit,
},
...reactiveMapOptions,
...reactiveMapOptions as _ReactiveMapOptions,
});
}
};
Expand Down

0 comments on commit 6aaa6cd

Please sign in to comment.