| 
									
										
										
										
											2018-03-03 14:15:50 -08:00
										 |  |  |  | import { updateInstanceInfo } from '../../_actions/instances' | 
					
						
							|  |  |  |  | import { createStream } from '../../_actions/streaming' | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  | import { getTimeline } from '../../_api/timelines' | 
					
						
							|  |  |  |  | import { addStatusesOrNotifications } from '../../_actions/addStatusOrNotification' | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | export function timelineObservers (store) { | 
					
						
							| 
									
										
										
										
											2018-02-15 09:02:46 -08:00
										 |  |  |  |   // stream to watch for local/federated/etc. updates. home and notification
 | 
					
						
							|  |  |  |  |   // updates are handled in timelineObservers.js
 | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |   let currentTimelineStream | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |   function shutdownPreviousStream () { | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |     if (currentTimelineStream) { | 
					
						
							|  |  |  |  |       currentTimelineStream.close() | 
					
						
							|  |  |  |  |       currentTimelineStream = null | 
					
						
							| 
									
										
										
										
											2018-02-15 09:02:46 -08:00
										 |  |  |  |       if (process.env.NODE_ENV !== 'production') { | 
					
						
							|  |  |  |  |         window.currentTimelineStream = null | 
					
						
							|  |  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   function shouldObserveTimeline (timeline) { | 
					
						
							|  |  |  |  |     return timeline && | 
					
						
							|  |  |  |  |       !( | 
					
						
							| 
									
										
										
										
											2018-04-18 20:43:13 -07:00
										 |  |  |  |       timeline !== 'local' && | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |         timeline !== 'federated' && | 
					
						
							|  |  |  |  |         !timeline.startsWith('list/') && | 
					
						
							|  |  |  |  |         !timeline.startsWith('tag/') | 
					
						
							| 
									
										
										
										
											2018-04-18 20:43:13 -07:00
										 |  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   store.observe('currentTimeline', async (currentTimeline) => { | 
					
						
							|  |  |  |  |     if (!process.browser) { | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |       return | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     shutdownPreviousStream() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!shouldObserveTimeline(currentTimeline)) { | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |       return | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:37:05 -07:00
										 |  |  |  |     let { currentInstance } = store.get() | 
					
						
							|  |  |  |  |     let { accessToken } = store.get() | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |     await updateInstanceInfo(currentInstance) | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:37:05 -07:00
										 |  |  |  |     let currentTimelineIsUnchanged = () => { | 
					
						
							|  |  |  |  |       let { | 
					
						
							|  |  |  |  |         currentInstance: newCurrentInstance, | 
					
						
							|  |  |  |  |         currentTimeline: newCurrentTimeline | 
					
						
							|  |  |  |  |       } = store.get() | 
					
						
							|  |  |  |  |       return newCurrentInstance === currentInstance && | 
					
						
							|  |  |  |  |         newCurrentTimeline === currentTimeline | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 18:00:49 -07:00
										 |  |  |  |     if (!currentTimelineIsUnchanged()) { | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |       return | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |     let timelineItemIds = store.getForTimeline(currentInstance, | 
					
						
							|  |  |  |  |       currentTimeline, 'timelineItemIds') | 
					
						
							|  |  |  |  |     let firstTimelineItemId = timelineItemIds && timelineItemIds[0] | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 18:00:49 -07:00
										 |  |  |  |     let onOpenStream = async () => { | 
					
						
							|  |  |  |  |       if (!firstTimelineItemId || !currentTimelineIsUnchanged()) { | 
					
						
							|  |  |  |  |         return | 
					
						
							|  |  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |       // fill in the "streaming gap" – i.e. fetch the most recent 20 items so that there isn't
 | 
					
						
							|  |  |  |  |       // a big gap in the timeline if you haven't looked at it in awhile
 | 
					
						
							| 
									
										
										
										
											2018-03-19 18:00:49 -07:00
										 |  |  |  |       let newTimelineItems = await getTimeline(currentInstance, accessToken, | 
					
						
							|  |  |  |  |         currentTimeline, null, firstTimelineItemId) | 
					
						
							| 
									
										
										
										
											2018-03-19 10:09:05 -07:00
										 |  |  |  |       if (newTimelineItems.length) { | 
					
						
							|  |  |  |  |         addStatusesOrNotifications(currentInstance, currentTimeline, newTimelineItems) | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:37:05 -07:00
										 |  |  |  |     let { currentInstanceInfo } = store.get() | 
					
						
							|  |  |  |  |     let streamingApi = currentInstanceInfo.urls.streaming_api | 
					
						
							| 
									
										
										
										
											2018-03-19 18:00:49 -07:00
										 |  |  |  |     currentTimelineStream = createStream(streamingApi, currentInstance, accessToken, | 
					
						
							|  |  |  |  |       currentTimeline, onOpenStream) | 
					
						
							| 
									
										
										
										
											2018-02-15 09:02:46 -08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (process.env.NODE_ENV !== 'production') { | 
					
						
							|  |  |  |  |       window.currentTimelineStream = currentTimelineStream | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-11 13:46:57 -08:00
										 |  |  |  |   }) | 
					
						
							|  |  |  |  | } |