| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  | import noteDetailService from './note_detail.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | import utils from './utils.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import recentNotesDialog from '../dialogs/recent_notes.js'; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | const $changesToPushCount = $("#changes-to-push-count"); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:08:58 -04:00
										 |  |  | let ws; | 
					
						
							|  |  |  | let lastSyncId; | 
					
						
							|  |  |  | let lastPingTs; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function logError(message) { | 
					
						
							|  |  |  |     console.log(utils.now(), message); // needs to be separate from .trace()
 | 
					
						
							|  |  |  |     console.trace(); | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (ws && ws.readyState === 1) { | 
					
						
							|  |  |  |         ws.send(JSON.stringify({ | 
					
						
							|  |  |  |             type: 'log-error', | 
					
						
							|  |  |  |             error: message | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-17 13:46:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function messageHandler(event) { | 
					
						
							|  |  |  |     const message = JSON.parse(event.data); | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (message.type === 'sync') { | 
					
						
							|  |  |  |         lastPingTs = new Date().getTime(); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:56:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (message.data.length > 0) { | 
					
						
							|  |  |  |             console.log(utils.now(), "Sync data: ", message.data); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             lastSyncId = message.data[message.data.length - 1].id; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         const syncData = message.data.filter(sync => sync.sourceId !== glob.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (syncData.some(sync => sync.entityName === 'branches') | 
					
						
							|  |  |  |             || syncData.some(sync => sync.entityName === 'notes')) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             console.log(utils.now(), "Reloading tree because of background changes"); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             treeService.reload(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  |         if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === noteDetailService.getCurrentNoteId())) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             utils.showMessage('Reloading note because of background changes'); | 
					
						
							| 
									
										
										
										
											2018-01-07 08:24:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  |             noteDetailService.reload(); | 
					
						
							| 
									
										
										
										
											2017-12-12 23:47:17 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (syncData.some(sync => sync.entityName === 'recent_notes')) { | 
					
						
							|  |  |  |             console.log(utils.now(), "Reloading recent notes because of background changes"); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:12:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             recentNotesDialog.reload(); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // we don't detect image changes here since images themselves are immutable and references should be
 | 
					
						
							|  |  |  |         // updated in note detail as well
 | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         $changesToPushCount.html(message.changesToPushCount); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (message.type === 'sync-hash-check-failed') { | 
					
						
							|  |  |  |         utils.utils.showError("Sync check failed!", 60000); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     else if (message.type === 'consistency-checks-failed') { | 
					
						
							|  |  |  |         utils.showError("Consistency checks failed! See logs for details.", 50 * 60000); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function connectWebSocket() { | 
					
						
							|  |  |  |     const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // use wss for secure messaging
 | 
					
						
							|  |  |  |     const ws = new WebSocket(protocol + "://" + location.host); | 
					
						
							|  |  |  |     ws.onopen = event => console.log(utils.now(), "Connected to server with WebSocket"); | 
					
						
							|  |  |  |     ws.onmessage = messageHandler; | 
					
						
							|  |  |  |     ws.onclose = function(){ | 
					
						
							|  |  |  |         // Try to reconnect in 5 seconds
 | 
					
						
							|  |  |  |         setTimeout(() => connectWebSocket(), 5000); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return ws; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:08:58 -04:00
										 |  |  | setTimeout(() => { | 
					
						
							|  |  |  |     ws = connectWebSocket(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lastSyncId = glob.maxSyncIdAtLoad; | 
					
						
							|  |  |  |     lastPingTs = new Date().getTime(); | 
					
						
							|  |  |  |     let connectionBrokenNotification = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setInterval(async () => { | 
					
						
							|  |  |  |         if (new Date().getTime() - lastPingTs > 30000) { | 
					
						
							|  |  |  |             if (!connectionBrokenNotification) { | 
					
						
							|  |  |  |                 connectionBrokenNotification = $.notify({ | 
					
						
							|  |  |  |                     // options
 | 
					
						
							|  |  |  |                     message: "Lost connection to server" | 
					
						
							|  |  |  |                 },{ | 
					
						
							|  |  |  |                     // settings
 | 
					
						
							|  |  |  |                     type: 'danger', | 
					
						
							|  |  |  |                     delay: 100000000 // keep it until we explicitly close it
 | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:08:58 -04:00
										 |  |  |         else if (connectionBrokenNotification) { | 
					
						
							|  |  |  |             await connectionBrokenNotification.close(); | 
					
						
							|  |  |  |             connectionBrokenNotification = null; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:08:58 -04:00
										 |  |  |             utils.showMessage("Re-connected to server"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:08:58 -04:00
										 |  |  |         ws.send(JSON.stringify({ | 
					
						
							|  |  |  |             type: 'ping', | 
					
						
							|  |  |  |             lastSyncId: lastSyncId | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     }, 1000); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }, 1000); | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     logError | 
					
						
							|  |  |  | }; |