mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	becca conversion WIP
This commit is contained in:
		
							parent
							
								
									e41be520a8
								
							
						
					
					
						commit
						ccadff5015
					
				| @ -1,39 +0,0 @@ | |||||||
| "use strict"; |  | ||||||
| 
 |  | ||||||
| const Entity = require('./entity'); |  | ||||||
| const dateUtils = require('../services/date_utils'); |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Option represents name-value pair, either directly configurable by the user or some system property. |  | ||||||
|  * |  | ||||||
|  * @property {string} name |  | ||||||
|  * @property {string} value |  | ||||||
|  * @property {boolean} isSynced |  | ||||||
|  * @property {string} utcDateModified |  | ||||||
|  * @property {string} utcDateCreated |  | ||||||
|  * |  | ||||||
|  * @extends Entity |  | ||||||
|  */ |  | ||||||
| class Option extends Entity { |  | ||||||
|     static get entityName() { return "options"; } |  | ||||||
|     static get primaryKeyName() { return "name"; } |  | ||||||
|     static get hashedProperties() { return ["name", "value"]; } |  | ||||||
| 
 |  | ||||||
|     constructor(row) { |  | ||||||
|         super(row); |  | ||||||
| 
 |  | ||||||
|         this.isSynced = !!this.isSynced; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     beforeSaving() { |  | ||||||
|         if (!this.utcDateCreated) { |  | ||||||
|             this.utcDateCreated = dateUtils.utcNowDateTime(); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         super.beforeSaving(); |  | ||||||
| 
 |  | ||||||
|         this.utcDateModified = dateUtils.utcNowDateTime(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| module.exports = Option; |  | ||||||
| @ -10,15 +10,17 @@ class Becca { | |||||||
| 
 | 
 | ||||||
|     reset() { |     reset() { | ||||||
|         /** @type {Object.<String, Note>} */ |         /** @type {Object.<String, Note>} */ | ||||||
|         this.notes = []; |         this.notes = {}; | ||||||
|         /** @type {Object.<String, Branch>} */ |         /** @type {Object.<String, Branch>} */ | ||||||
|         this.branches = []; |         this.branches = {}; | ||||||
|         /** @type {Object.<String, Branch>} */ |         /** @type {Object.<String, Branch>} */ | ||||||
|         this.childParentToBranch = {}; |         this.childParentToBranch = {}; | ||||||
|         /** @type {Object.<String, Attribute>} */ |         /** @type {Object.<String, Attribute>} */ | ||||||
|         this.attributes = []; |         this.attributes = {}; | ||||||
|         /** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes */ |         /** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes */ | ||||||
|         this.attributeIndex = {}; |         this.attributeIndex = {}; | ||||||
|  |         /** @type {Object.<String, Option>} */ | ||||||
|  |         this.options = {}; | ||||||
| 
 | 
 | ||||||
|         this.loaded = false; |         this.loaded = false; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -8,6 +8,7 @@ const log = require('../log'); | |||||||
| const Note = require('./entities/note'); | const Note = require('./entities/note'); | ||||||
| const Branch = require('./entities/branch'); | const Branch = require('./entities/branch'); | ||||||
| const Attribute = require('./entities/attribute'); | const Attribute = require('./entities/attribute'); | ||||||
|  | const Option = require('./entities/option'); | ||||||
| 
 | 
 | ||||||
| sqlInit.dbReady.then(() => { | sqlInit.dbReady.then(() => { | ||||||
|     load(); |     load(); | ||||||
| @ -18,15 +19,19 @@ function load() { | |||||||
|     becca.reset(); |     becca.reset(); | ||||||
| 
 | 
 | ||||||
|     for (const row of sql.iterateRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes`, [])) { |     for (const row of sql.iterateRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes`, [])) { | ||||||
|         new Note(becca, row); |         new Note(row); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     for (const row of sql.iterateRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded FROM branches WHERE isDeleted = 0`, [])) { |     for (const row of sql.iterateRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded FROM branches WHERE isDeleted = 0`, [])) { | ||||||
|         new Branch(becca, row); |         new Branch(row); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     for (const row of sql.iterateRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position FROM attributes WHERE isDeleted = 0`, [])) { |     for (const row of sql.iterateRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position FROM attributes WHERE isDeleted = 0`, [])) { | ||||||
|         new Attribute(becca, row); |         new Attribute(row); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     for (const row of sql.getRows(`SELECT name, value, isSynced, utcDateModified FROM options`)) { | ||||||
|  |         new Option(row); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     becca.loaded = true; |     becca.loaded = true; | ||||||
| @ -34,29 +39,21 @@ function load() { | |||||||
|     log.info(`Becca (note cache) load took ${Date.now() - start}ms`); |     log.info(`Becca (note cache) load took ${Date.now() - start}ms`); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED],  ({entityName, entity}) => { | function updateNote(entity) { | ||||||
|     // note that entity can also be just POJO without methods if coming from sync
 |  | ||||||
| 
 |  | ||||||
|     if (!becca.loaded) { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     if (entityName === 'notes') { |  | ||||||
|     const {noteId} = entity; |     const {noteId} = entity; | ||||||
| 
 | 
 | ||||||
|     if (entity.isDeleted) { |     if (entity.isDeleted) { | ||||||
|         delete becca.notes[noteId]; |         delete becca.notes[noteId]; | ||||||
|         } |     } else if (noteId in becca.notes) { | ||||||
|         else if (noteId in becca.notes) { |  | ||||||
|         becca.notes[noteId].update(entity); |         becca.notes[noteId].update(entity); | ||||||
|         } |     } else { | ||||||
|         else { |         const note = new Note(entity); | ||||||
|             const note = new Note(becca, entity); |  | ||||||
| 
 | 
 | ||||||
|         note.decrypt(); |         note.decrypt(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|     else if (entityName === 'branches') { | 
 | ||||||
|  | function updateBranch(entity) { | ||||||
|     const {branchId, noteId, parentNoteId} = entity; |     const {branchId, noteId, parentNoteId} = entity; | ||||||
|     const childNote = becca.notes[noteId]; |     const childNote = becca.notes[noteId]; | ||||||
| 
 | 
 | ||||||
| @ -78,8 +75,7 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
| 
 | 
 | ||||||
|         delete becca.childParentToBranch[`${noteId}-${parentNoteId}`]; |         delete becca.childParentToBranch[`${noteId}-${parentNoteId}`]; | ||||||
|         delete becca.branches[branchId]; |         delete becca.branches[branchId]; | ||||||
|         } |     } else if (branchId in becca.branches) { | ||||||
|         else if (branchId in becca.branches) { |  | ||||||
|         // only relevant properties which can change in a branch are prefix and isExpanded
 |         // only relevant properties which can change in a branch are prefix and isExpanded
 | ||||||
|         becca.branches[branchId].prefix = entity.prefix; |         becca.branches[branchId].prefix = entity.prefix; | ||||||
|         becca.branches[branchId].isExpanded = entity.isExpanded; |         becca.branches[branchId].isExpanded = entity.isExpanded; | ||||||
| @ -87,16 +83,16 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
|         if (childNote) { |         if (childNote) { | ||||||
|             childNote.flatTextCache = null; |             childNote.flatTextCache = null; | ||||||
|         } |         } | ||||||
|         } |     } else { | ||||||
|         else { |         becca.branches[branchId] = new Branch(entity); | ||||||
|             becca.branches[branchId] = new Branch(becca, entity); |  | ||||||
| 
 | 
 | ||||||
|         if (childNote) { |         if (childNote) { | ||||||
|             childNote.resortParents(); |             childNote.resortParents(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|     else if (entityName === 'attributes') { | 
 | ||||||
|  | function updateAttribute(entity) { | ||||||
|     const {attributeId, noteId} = entity; |     const {attributeId, noteId} = entity; | ||||||
|     const note = becca.notes[noteId]; |     const note = becca.notes[noteId]; | ||||||
|     const attr = becca.attributes[attributeId]; |     const attr = becca.attributes[attributeId]; | ||||||
| @ -128,8 +124,7 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
|                 becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attributeId); |                 becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attributeId); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         } |     } else if (attributeId in becca.attributes) { | ||||||
|         else if (attributeId in becca.attributes) { |  | ||||||
|         const attr = becca.attributes[attributeId]; |         const attr = becca.attributes[attributeId]; | ||||||
| 
 | 
 | ||||||
|         // attr name and isInheritable are immutable
 |         // attr name and isInheritable are immutable
 | ||||||
| @ -137,25 +132,23 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
| 
 | 
 | ||||||
|         if (attr.isAffectingSubtree || note.isTemplate) { |         if (attr.isAffectingSubtree || note.isTemplate) { | ||||||
|             note.invalidateSubtreeFlatText(); |             note.invalidateSubtreeFlatText(); | ||||||
|             } |         } else { | ||||||
|             else { |  | ||||||
|             note.invalidateThisCache(); |             note.invalidateThisCache(); | ||||||
|         } |         } | ||||||
|         } |     } else { | ||||||
|         else { |         const attr = new Attribute(entity); | ||||||
|             const attr = new Attribute(becca, entity); |  | ||||||
| 
 | 
 | ||||||
|         if (note) { |         if (note) { | ||||||
|             if (attr.isAffectingSubtree || note.isTemplate) { |             if (attr.isAffectingSubtree || note.isTemplate) { | ||||||
|                 note.invalidateSubTree(); |                 note.invalidateSubTree(); | ||||||
|                 } |             } else { | ||||||
|                 else { |  | ||||||
|                 note.invalidateThisCache(); |                 note.invalidateThisCache(); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|     else if (entityName === 'note_reordering') { | 
 | ||||||
|  | function updateNoteReordering(entity) { | ||||||
|     const parentNoteIds = new Set(); |     const parentNoteIds = new Set(); | ||||||
| 
 | 
 | ||||||
|     for (const branchId in entity) { |     for (const branchId in entity) { | ||||||
| @ -168,6 +161,29 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED],  ({entityName, entity}) => { | ||||||
|  |     // note that entity can also be just POJO without methods if coming from sync
 | ||||||
|  | 
 | ||||||
|  |     if (!becca.loaded) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (entityName === 'notes') { | ||||||
|  |         updateNote(entity); | ||||||
|  |     } | ||||||
|  |     else if (entityName === 'branches') { | ||||||
|  |         updateBranch(entity); | ||||||
|  |     } | ||||||
|  |     else if (entityName === 'attributes') { | ||||||
|  |         updateAttribute(entity); | ||||||
|  |     } | ||||||
|  |     else if (entityName === 'options') { | ||||||
|  |         // nothing needed
 | ||||||
|  |     } | ||||||
|  |     else if (entityName === 'note_reordering') { | ||||||
|  |         updateNoteReordering(entity); | ||||||
|  |     } | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { | eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { | ||||||
|  | |||||||
							
								
								
									
										32
									
								
								src/services/becca/entities/option.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/services/becca/entities/option.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,32 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | const dateUtils = require('../../date_utils.js'); | ||||||
|  | const AbstractEntity = require("./abstract_entity.js"); | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Option represents name-value pair, either directly configurable by the user or some system property. | ||||||
|  |  */ | ||||||
|  | class Option extends AbstractEntity { | ||||||
|  |     static get entityName() { return "options"; } | ||||||
|  |     static get primaryKeyName() { return "name"; } | ||||||
|  |     static get hashedProperties() { return ["name", "value"]; } | ||||||
|  | 
 | ||||||
|  |     constructor(row) { | ||||||
|  |         super(); | ||||||
|  | 
 | ||||||
|  |         this.name = row.name; | ||||||
|  |         this.value = row.value; | ||||||
|  |         this.isSynced = !!row.isSynced; | ||||||
|  |         this.utcDateModified = row.utcDateModified; | ||||||
|  | 
 | ||||||
|  |         this.becca.options[this.name] = this; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     beforeSaving() { | ||||||
|  |         super.beforeSaving(); | ||||||
|  | 
 | ||||||
|  |         this.utcDateModified = dateUtils.utcNowDateTime(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = Option; | ||||||
| @ -1,5 +1,5 @@ | |||||||
| function getOption(name) { | function getOption(name) { | ||||||
|     const option = require('./repository').getOption(name); |     const option = require('./becca').getOption(name); | ||||||
| 
 | 
 | ||||||
|     if (!option) { |     if (!option) { | ||||||
|         throw new Error(`Option ${name} doesn't exist`); |         throw new Error(`Option ${name} doesn't exist`); | ||||||
| @ -37,7 +37,7 @@ function getOptionBool(name) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function setOption(name, value) { | function setOption(name, value) { | ||||||
|     const option = require('./repository').getOption(name); |     const option = require('./becca').getOption(name); | ||||||
| 
 | 
 | ||||||
|     if (value === true || value === false) { |     if (value === true || value === false) { | ||||||
|         value = value.toString(); |         value = value.toString(); | ||||||
| @ -55,7 +55,7 @@ function setOption(name, value) { | |||||||
| 
 | 
 | ||||||
| function createOption(name, value, isSynced) { | function createOption(name, value, isSynced) { | ||||||
|     // to avoid circular dependency, need to find better solution
 |     // to avoid circular dependency, need to find better solution
 | ||||||
|     const Option = require('../entities/option'); |     const Option = require('../services/becca/entities/option'); | ||||||
| 
 | 
 | ||||||
|     new Option({ |     new Option({ | ||||||
|         name: name, |         name: name, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam