Skip to content

Commit 011cb76

Browse files
authored
Merge pull request #453 from ember-learn/fix-progress-bar
fix progress-bar transition aborted
2 parents 7b02921 + ff75865 commit 011cb76

File tree

14 files changed

+150
-123
lines changed

14 files changed

+150
-123
lines changed

addon/services/progress.js

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { htmlSafe } from '@ember/template';
33
import { tracked } from '@glimmer/tracking';
44
import { action } from '@ember/object';
55

6-
import { rawTimeout, task, timeout } from 'ember-concurrency';
6+
import { rawTimeout, task } from 'ember-concurrency';
77
import { buildWaiter } from '@ember/test-waiters';
88

99
const SPEED = 200;
@@ -22,10 +22,12 @@ export default class ProgressService extends Service {
2222
super(...arguments);
2323

2424
this.router.on('routeWillChange', this.onRouteWillChange);
25+
this.router.on('routeDidChange', this.onRouteDidChange);
2526
}
2627

2728
willDestroy() {
2829
this.router.off('routeWillChange', this.onRouteWillChange);
30+
this.router.off('routeDidChange', this.onRouteDidChange);
2931

3032
super.willDestroy(...arguments);
3133
}
@@ -35,77 +37,56 @@ export default class ProgressService extends Service {
3537
}
3638

3739
@action
38-
async onRouteWillChange(transition) {
39-
this.counterTask.perform(transition.promise);
40-
}
41-
42-
increaseCounter() {
43-
this.count += 1;
40+
async onRouteWillChange() {
4441
this.updateTask.perform();
4542
}
4643

47-
decreaseCounter() {
48-
this.count -= 1;
44+
@action
45+
onRouteDidChange() {
46+
this.updateTask.cancelAll();
4947
}
5048

51-
@task(function* (promise) {
52-
// if the promise resolves on the next tick, we don't show the loading bar
53-
let resolved = false;
54-
promise.then(() => {
55-
resolved = true;
56-
});
57-
yield timeout(0);
58-
59-
if (!resolved) {
60-
try {
61-
this.increaseCounter();
62-
yield promise;
63-
} finally {
64-
this.decreaseCounter();
65-
}
66-
}
67-
})
68-
counterTask;
69-
7049
@(task(function* () {
7150
let token = waiter.beginAsync();
7251

7352
this.progress = 0;
7453
this._style = `width: 0%`;
7554

76-
while (this.count !== 0) {
77-
yield rawTimeout(SPEED);
78-
79-
let currentAmount;
80-
if (this.progress >= 0 && this.progress < 0.2) {
81-
currentAmount = 0.1;
82-
} else if (this.progress >= 0.2 && this.progress < 0.5) {
83-
currentAmount = 0.04;
84-
} else if (this.progress >= 0.5 && this.progress < 0.8) {
85-
currentAmount = 0.02;
86-
} else if (this.progress >= 0.8 && this.progress < 0.99) {
87-
currentAmount = 0.005;
88-
} else {
89-
currentAmount = 0;
90-
}
91-
92-
this.progress += currentAmount;
93-
if (this.progress > 0.998) {
94-
this.progress = 0.998;
55+
try {
56+
while (true) {
57+
yield rawTimeout(SPEED);
58+
59+
let currentAmount;
60+
if (this.progress >= 0 && this.progress < 0.2) {
61+
currentAmount = 0.1;
62+
} else if (this.progress >= 0.2 && this.progress < 0.5) {
63+
currentAmount = 0.04;
64+
} else if (this.progress >= 0.5 && this.progress < 0.8) {
65+
currentAmount = 0.02;
66+
} else if (this.progress >= 0.8 && this.progress < 0.99) {
67+
currentAmount = 0.005;
68+
} else {
69+
currentAmount = 0;
70+
}
71+
72+
this.progress += currentAmount;
73+
if (this.progress > 0.998) {
74+
this.progress = 0.998;
75+
}
76+
77+
this._style = `transition: width ${SPEED}ms linear; width: ${
78+
this.progress * 100
79+
}%`;
9580
}
81+
} finally {
82+
this._style = `transition: width ${SPEED}ms linear; width: 100%`;
83+
yield rawTimeout(SPEED);
84+
this._style = `transition: opacity ${
85+
SPEED * 2
86+
}ms linear; width: 100%; opacity: 0`;
9687

97-
this._style = `transition: width ${SPEED}ms linear; width: ${
98-
this.progress * 100
99-
}%`;
88+
waiter.endAsync(token);
10089
}
101-
102-
this._style = `transition: width ${SPEED}ms linear; width: 100%`;
103-
yield rawTimeout(SPEED);
104-
this._style = `transition: opacity ${
105-
SPEED * 2
106-
}ms linear; width: 100%; opacity: 0`;
107-
108-
waiter.endAsync(token);
10990
}).drop())
11091
updateTask;
11192
}

package-lock.json

Lines changed: 43 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"ember-source": "~3.28.8",
9191
"ember-source-channel-url": "^3.0.0",
9292
"ember-template-lint": "^3.15.0",
93-
"ember-try": "https://git@github.com/mansona/ember-try.git#npm-overrides",
93+
"ember-try": "https://git@github.com/ember-cli/ember-try.git#master",
9494
"eslint": "^7.32.0",
9595
"eslint-config-prettier": "^8.3.0",
9696
"eslint-plugin-ember": "^10.5.8",
@@ -103,6 +103,7 @@
103103
"loader.js": "^4.7.0",
104104
"np": "*",
105105
"npm-run-all": "^4.1.5",
106+
"pnpm": "^7.0.0",
106107
"prember": "^1.1.0",
107108
"prettier": "^2.5.1",
108109
"qunit": "^2.17.2",

tests/acceptance/progress-bar-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { module, test } from 'qunit';
2+
import { visit, currentURL, click } from '@ember/test-helpers';
3+
import { setupApplicationTest } from 'ember-qunit';
4+
5+
module('Acceptance | progress bar', function (hooks) {
6+
setupApplicationTest(hooks);
7+
8+
test('progress bar should not causea a transition abort error', async function (assert) {
9+
await visit('/');
10+
11+
await click('[data-test-basic-route]');
12+
13+
assert.equal(currentURL(), '/fancy');
14+
15+
await visit('/');
16+
17+
await click('[data-test-slow-route]');
18+
19+
assert.equal(currentURL(), '/fancy');
20+
});
21+
});

tests/dummy/app/router.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ export default class Router extends EmberRouter {
66
rootURL = config.rootURL;
77
}
88

9-
Router.map(function () {});
9+
Router.map(function () {
10+
this.route('basicy');
11+
this.route('slow');
12+
this.route('fancy');
13+
});

0 commit comments

Comments
 (0)