Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent multiple calls on routeDidChange #241

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { setupRouter, reset, whenRouteIdle } from 'ember-app-scheduler';

let requestId;

// to prevent scheduleOnce calling multiple times, give it the same ref to this function
const CALLBACK = function(transition) {
this.updateScrollPosition(transition);
}

class EmberRouterScroll extends EmberRouter {
@inject('router-scroll') service;

Expand Down Expand Up @@ -104,21 +109,13 @@ class EmberRouterScroll extends EmberRouter {

if (!scrollWhenIdle && !scrollWhenAfterRender) {
// out of the option, this happens on the tightest schedule
const callback = function() {
this.updateScrollPosition(transition);
}
scheduleOnce('render', this, callback);
scheduleOnce('render', this, CALLBACK.bind(this, transition));
} else if (scrollWhenAfterRender) {
// out of the option, this happens on the tightest schedule
const callback = function() {
this.updateScrollPosition(transition);
}
scheduleOnce('afterRender', this, callback);
scheduleOnce('afterRender', this, CALLBACK.bind(this, transition));
} else {
// as described in ember-app-scheduler, this addon can be used to delay rendering until after the route is idle
whenRouteIdle().then(() => {
this.updateScrollPosition(transition);
});
whenRouteIdle().then(CALLBACK.bind(this, transition));
}
}
}
Expand Down