Skip to content

Commit

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

Tags: #material_motion

Differential Revision: http://codereview.cc/D3424
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent 776bacf commit b9cdabb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/interactions/Point2DSpring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ export class Point2DSpring implements Spring<Point2D> {
}

readonly state$: ObservableWithMotionOperators<State> = anyOf([
this.xSpring.state$.isAnyOf({ candidates: [ State.ACTIVE ] }),
this.ySpring.state$.isAnyOf({ candidates: [ State.ACTIVE ] }),
this.xSpring.state$.isAnyOf([ State.ACTIVE ]),
this.ySpring.state$.isAnyOf([ State.ACTIVE ]),
]).dedupe().rewrite<State, State>({
mapping: {
true: State.ACTIVE,
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/interactions/Swipeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class Swipeable {

this.iconSpring.initialValue = ICON_SPRING_INITIAL_VALUE;

const tossableIsAtRest$ = tossable.state$.isAnyOf({ candidates: [ State.AT_REST ] });
const tossableIsAtRest$ = tossable.state$.isAnyOf([ State.AT_REST ]);
subscribe({
sink: tossable.resistanceFactor$,
source: when(tossableIsAtRest$).rewriteTo({
Expand All @@ -160,7 +160,7 @@ export class Swipeable {
})
});

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

this.isThresholdMet$ = draggedX$.distanceFrom(0).threshold({ limit$: Swipeable.VISUAL_THRESHOLD }).isAnyOf({
candidates: [ThresholdRegion.ABOVE, ThresholdRegion.WITHIN]
});
this.isThresholdMet$ = draggedX$.distanceFrom(0).threshold({ limit$: Swipeable.VISUAL_THRESHOLD }).isAnyOf([
ThresholdRegion.ABOVE,
ThresholdRegion.WITHIN,
]);
this.whenThresholdCrossed$ = when(this.isThresholdMet$.dedupe());
this.whenThresholdFirstCrossed$ = when(tossable.resistanceFactor$.dedupe().isAnyOf({ candidates: [ DISABLED_RESISTANCE_FACTOR ] }));
this.whenThresholdFirstCrossed$ = when(tossable.resistanceFactor$.dedupe().isAnyOf([ DISABLED_RESISTANCE_FACTOR ]));

subscribe({
sink: spring.enabled$,
source: this.whenThresholdFirstCrossed$.merge({
others: [
when(spring.state$.isAnyOf({ candidates: [ State.AT_REST ] })).rewriteTo({
when(spring.state$.isAnyOf([ State.AT_REST ])).rewriteTo({
value$: false,
})
]
Expand Down Expand Up @@ -233,7 +234,7 @@ export class Swipeable {
// cares about final position.
subscribe({
sink: this.swipeState$,
source: when(draggable.state$.isAnyOf({ candidates: [ State.AT_REST ] })).rewriteTo({
source: when(draggable.state$.isAnyOf([ State.AT_REST ])).rewriteTo({
value$: this.isThresholdMet$.rewrite({
mapping: {
true: this.direction$,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/interactions/Tossable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ export class Tossable {
subscribe({
sink: this.state$,
source: anyOf([
spring.state$.isAnyOf({ candidates: [ State.ACTIVE ] }),
draggable.state$.isAnyOf({ candidates: [ State.ACTIVE ] }),
spring.state$.isAnyOf([ State.ACTIVE ]),
draggable.state$.isAnyOf([ State.ACTIVE ]),
]).rewrite({
mapping: {
true: State.ACTIVE,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/operators/__tests__/isAnyOf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,19 @@ describe('motionObservable.isAnyOf',
expect(listener).to.have.been.calledTwice.and.to.have.been.calledWith(true);
}
);

it('should have a shorthand signature',
() => {
subject.isAnyOf([ 1, argSubject, 3 ]).subscribe(listener);

argSubject.next(2);
subject.next(4);

expect(listener).to.have.been.calledOnce.and.to.have.been.calledWith(false);

argSubject.next(4);
expect(listener).to.have.been.calledTwice.and.to.have.been.calledWith(true);
}
);
}
);
13 changes: 11 additions & 2 deletions packages/core/src/operators/isAnyOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {

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

import {
Expand All @@ -30,13 +31,15 @@ import {
ObservableWithMotionOperators,
} from '../types';

export type IsAnyOfCandidates<T> = Array<T | Observable<T>>;
export type IsAnyOfArgs<T> = {
// To be consistent with our naming strategy, this should probably be called
// candidate$s, but it looks weird to combine the two sufficies.
candidates: Array<T | Observable<T>>,
candidates: IsAnyOfCandidates<T>,
};

export interface MotionIsAnyOfable<T> {
isAnyOf(candidates: IsAnyOfCandidates<T>): ObservableWithMotionOperators<boolean>;
isAnyOf(kwargs: IsAnyOfArgs<T>): ObservableWithMotionOperators<boolean>;
}

Expand All @@ -46,7 +49,13 @@ export function withIsAnyOf<T, S extends Constructor<MotionReactiveMappable<T>>>
* Dispatches `true` when it receives a value that matches any of the
* provided candidates and `false` otherwise.
*/
isAnyOf({ candidates }: IsAnyOfArgs<T>): ObservableWithMotionOperators<boolean> {
isAnyOf(candidates: IsAnyOfCandidates<T>): ObservableWithMotionOperators<boolean>;
isAnyOf({ candidates }: IsAnyOfArgs<T>): ObservableWithMotionOperators<boolean>;
isAnyOf({ candidates }: IsAnyOfArgs<T> & IsAnyOfCandidates<T>): ObservableWithMotionOperators<boolean> {
if (!isDefined(candidates)) {
candidates = arguments[0];
}

return combineLatest([ this, ...candidates ])._map({
transform: ([ upstream, ...currentCandidates ]: Array<T>) => currentCandidates.includes(upstream)
});
Expand Down

0 comments on commit b9cdabb

Please sign in to comment.