Skip to content

Commit

Permalink
[refactored] _debounce to use named args
Browse files Browse the repository at this point in the history
Summary: Continuation of #193

Reviewers: O2 Material Motion, O3 Material JavaScript platform reviewers, #material_motion, featherless

Reviewed By: O2 Material Motion, #material_motion, featherless

Tags: #material_motion

Differential Revision: http://codereview.cc/D3399
  • Loading branch information
appsforartists committed Oct 11, 2017
1 parent 8914c14 commit b2d5453
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/getFrame$.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
*
* Useful as a pulse for `_debounce` to ensure that UI work only happens once
* per frame. Since no rendering will happen until `requestAnimationFrame` is
* called, it should be safe to `_debounce(frame$)` without missing a frame.
* called, it should be safe to `_debounce({ pulse$: frame$ })` without missing
* a frame.
*/
let frame$: ObservableWithMotionOperators<number>;
export function getFrame$() {
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 @@ -168,10 +168,10 @@ export class Tossable {
// start from 0
subscribe({
sink: spring.initialValue$,
source: this.location$._debounce(whenDragIsAtRest$)
source: this.location$._debounce({ pulse$: whenDragIsAtRest$}),
});

const locationOnDown$ = this.location$._debounce(whenDragIsActive$);
const locationOnDown$ = this.location$._debounce({ pulse$: whenDragIsActive$ });

this.draggedLocation$ = draggable.value$.addedBy<Point2D>(locationOnDown$, { onlyDispatchWithUpstream: true })._reactiveMap({
transform: ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Point2DSpring',
//
// Thus, we only measure what its values are when the state changes,
// to be sure we are checking the correct values.
spring.value$._debounce(spring.state$).subscribe(valueListener);
spring.value$._debounce({ pulse$: spring.state$ }).subscribe(valueListener);

const initialValue = { x: 2, y: 3 };
const destination = { x: 4, y: 2 };
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('Point2DSpring',
//
// Thus, we only measure what its values are when the state changes,
// to be sure we are checking the correct values.
spring.value$._debounce(spring.state$).subscribe(valueListener);
spring.value$._debounce({ pulse$: spring.state$ }).subscribe(valueListener);

const initialValue = { x: 4, y: 8 };
const destination = { x: 0, y: 0 };
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/operators/foundation/_debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ import {
Observer
} from '../../types';

export type _DebounceArgs = {
pulse$?: Observable<any>,
}

export interface MotionDebounceable<T> {
_debounce(pulse$?: Observable<any>): ObservableWithMotionOperators<T>;
_debounce(kwargs?: _DebounceArgs): ObservableWithMotionOperators<T>;
}

export function withDebounce<T, S extends Constructor<MotionNextOperable<T>>>(superclass: S): S
Expand All @@ -47,7 +51,7 @@ export function withDebounce<T, S extends Constructor<MotionNextOperable<T>>>(su
* By default, it will throttle to the framerate using
* `requestAnimationFrame`.
*/
_debounce(pulse$: Observable<any> = getFrame$()): ObservableWithMotionOperators<T> {
_debounce({ pulse$ = getFrame$() } = {}): ObservableWithMotionOperators<T> {
return new MotionObservable<T>(
(observer: Observer<T>) => {
let awaitingDispatch = false;
Expand Down

0 comments on commit b2d5453

Please sign in to comment.