Skip to content

Commit

Permalink
Switch ScrollBehavior to take getCurrentLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed May 6, 2016
1 parent b84abd3 commit ec2477e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/ScrollBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { readState, saveState } from 'history/lib/DOMStateStorage';
const MAX_SCROLL_ATTEMPTS = 2;

export default class ScrollBehavior {
constructor(history, getCurrentKey) {
this._getCurrentKey = getCurrentKey;
constructor(history, getCurrentLocation) {
this._getCurrentLocation = getCurrentLocation;

// This helps avoid some jankiness in fighting against the browser's
// default scroll behavior on `POP` transitions.
Expand Down Expand Up @@ -109,6 +109,10 @@ export default class ScrollBehavior {
saveState(currentKey, { ...state, scrollPosition });
};

_getCurrentKey() {
return this._getCurrentLocation().key;
}

_cancelCheckScroll() {
if (this._checkScrollHandle !== null) {
requestAnimationFrame.cancel(this._checkScrollHandle);
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export default function withScroll(history, shouldUpdateScroll) {
// currentLocation will always be defined when needed.
let currentLocation = null;

function getCurrentKey() {
return currentLocation.key;
function getCurrentLocation() {
return currentLocation;
}

let listeners = [];
Expand All @@ -32,7 +32,7 @@ export default function withScroll(history, shouldUpdateScroll) {

function listen(listener) {
if (listeners.length === 0) {
scrollBehavior = new ScrollBehavior(history, getCurrentKey);
scrollBehavior = new ScrollBehavior(history, getCurrentLocation);
unlisten = history.listen(onChange);
}

Expand Down

0 comments on commit ec2477e

Please sign in to comment.