Skip to content

Commit

Permalink
Improve keyboard navigation (move) performance
Browse files Browse the repository at this point in the history
  • Loading branch information
larsjohnsen committed Oct 18, 2016
1 parent 2e70427 commit 5deccb2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 30 additions & 7 deletions lib/modules/selectedEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ module.go = () => {
addFocusBorder();
}

addListener((selected, unselected) => unselected && Hover.infocard('showParent').close(false));
addListener(updateActiveElement);
addListener((selected, unselected) => unselected && Hover.infocard('showParent').close(false), 'beforePaint');
addListener(updateActiveElement, 'beforePaint');
addListener(updateLastSelectedCache);
};

Expand Down Expand Up @@ -167,12 +167,36 @@ function _selectedThing() {
return selectedThing;
}

const listeners = new $.Callbacks();
const listeners = { 'beforeScroll': [], 'beforePaint': [], 'idle': [] };

export function addListener(callback) {
listeners.add(callback);
export function addListener(callback, when = 'idle') {
listeners[when].push(callback);
}

const runCallbacks = (() => {
function runListeners(listeners, newSelected, oldSelected, options) {
for (const listener of listeners) listener(newSelected, oldSelected, options);
}

let idleCallback;
const requestIdle = window.requestIdleCallback ?
window.requestIdleCallback :
fn => requestAnimationFrame(() => requestAnimationFrame(fn));
const runIdle = callback => {
idleCallback = callback;
requestIdle(() => {
if (idleCallback) idleCallback();
idleCallback = null;
})
}

return (...args) => {
runListeners(listeners.beforeScroll, ...args);
requestAnimationFrame(() => runListeners(listeners.beforePaint, ...args));
runIdle(() => runListeners(listeners.idle, ...args));
};
})();

function _select(thingOrEntry, options = {}) {
const newThing = new Thing(thingOrEntry);
if (!newThing.thing || newThing.is(selectedThing)) return;
Expand All @@ -184,9 +208,8 @@ function _select(thingOrEntry, options = {}) {
(newThing.entry.getBoundingClientRect().top > oldSelected.entry.getBoundingClientRect().top ? 'down' : 'up') :
null;

listeners.fire(newSelected, oldSelected, options);
runCallbacks(newSelected, oldSelected, options);

// options.scrollStyle may be changed by listeners, so have this run last
scrollToElement(newThing.entry, options);

selectedThing = newSelected;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/showImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ module.go = () => {
findAllImages(document.body);
document.addEventListener('dragstart', () => false);

SelectedEntry.addListener(mediaBrowse);
SelectedEntry.addListener(mediaBrowse, 'beforeScroll');

// Handle spotlight next/prev hiding open expando's
const spotlight = document.querySelector('#siteTable_organic');
Expand Down

0 comments on commit 5deccb2

Please sign in to comment.