| 
									
										
										
										
											2018-03-25 12:29:00 -04:00
										 |  |  | import utils from "./utils.js"; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:02:39 -04:00
										 |  |  | import Branch from "../entities/branch.js"; | 
					
						
							|  |  |  | import NoteShort from "../entities/note_short.js"; | 
					
						
							| 
									
										
										
										
											2018-03-25 12:29:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TreeCache { | 
					
						
							|  |  |  |     load(noteRows, branchRows) { | 
					
						
							|  |  |  |         this.parents = []; | 
					
						
							|  |  |  |         this.children = []; | 
					
						
							|  |  |  |         this.childParentToBranch = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @type {Object.<string, NoteShort>} */ | 
					
						
							|  |  |  |         this.notes = {}; | 
					
						
							|  |  |  |         for (const noteRow of noteRows) { | 
					
						
							|  |  |  |             const note = new NoteShort(this, noteRow); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.notes[note.noteId] = note; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @type {Object.<string, Branch>} */ | 
					
						
							|  |  |  |         this.branches = {}; | 
					
						
							|  |  |  |         for (const branchRow of branchRows) { | 
					
						
							|  |  |  |             const branch = new Branch(this, branchRow); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.addBranch(branch); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     /** @return NoteShort */ | 
					
						
							|  |  |  |     async getNote(noteId) { | 
					
						
							| 
									
										
										
										
											2018-03-25 12:29:00 -04:00
										 |  |  |         return this.notes[noteId]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     addBranch(branch) { | 
					
						
							|  |  |  |         this.branches[branch.branchId] = branch; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.parents[branch.noteId] = this.parents[branch.noteId] || []; | 
					
						
							|  |  |  |         this.parents[branch.noteId].push(this.notes[branch.parentNoteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.children[branch.parentNoteId] = this.children[branch.parentNoteId] || []; | 
					
						
							|  |  |  |         this.children[branch.parentNoteId].push(this.notes[branch.noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.childParentToBranch[branch.noteId + '-' + branch.parentNoteId] = branch; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     add(note, branch) { | 
					
						
							|  |  |  |         this.notes[note.noteId] = note; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.addBranch(branch); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     /** @return Branch */ | 
					
						
							|  |  |  |     async getBranch(branchId) { | 
					
						
							| 
									
										
										
										
											2018-03-25 12:29:00 -04:00
										 |  |  |         return this.branches[branchId]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     /** @return Branch */ | 
					
						
							|  |  |  |     async getBranchByChildParent(childNoteId, parentNoteId) { | 
					
						
							| 
									
										
										
										
											2018-03-25 12:29:00 -04:00
										 |  |  |         const key = (childNoteId + '-' + parentNoteId); | 
					
						
							|  |  |  |         const branch = this.childParentToBranch[key]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!branch) { | 
					
						
							|  |  |  |             utils.throwError("Cannot find branch for child-parent=" + key); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return branch; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const treeCache = new TreeCache(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default treeCache; |