| 
									
										
										
										
											2018-03-04 13:37:55 -08:00
										 |  |  | // Avoid re-renders by caching the most recent value of an array
 | 
					
						
							|  |  |  | // or an object, using an approach similar to https://github.com/reactjs/reselect.
 | 
					
						
							|  |  |  | // This avoids the issue where Svelte may keep re-rendering because it doesn't
 | 
					
						
							|  |  |  | // know if an object/array has changed or not.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-05 17:57:36 -07:00
										 |  |  | import isEqual from 'lodash-es/isEqual' | 
					
						
							| 
									
										
										
										
											2018-03-04 13:37:55 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | if (process.browser && process.env.NODE_ENV !== 'production') { | 
					
						
							|  |  |  |   window.reselectStats = {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function reselect (store, outputKey, inputKey) { | 
					
						
							|  |  |  |   let prevValue | 
					
						
							|  |  |  |   let nextValue | 
					
						
							|  |  |  |   let count = 0 | 
					
						
							|  |  |  |   let countKey = `${outputKey}_reselectCount` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   store.compute(countKey, [inputKey], input => { | 
					
						
							|  |  |  |     if (process.browser && process.env.NODE_ENV !== 'production') { | 
					
						
							|  |  |  |       window.reselectStats[inputKey] = window.reselectStats[inputKey] || {numInputChanges: 0, numOutputChanges: 0} | 
					
						
							|  |  |  |       window.reselectStats[inputKey].numInputChanges++ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!isEqual(prevValue, input)) { | 
					
						
							|  |  |  |       nextValue = input | 
					
						
							|  |  |  |       count++ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return count | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   store.compute(outputKey, [countKey], () => { | 
					
						
							|  |  |  |     if (process.browser && process.env.NODE_ENV !== 'production') { | 
					
						
							|  |  |  |       window.reselectStats[inputKey].numOutputChanges++ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     prevValue = nextValue | 
					
						
							|  |  |  |     nextValue = null | 
					
						
							|  |  |  |     return prevValue | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } |