Skip to content

Commit

Permalink
[refactored] pluck and log to have shorthand signatures
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/D3423
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent ac4f518 commit 776bacf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
12 changes: 6 additions & 6 deletions packages/core/src/interactions/Point2DSpring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,32 +185,32 @@ export class Point2DSpring implements Spring<Point2D> {
constructor() {
subscribe({
sink: this.xSpring.destination$,
source: this.destination$.pluck({ path: 'x'}),
source: this.destination$.pluck('x'),
});

subscribe({
sink: this.ySpring.destination$,
source: this.destination$.pluck({ path: 'y'}),
source: this.destination$.pluck('y'),
});

subscribe({
sink: this.xSpring.initialValue$,
source: this.initialValue$.pluck({ path: 'x'}),
source: this.initialValue$.pluck('x'),
});

subscribe({
sink: this.ySpring.initialValue$,
source: this.initialValue$.pluck({ path: 'y'}),
source: this.initialValue$.pluck('y'),
});

subscribe({
sink: this.xSpring.initialVelocity$,
source: this.initialVelocity$.pluck({ path: 'x'}),
source: this.initialVelocity$.pluck('x'),
});

subscribe({
sink: this.ySpring.initialVelocity$,
source: this.initialVelocity$.pluck({ path: 'y'}),
source: this.initialVelocity$.pluck('y'),
});

subscribe({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('motionProperty',

it(`should be a property with operators`,
() => {
property.pluck({ path: 'a' }).subscribe(listener);
property.pluck('a').subscribe(listener);

property.write({ 'a': 1 });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('motionSubject',

it(`should be a subject with operators`,
() => {
subject.pluck({ path: 'a' }).subscribe(listener);
subject.pluck('a').subscribe(listener);

subject.next({ 'a': 1 });

Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/operators/__tests__/pluck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ describe('motionObservable.pluck',
expect(listener).to.have.been.calledWith(10);
}
);

it('should have a shorthand signature',
() => {
const translate = {
x: 10,
y: 15,
};

const transform = {
translate,
};

stream.pluck({ path: 'translate.x' }).subscribe(listener);

mockObserver.next(transform);

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

10 changes: 9 additions & 1 deletion packages/core/src/operators/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type LogArgs = Partial<{
}>;

export interface MotionLoggable<T> {
log(label?: string): ObservableWithMotionOperators<T>;
log(kwargs?: LogArgs): ObservableWithMotionOperators<T>;
}

Expand All @@ -51,7 +52,14 @@ export function withLog<T, S extends Constructor<MotionTappable<T>>>(superclass:
*
* it would log `Name: banana`.
*/
log({ label = '', pluckPath = '' } = {}): ObservableWithMotionOperators<T> {
log(label: string): ObservableWithMotionOperators<T>;
log(kwargs: LogArgs): ObservableWithMotionOperators<T>;
log(kwargs = {}): ObservableWithMotionOperators<T> {
const {
label = '',
pluckPath = '',
} = kwargs as LogArgs;

let plucker: (value: T) => any | undefined;

if (pluckPath) {
Expand Down
16 changes: 14 additions & 2 deletions packages/core/src/operators/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ import {
ObservableWithMotionOperators,
} from '../types';

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

export type PluckPath<T> = keyof T;
export type PluckArgs<T> = {
path: keyof T,
path: PluckPath<T>,
};

export interface MotionPluckable<T> {
pluck<K extends keyof T, U extends T[K]>(path: PluckPath<T>): ObservableWithMotionOperators<U>;
pluck<K extends keyof T, U extends T[K]>(kwargs: PluckArgs<T>): ObservableWithMotionOperators<U>;
}

Expand All @@ -43,7 +49,13 @@ export function withPluck<T, S extends Constructor<MotionMappable<T>>>(superclas
* - `transform$.pluck({ path: 'translate.x' })` is equivalent to
* `transform$.map(transform => transform.translate.x)`
*/
pluck<K extends keyof T, U extends T[K]>({ path }: PluckArgs<T>): ObservableWithMotionOperators<U> {
pluck<K extends keyof T, U extends T[K]>(path: PluckPath<T>): ObservableWithMotionOperators<U>;
pluck<K extends keyof T, U extends T[K]>({ path }: PluckArgs<T>): ObservableWithMotionOperators<U>;
pluck<K extends keyof T, U extends T[K]>({ path }: PluckArgs<T> & K): ObservableWithMotionOperators<U> {
if (!isDefined(path)) {
path = arguments[0];
}

return (this as any as ObservableWithMotionOperators<Record<K, any>>)._map({
transform: createPlucker<K>(path as K)
});
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 @@ -42,7 +42,7 @@ import {
} from 'material-motion-views-react';

export function SwipeableDemo() {
const width$ = viewportDimensions$.pluck({ path: 'width' });
const width$ = viewportDimensions$.pluck('width');

return (
<ul
Expand Down

0 comments on commit 776bacf

Please sign in to comment.