@@ -29,34 +29,40 @@ class SCRouter extends HTMLElement {
2929 return ;
3030 }
3131
32- // Figure out the new view.
32+ // Store the new view.
3333 this . _newView = this . _routes . get ( route ) ;
3434
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.
3638 if ( this . _isTransitioningBetweenViews ) {
3739 return Promise . resolve ( ) ;
3840 }
39-
4041 this . _isTransitioningBetweenViews = true ;
4142
43+ // Assume that there's no outgoing animation required.
4244 let outViewPromise = Promise . resolve ( ) ;
4345
4446 // If there is a current view...
4547 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.
4749 if ( this . _currentView === this . _newView ) {
4850 return this . _currentView . update ( data ) ;
4951 }
5052
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.
5255 outViewPromise = this . _currentView . out ( data ) ;
5356 }
5457
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.
5561 return outViewPromise
5662 . then ( _ => {
5763 this . _currentView = this . _newView ;
5864 this . _isTransitioningBetweenViews = false ;
59- return this . _newView . in ( data )
65+ return this . _newView . in ( data ) ;
6066 } ) ;
6167 }
6268
@@ -65,26 +71,30 @@ class SCRouter extends HTMLElement {
6571 return this . _onChanged ( ) ;
6672 }
6773
68- _clearRoutes ( ) {
69- this . _routes . clear ( ) ;
70- }
71-
72- _createRoute ( route , view ) {
74+ addRoute ( route , view ) {
7375 if ( this . _routes . has ( route ) )
7476 return console . warn ( `Route already exists: ${ route } ` ) ;
7577
7678 this . _routes . set ( route , view ) ;
7779 }
7880
79- _createRoutes ( ) {
81+ _addRoutes ( ) {
8082 for ( let view of document . querySelectorAll ( 'sc-view' ) ) {
8183 if ( ! view . route )
8284 continue ;
8385
84- this . _createRoute ( new RegExp ( view . route , 'i' ) , view ) ;
86+ this . addRoute ( new RegExp ( view . route , 'i' ) , view ) ;
8587 }
8688 }
8789
90+ _removeRoute ( route ) {
91+ this . _routes . delete ( route ) ;
92+ }
93+
94+ _clearRoutes ( ) {
95+ this . _routes . clear ( ) ;
96+ }
97+
8898 createdCallback ( ) {
8999 this . _onChanged = this . _onChanged . bind ( this ) ;
90100 this . _routes = new Map ( ) ;
@@ -93,7 +103,7 @@ class SCRouter extends HTMLElement {
93103 attachedCallback ( ) {
94104 window . addEventListener ( 'popstate' , this . _onChanged ) ;
95105 this . _clearRoutes ( ) ;
96- this . _createRoutes ( ) ;
106+ this . _addRoutes ( ) ;
97107 this . _onChanged ( ) ;
98108 }
99109
0 commit comments