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 1 commit
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: 16 additions & 3 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { scheduleOnce } from '@ember/runloop';
import { setupRouter, reset, whenRouteIdle } from 'ember-app-scheduler';

let requestId;
let REQUEST_CALLBACK;

class EmberRouterScroll extends EmberRouter {
@inject('router-scroll') service;
Expand Down Expand Up @@ -84,6 +85,8 @@ class EmberRouterScroll extends EmberRouter {
}
}
}

REQUEST_CALLBACK = null;
}

_routeWillChange() {
Expand All @@ -95,7 +98,7 @@ class EmberRouterScroll extends EmberRouter {
}

_routeDidChange(transition) {
if (get(this, 'isFastBoot')) {
if (get(this, 'isFastBoot') || REQUEST_CALLBACK) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this should be debounce like instead of throttle like...

return;
}

Expand All @@ -107,18 +110,28 @@ class EmberRouterScroll extends EmberRouter {
const callback = function() {
this.updateScrollPosition(transition);
}

REQUEST_CALLBACK = callback;

scheduleOnce('render', this, callback);
} else if (scrollWhenAfterRender) {
// out of the option, this happens on the tightest schedule
const callback = function() {
this.updateScrollPosition(transition);
}

REQUEST_CALLBACK = callback;

scheduleOnce('afterRender', this, callback);
} else {
// as described in ember-app-scheduler, this addon can be used to delay rendering until after the route is idle
whenRouteIdle().then(() => {
const callback = function() {
this.updateScrollPosition(transition);
});
}

REQUEST_CALLBACK = callback;

whenRouteIdle().then(callback);
}
}
}
Expand Down