| 
									
										
										
										
											2015-12-31 17:39:29 -08:00
										 |  |  | import { Disposable, Observable } from 'rx'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function location$(history) { | 
					
						
							|  |  |  |   return Observable.create(function(observer) { | 
					
						
							|  |  |  |     const dispose = history.listen(function(location) { | 
					
						
							|  |  |  |       observer.onNext(location); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Disposable.create(() => { | 
					
						
							|  |  |  |       dispose(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const emptyLocation = { | 
					
						
							|  |  |  |   pathname: '', | 
					
						
							|  |  |  |   search: '', | 
					
						
							|  |  |  |   hash: '' | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let prevKey; | 
					
						
							|  |  |  | let isSyncing = false; | 
					
						
							| 
									
										
										
										
											2016-01-07 14:51:41 -08:00
										 |  |  | export default function historySaga( | 
					
						
							| 
									
										
										
										
											2015-12-31 17:39:29 -08:00
										 |  |  |   history, | 
					
						
							|  |  |  |   updateLocation, | 
					
						
							|  |  |  |   goTo, | 
					
						
							|  |  |  |   goBack, | 
					
						
							|  |  |  |   routerState$ | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  |   routerState$.subscribe( | 
					
						
							|  |  |  |     location => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!location) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // store location has changed, update history
 | 
					
						
							| 
									
										
										
										
											2016-01-09 18:38:15 -08:00
										 |  |  |       if (!location.key || location.key !== prevKey) { | 
					
						
							| 
									
										
										
										
											2015-12-31 17:39:29 -08:00
										 |  |  |         isSyncing = true; | 
					
						
							|  |  |  |         history.transitionTo({ ...emptyLocation, ...location }); | 
					
						
							|  |  |  |         isSyncing = false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   location$(history) | 
					
						
							|  |  |  |     .doOnNext(location => { | 
					
						
							|  |  |  |       prevKey = location.key; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (isSyncing) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return updateLocation(location); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .subscribe(() => {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   goTo | 
					
						
							|  |  |  |     .doOnNext((route = '/') => { | 
					
						
							|  |  |  |       history.push(route); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .subscribe(() => {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   goBack | 
					
						
							|  |  |  |     .doOnNext(() => { | 
					
						
							|  |  |  |       history.goBack(); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .subscribe(() => {}); | 
					
						
							|  |  |  | } |