Created
July 20, 2019 20:20
-
-
Save DimitryDushkin/64639645ab7068b496c8839f45ea1ef2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type DragEvent = { x: number; y: number }; | |
export function createDragObservable<T extends PointerEvent>( | |
up$: Observable<T>, | |
down$: Observable<T>, | |
move$: Observable<T> | |
): Observable<DragEvent> { | |
let startPosition: DragEvent; | |
return down$.pipe( | |
mergeMap(e => { | |
startPosition = startPosition || { x: e.pageX, y: e.pageY }; | |
return move$.pipe( | |
takeUntil(up$), | |
map(e => ({ | |
x: e.pageX - startPosition.x, | |
y: e.pageY - startPosition.y | |
})) | |
); | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment