@@ -3,7 +3,7 @@ import { htmlSafe } from '@ember/template';
3
3
import { tracked } from '@glimmer/tracking' ;
4
4
import { action } from '@ember/object' ;
5
5
6
- import { rawTimeout , task , timeout } from 'ember-concurrency' ;
6
+ import { rawTimeout , task } from 'ember-concurrency' ;
7
7
import { buildWaiter } from '@ember/test-waiters' ;
8
8
9
9
const SPEED = 200 ;
@@ -22,10 +22,12 @@ export default class ProgressService extends Service {
22
22
super ( ...arguments ) ;
23
23
24
24
this . router . on ( 'routeWillChange' , this . onRouteWillChange ) ;
25
+ this . router . on ( 'routeDidChange' , this . onRouteDidChange ) ;
25
26
}
26
27
27
28
willDestroy ( ) {
28
29
this . router . off ( 'routeWillChange' , this . onRouteWillChange ) ;
30
+ this . router . off ( 'routeDidChange' , this . onRouteDidChange ) ;
29
31
30
32
super . willDestroy ( ...arguments ) ;
31
33
}
@@ -35,77 +37,56 @@ export default class ProgressService extends Service {
35
37
}
36
38
37
39
@action
38
- async onRouteWillChange ( transition ) {
39
- this . counterTask . perform ( transition . promise ) ;
40
- }
41
-
42
- increaseCounter ( ) {
43
- this . count += 1 ;
40
+ async onRouteWillChange ( ) {
44
41
this . updateTask . perform ( ) ;
45
42
}
46
43
47
- decreaseCounter ( ) {
48
- this . count -= 1 ;
44
+ @action
45
+ onRouteDidChange ( ) {
46
+ this . updateTask . cancelAll ( ) ;
49
47
}
50
48
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
-
70
49
@( task ( function * ( ) {
71
50
let token = waiter . beginAsync ( ) ;
72
51
73
52
this . progress = 0 ;
74
53
this . _style = `width: 0%` ;
75
54
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
+ } %`;
95
80
}
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`;
96
87
97
- this . _style = `transition: width ${ SPEED } ms linear; width: ${
98
- this . progress * 100
99
- } %`;
88
+ waiter . endAsync ( token ) ;
100
89
}
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 ) ;
109
90
} ) . drop ( ) )
110
91
updateTask ;
111
92
}
0 commit comments