Skip to content

Commit

Permalink
Schedule scrollTo work to after content paint by using ember-app-sche…
Browse files Browse the repository at this point in the history
…duler (#57)

#17
  • Loading branch information
cclo7 authored and briangonzalez committed Aug 2, 2017
1 parent d835b15 commit 4eb771a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const {
computed,
get,
inject,
run: { next },
} = Ember;

export default Ember.Mixin.create({
scheduler: inject.service('scheduler'),
service: inject.service('router-scroll'),

isFastBoot: computed(function() {
Expand All @@ -26,7 +26,9 @@ export default Ember.Mixin.create({

if (get(this, 'isFastBoot')) { return; }

next(() => this.updateScrollPosition(transitions));
this.get('scheduler').scheduleWork('afterContentPaint', () => {
this.updateScrollPosition(transitions);
});
},

updateScrollPosition(transitions) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"devDependencies": {
"broccoli-asset-rev": "^2.4.2",
"ember-ajax": "^2.5.6",
"ember-app-scheduler": "0.0.5",
"ember-cli": "^2.12.1",
"ember-cli-app-version": "^3.0.0",
"ember-cli-dependency-checker": "^1.4.0",
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/mixins/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import { module, test } from 'qunit';

module('mixin:router-scroll');

function getSchedulerMock() {
return {
scheduleWork: ((eventName, callback) => {
callback();
}),
};
}

test('when the application is FastBooted', (assert) => {
assert.expect(1);

const done = assert.async();
const RouterScrollObject = Ember.Object.extend(RouterScroll);
const subject = RouterScrollObject.create({
isFastBoot: true,
scheduler: getSchedulerMock(),
updateScrollPosition() {
assert.notOk(true, 'it should not call updateScrollPosition.');
done();
Expand All @@ -33,6 +42,7 @@ test('when the application is not FastBooted', (assert) => {
const RouterScrollObject = Ember.Object.extend(RouterScroll);
const subject = RouterScrollObject.create({
isFastBoot: false,
scheduler: getSchedulerMock(),
updateScrollPosition() {
assert.ok(true, 'it should call updateScrollPosition.');
done();
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,12 @@ ember-ajax@^2.5.6:
dependencies:
ember-cli-babel "^5.1.5"

[email protected]:
version "0.0.5"
resolved "https://registry.yarnpkg.com/ember-app-scheduler/-/ember-app-scheduler-0.0.5.tgz#d9228207e169c60d1d6546943e51fcd6a1554b90"
dependencies:
ember-cli-babel "^5.1.7"

ember-cli-app-version@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ember-cli-app-version/-/ember-cli-app-version-3.0.0.tgz#d67a33aeec7bd03187fbe72c5663dadec4c3368a"
Expand Down

0 comments on commit 4eb771a

Please sign in to comment.