| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     function logError(message) { | 
					
						
							| 
									
										
										
										
											2017-12-18 22:06:24 -05:00
										 |  |  |         console.log(now(), message); // needs to be separate from .trace()
 | 
					
						
							| 
									
										
										
										
											2017-12-15 21:36:21 -05:00
										 |  |  |         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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (message.type === 'sync') { | 
					
						
							|  |  |  |             lastPingTs = new Date().getTime(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  |             if (message.data.length > 0) { | 
					
						
							|  |  |  |                 console.log(now(), "Sync data: ", message.data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 lastSyncId = message.data[message.data.length - 1].id; | 
					
						
							| 
									
										
										
										
											2017-12-17 13:46:18 -05:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             const syncData = message.data.filter(sync => sync.sourceId !== glob.sourceId); | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             if (syncData.some(sync => sync.entityName === 'notes_tree') | 
					
						
							|  |  |  |                 || syncData.some(sync => sync.entityName === 'notes')) { | 
					
						
							| 
									
										
										
										
											2018-01-06 22:56:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 22:06:24 -05:00
										 |  |  |                 console.log(now(), "Reloading tree because of background changes"); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 noteTree.reload(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === noteEditor.getCurrentNoteId())) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |                 showMessage('Reloading note because background change'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 noteEditor.reload(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             if (syncData.some(sync => sync.entityName === 'recent_notes')) { | 
					
						
							| 
									
										
										
										
											2017-12-18 22:06:24 -05:00
										 |  |  |                 console.log(now(), "Reloading recent notes because of background changes"); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:42:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 recentNotes.reload(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 08:24:04 -05:00
										 |  |  |             // we don't detect image changes here since images themselves are immutable and references should be
 | 
					
						
							|  |  |  |             // updated in note detail as well
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-19 23:22:21 -05:00
										 |  |  |         const ws = new WebSocket(protocol + "://" + location.host); | 
					
						
							| 
									
										
										
										
											2017-12-18 22:06:24 -05:00
										 |  |  |         ws.onopen = event => console.log(now(), "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); | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return ws; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  |     const ws = connectWebSocket(); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  |     let lastSyncId = glob.maxSyncIdAtLoad; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  |     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"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-19 23:22:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ws.send(JSON.stringify({ | 
					
						
							|  |  |  |             type: 'ping', | 
					
						
							|  |  |  |             lastSyncId: lastSyncId | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     }, 1000); | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         logError | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-28 17:52:47 -05:00
										 |  |  | })(); |