Skip to content

Commit

Permalink
[refactored] merge 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/D3425
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent b9cdabb commit 80b1264
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
14 changes: 6 additions & 8 deletions packages/core/src/interactions/Draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class Draggable {
(downEvent.target as Element).setPointerCapture(downEvent.pointerId);
}

moveSubscription = move$.merge({ others: [ up$ ] })._filter({
moveSubscription = move$.merge([ up$ ])._filter({
predicate: (nextEvent: PartialPointerEvent) => nextEvent.pointerId === downEvent.pointerId
}).subscribe(
(nextEvent: PartialPointerEvent) => {
Expand Down Expand Up @@ -251,13 +251,11 @@ export class Draggable {
}
);

cancellationSubscription = this.cancellation$.merge({
others: [
when(not(this.enabled$)),
cancel$,
contextMenu$,
]
}).subscribe(
cancellationSubscription = this.cancellation$.merge([
when(not(this.enabled$)),
cancel$,
contextMenu$,
]).subscribe(
() => {
if (moveSubscription) {
moveSubscription.unsubscribe();
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/interactions/Swipeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,11 @@ export class Swipeable {

subscribe({
sink: spring.enabled$,
source: this.whenThresholdFirstCrossed$.merge({
others: [
when(spring.state$.isAnyOf([ State.AT_REST ])).rewriteTo({
value$: false,
})
]
}),
source: this.whenThresholdFirstCrossed$.merge([
when(spring.state$.isAnyOf([ State.AT_REST ])).rewriteTo({
value$: false,
})
]),
});

subscribe({
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/operators/__tests__/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,23 @@ describe('motionObservable.merge',
expect(listener).to.have.been.calledWith(4);
}
);

it('should have a shorthand signature',
() => {
stream1.merge([ stream2, stream3 ]).subscribe(listener);

mockObserver1.next(1);
expect(listener).to.have.been.calledOnce.and.calledWith(1);

mockObserver3.next(3);
expect(listener).to.have.been.calledTwice.and.calledWith(3);

mockObserver2.next(2);
expect(listener).to.have.been.calledThrice.and.calledWith(2);

mockObserver1.next(4);
expect(listener).to.have.been.calledWith(4);
}
);
}
);
15 changes: 13 additions & 2 deletions packages/core/src/operators/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import {
Observer,
} from '../types';

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

import {
createPlucker,
} from './pluck';

export type MergeArgs<T> = {
others: Array<Observable<T>>
others: Array<Observable<T>>,
};

export interface MotionMergeable<T> extends Observable<T> {
merge(others: Array<Observable<T>>): ObservableWithMotionOperators<T>;
merge(kwargs: MergeArgs<T>): ObservableWithMotionOperators<T>;
}

Expand All @@ -43,7 +48,13 @@ export function withMerge<T, S extends Constructor<Observable<T>>>(superclass: S
* Dispatches values as it receives them, both from upstream and from any
* streams provided as arguments.
*/
merge({ others }: MergeArgs<T>): ObservableWithMotionOperators<T> {
merge(others: Array<Observable<T>>): ObservableWithMotionOperators<T>;
merge(kwargs: MergeArgs<T>): ObservableWithMotionOperators<T>;
merge({ others }: MergeArgs<T> & Array<Observable<T>>): ObservableWithMotionOperators<T> {
if (!isDefined(others)) {
others = arguments[0];
}

return new MotionObservable(
(observer: Observer<T>) => {
const subscriptions = [this, ...others].map(
Expand Down
36 changes: 15 additions & 21 deletions packages/views-dom/src/getPointerEventStreamsFromElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,21 @@ export function getPointerEventStreamsFromElement(element: Element): PointerEven
};
} else {
return {
down$: getEventStreamFromElement<MouseEvent>('mousedown', element).merge({
others: [
convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchstart', element)
),
],
}),
move$: getEventStreamFromElement<MouseEvent>('mousemove', element).merge({
others: [
convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchmove', element)
),
],
}),
up$: getEventStreamFromElement<MouseEvent>('mouseup', element).merge({
others: [
convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchend', element)
),
],
}),
down$: getEventStreamFromElement<MouseEvent>('mousedown', element).merge([
convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchstart', element)
),
]),
move$: getEventStreamFromElement<MouseEvent>('mousemove', element).merge([
convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchmove', element)
),
]),
up$: getEventStreamFromElement<MouseEvent>('mouseup', element).merge([
convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchend', element)
),
]),
cancel$: convertTouchEventsToPointerEvents(
getEventStreamFromElement<TouchEvent>('touchcancel', element)
),
Expand Down

0 comments on commit 80b1264

Please sign in to comment.