| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const messaging = (function() { | 
					
						
							| 
									
										
										
										
											2017-12-09 20:44:06 -05:00
										 |  |  |     const changesToPushCountEl = $("#changes-to-push-count"); | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  |     let ws = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function logError(message) { | 
					
						
							| 
									
										
										
										
											2017-12-15 21:36:21 -05:00
										 |  |  |         console.log(message); // needs to be separate from .trace()
 | 
					
						
							|  |  |  |         console.trace(); | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (ws && ws.readyState === 1) { | 
					
						
							|  |  |  |             ws.send(JSON.stringify({ | 
					
						
							|  |  |  |                 type: 'log-error', | 
					
						
							|  |  |  |                 error: message | 
					
						
							|  |  |  |             })); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  |     function messageHandler(event) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |         const message = JSON.parse(event.data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         if (message.data.length > 0) { | 
					
						
							|  |  |  |             console.log(message); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |         if (message.type === 'sync') { | 
					
						
							|  |  |  |             lastPingTs = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             const syncData = message.data.filter(sync => sync.source_id !== glob.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             if (syncData.some(sync => sync.entity_name === 'notes_tree')) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |                 console.log("Reloading tree because of background changes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 noteTree.reload(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             if (syncData.some(sync => sync.entity_name === 'notes' && sync.entity_id === noteEditor.getCurrentNoteId())) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |                 showMessage('Reloading note because background change'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 noteEditor.reload(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             if (syncData.some(sync => sync.entity_name === 'recent_notes')) { | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  |                 console.log("Reloading recent notes because of background changes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 recentNotes.reload(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |             changesToPushCountEl.html(message.changesToPushCount); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-12 23:47:17 -05:00
										 |  |  |         else if (message.type === 'sync-hash-check-failed') { | 
					
						
							|  |  |  |             showError("Sync check failed!", 60000); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-14 23:21:03 -05:00
										 |  |  |         else if (message.type === 'consistency-checks-failed') { | 
					
						
							|  |  |  |             showError("Consistency checks failed! See logs for details.", 50 * 60000); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function connectWebSocket() { | 
					
						
							| 
									
										
										
										
											2017-12-03 10:12:16 -05:00
										 |  |  |         const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |         // use wss for secure messaging
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:12:16 -05:00
										 |  |  |         ws = new WebSocket(protocol + "://" + location.host); | 
					
						
							|  |  |  |         ws.onopen = event => console.log("Connected to server with WebSocket"); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |         ws.onmessage = messageHandler; | 
					
						
							|  |  |  |         ws.onclose = function(){ | 
					
						
							|  |  |  |             // Try to reconnect in 5 seconds
 | 
					
						
							|  |  |  |             setTimeout(() => connectWebSocket(), 5000); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     connectWebSocket(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let lastPingTs = new Date().getTime(); | 
					
						
							|  |  |  |     let connectionBrokenNotification = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setInterval(async () => { | 
					
						
							|  |  |  |         if (new Date().getTime() - lastPingTs > 5000) { | 
					
						
							|  |  |  |             if (!connectionBrokenNotification) { | 
					
						
							|  |  |  |                 connectionBrokenNotification = $.notify({ | 
					
						
							|  |  |  |                     // options
 | 
					
						
							|  |  |  |                     message: "Lost connection to server" | 
					
						
							|  |  |  |                 },{ | 
					
						
							|  |  |  |                     // settings
 | 
					
						
							|  |  |  |                     type: 'danger', | 
					
						
							|  |  |  |                     delay: 100000000 // keep it until we explicitly close it
 | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (connectionBrokenNotification) { | 
					
						
							|  |  |  |             await connectionBrokenNotification.close(); | 
					
						
							|  |  |  |             connectionBrokenNotification = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             showMessage("Re-connected to server"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, 3000); | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         logError | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | })(); |