@@ -29,34 +29,40 @@ class SCRouter extends HTMLElement {
29
29
return ;
30
30
}
31
31
32
- // Figure out the new view.
32
+ // Store the new view.
33
33
this . _newView = this . _routes . get ( route ) ;
34
34
35
- // We don't want to create more promises here.
35
+ // We don't want to create more promises for the outgoing view animation,
36
+ // because then we get a lot of hanging Promises, so we add a boolean gate
37
+ // here to stop if there's already a transition running.
36
38
if ( this . _isTransitioningBetweenViews ) {
37
39
return Promise . resolve ( ) ;
38
40
}
39
-
40
41
this . _isTransitioningBetweenViews = true ;
41
42
43
+ // Assume that there's no outgoing animation required.
42
44
let outViewPromise = Promise . resolve ( ) ;
43
45
44
46
// If there is a current view...
45
47
if ( this . _currentView ) {
46
- // If it's the one we already have, just update it.
48
+ // ...and it's the one we already have, just update it.
47
49
if ( this . _currentView === this . _newView ) {
48
50
return this . _currentView . update ( data ) ;
49
51
}
50
52
51
- // Otherwise we animate it out.
53
+ // Otherwise animate it out, and take the Promise made by the view as an
54
+ // indicator that the view is done.
52
55
outViewPromise = this . _currentView . out ( data ) ;
53
56
}
54
57
58
+ // Whenever the outgoing animation is done (which may be immediately if
59
+ // there isn't one), update the references to the current view, allow
60
+ // outgoing animations to proceed.
55
61
return outViewPromise
56
62
. then ( _ => {
57
63
this . _currentView = this . _newView ;
58
64
this . _isTransitioningBetweenViews = false ;
59
- return this . _newView . in ( data )
65
+ return this . _newView . in ( data ) ;
60
66
} ) ;
61
67
}
62
68
@@ -65,26 +71,30 @@ class SCRouter extends HTMLElement {
65
71
return this . _onChanged ( ) ;
66
72
}
67
73
68
- _clearRoutes ( ) {
69
- this . _routes . clear ( ) ;
70
- }
71
-
72
- _createRoute ( route , view ) {
74
+ addRoute ( route , view ) {
73
75
if ( this . _routes . has ( route ) )
74
76
return console . warn ( `Route already exists: ${ route } ` ) ;
75
77
76
78
this . _routes . set ( route , view ) ;
77
79
}
78
80
79
- _createRoutes ( ) {
81
+ _addRoutes ( ) {
80
82
for ( let view of document . querySelectorAll ( 'sc-view' ) ) {
81
83
if ( ! view . route )
82
84
continue ;
83
85
84
- this . _createRoute ( new RegExp ( view . route , 'i' ) , view ) ;
86
+ this . addRoute ( new RegExp ( view . route , 'i' ) , view ) ;
85
87
}
86
88
}
87
89
90
+ _removeRoute ( route ) {
91
+ this . _routes . delete ( route ) ;
92
+ }
93
+
94
+ _clearRoutes ( ) {
95
+ this . _routes . clear ( ) ;
96
+ }
97
+
88
98
createdCallback ( ) {
89
99
this . _onChanged = this . _onChanged . bind ( this ) ;
90
100
this . _routes = new Map ( ) ;
@@ -93,7 +103,7 @@ class SCRouter extends HTMLElement {
93
103
attachedCallback ( ) {
94
104
window . addEventListener ( 'popstate' , this . _onChanged ) ;
95
105
this . _clearRoutes ( ) ;
96
- this . _createRoutes ( ) ;
106
+ this . _addRoutes ( ) ;
97
107
this . _onChanged ( ) ;
98
108
}
99
109
0 commit comments