Skip to content

Commit

Permalink
[refactored] rewriteTo 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/D3429
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent 6aaa6cd commit 9102418
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
6 changes: 2 additions & 4 deletions packages/core/src/interactions/Swipeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,13 @@ export class Swipeable {
subscribe({
sink: spring.enabled$,
source: this.whenThresholdFirstCrossed$.merge([
when(spring.state$.isAnyOf([ State.AT_REST ])).rewriteTo({
value$: false,
})
when(spring.state$.isAnyOf([ State.AT_REST ])).rewriteTo(false),
]),
});

subscribe({
sink: tossable.resistanceFactor$,
source: this.whenThresholdCrossed$.rewriteTo({ value$: DISABLED_RESISTANCE_FACTOR }),
source: this.whenThresholdCrossed$.rewriteTo(DISABLED_RESISTANCE_FACTOR),
});

subscribe({
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/operators/__tests__/rewriteTo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,36 @@ describe('motionObservable.rewriteTo',
expect(listener).to.have.been.calledTwice.and.to.have.been.calledWith(12).and.to.have.been.calledWith(15);
}
);

it('should have a shorthand signature for constants',
() => {
stream.rewriteTo('banana').subscribe(listener);

mockObserver.next();
mockObserver.next(false);
mockObserver.next(123);
mockObserver.next({a: '1234'});

expect(listener).to.have.callCount(4).and.to.always.have.been.calledWith('banana');
}
);

it('should have a shorthand signature for streams',
() => {
stream.rewriteTo(value$).subscribe(listener);

mockObserver.next();
mockObserver.next(false);

value$.next(12);
value$.next(15);

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

mockObserver.next({});

expect(listener).to.have.been.calledTwice.and.to.have.been.calledWith(15);
}
);
}
);
14 changes: 13 additions & 1 deletion packages/core/src/operators/rewriteTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,36 @@ import {
ObservableWithMotionOperators,
} from '../types';

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

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

export type RewriteToValue<U> = U | Observable<U>;
export type RewriteToArgs<U> = _ReactiveMapOptions & {
value$: U | Observable<U>,
value$: RewriteToValue<U>,
};

export interface MotionRewriteToable {
rewriteTo<U>(kwargs: RewriteToArgs<U>): ObservableWithMotionOperators<U>;
rewriteTo<U>(value$: RewriteToValue<U>): ObservableWithMotionOperators<U>;
}

export function withRewriteTo<T, S extends Constructor<MotionReactiveMappable<T>>>(superclass: S): S & Constructor<MotionRewriteToable> {
return class extends superclass implements MotionRewriteToable {
/**
* Dispatches its argument every time it receives a value from upstream.
*/
rewriteTo<U>(value$: RewriteToValue<U>): ObservableWithMotionOperators<U>;
rewriteTo<U>(kwargs: RewriteToArgs<U>): ObservableWithMotionOperators<U>;
rewriteTo<U>({ value$, onlyDispatchWithUpstream = true, ...reactiveMapOptions }: RewriteToArgs<U>): ObservableWithMotionOperators<U> {
if (!isDefined(value$)) {
value$ = arguments[0];
}

return this._reactiveMap({
transform: ({ value }) => value,
inputs: {
Expand Down

0 comments on commit 9102418

Please sign in to comment.