diff --git a/packages/core/src/interactions/Point2DSpring.ts b/packages/core/src/interactions/Point2DSpring.ts index af0207d7..a9ca28ea 100644 --- a/packages/core/src/interactions/Point2DSpring.ts +++ b/packages/core/src/interactions/Point2DSpring.ts @@ -185,32 +185,32 @@ export class Point2DSpring implements Spring { 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({ diff --git a/packages/core/src/observables/__tests__/motionProperty.test.ts b/packages/core/src/observables/__tests__/motionProperty.test.ts index 00404ccd..43ea2b1e 100644 --- a/packages/core/src/observables/__tests__/motionProperty.test.ts +++ b/packages/core/src/observables/__tests__/motionProperty.test.ts @@ -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 }); diff --git a/packages/core/src/observables/__tests__/motionSubject.test.ts b/packages/core/src/observables/__tests__/motionSubject.test.ts index 87dc12e3..3272bbcd 100644 --- a/packages/core/src/observables/__tests__/motionSubject.test.ts +++ b/packages/core/src/observables/__tests__/motionSubject.test.ts @@ -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 }); diff --git a/packages/core/src/operators/__tests__/pluck.test.ts b/packages/core/src/operators/__tests__/pluck.test.ts index 95f85823..f8caed63 100644 --- a/packages/core/src/operators/__tests__/pluck.test.ts +++ b/packages/core/src/operators/__tests__/pluck.test.ts @@ -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); + } + ); } ); diff --git a/packages/core/src/operators/log.ts b/packages/core/src/operators/log.ts index 663df17b..12ca9de8 100644 --- a/packages/core/src/operators/log.ts +++ b/packages/core/src/operators/log.ts @@ -30,6 +30,7 @@ export type LogArgs = Partial<{ }>; export interface MotionLoggable { + log(label?: string): ObservableWithMotionOperators; log(kwargs?: LogArgs): ObservableWithMotionOperators; } @@ -51,7 +52,14 @@ export function withLog>>(superclass: * * it would log `Name: banana`. */ - log({ label = '', pluckPath = '' } = {}): ObservableWithMotionOperators { + log(label: string): ObservableWithMotionOperators; + log(kwargs: LogArgs): ObservableWithMotionOperators; + log(kwargs = {}): ObservableWithMotionOperators { + const { + label = '', + pluckPath = '', + } = kwargs as LogArgs; + let plucker: (value: T) => any | undefined; if (pluckPath) { diff --git a/packages/core/src/operators/pluck.ts b/packages/core/src/operators/pluck.ts index 74d29826..8ee5f7b6 100644 --- a/packages/core/src/operators/pluck.ts +++ b/packages/core/src/operators/pluck.ts @@ -21,11 +21,17 @@ import { ObservableWithMotionOperators, } from '../types'; +import { + isDefined, +} from '../typeGuards'; + +export type PluckPath = keyof T; export type PluckArgs = { - path: keyof T, + path: PluckPath, }; export interface MotionPluckable { + pluck(path: PluckPath): ObservableWithMotionOperators; pluck(kwargs: PluckArgs): ObservableWithMotionOperators; } @@ -43,7 +49,13 @@ export function withPluck>>(superclas * - `transform$.pluck({ path: 'translate.x' })` is equivalent to * `transform$.map(transform => transform.translate.x)` */ - pluck({ path }: PluckArgs): ObservableWithMotionOperators { + pluck(path: PluckPath): ObservableWithMotionOperators; + pluck({ path }: PluckArgs): ObservableWithMotionOperators; + pluck({ path }: PluckArgs & K): ObservableWithMotionOperators { + if (!isDefined(path)) { + path = arguments[0]; + } + return (this as any as ObservableWithMotionOperators>)._map({ transform: createPlucker(path as K) }); diff --git a/packages/demos-react/src/SwipeableDemo.tsx b/packages/demos-react/src/SwipeableDemo.tsx index 3a2b9d9c..7d582219 100644 --- a/packages/demos-react/src/SwipeableDemo.tsx +++ b/packages/demos-react/src/SwipeableDemo.tsx @@ -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 (