Skip to content

Commit

Permalink
Prevent multiple calls on routeDidChange (#241)
Browse files Browse the repository at this point in the history
* Prevent multiple calls on routeDidChange

* ensure we have same ref to function so only called once in backburner
  • Loading branch information
snewcomer authored Feb 25, 2020
1 parent 9a8a9cd commit 4db3449
Showing 1 changed file with 8 additions and 11 deletions.
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

0 comments on commit 4db3449

Please sign in to comment.