server-ts: Port bbranch (with some build errors)

This commit is contained in:
Elian Doran 2024-02-17 02:02:08 +02:00
parent cf18e61a33
commit f51f070b2f
No known key found for this signature in database
5 changed files with 64 additions and 40 deletions

View File

@ -5,6 +5,7 @@ import BOption = require('./entities/boption');
import BNote = require('./entities/bnote');
import BEtapiToken = require('./entities/betapi_token');
import BAttribute = require('./entities/battribute');
import BBranch = require('./entities/bbranch');
/**
* Becca is a backend cache of all notes, branches, and attributes.
@ -14,6 +15,8 @@ class Becca {
loaded!: boolean;
notes!: Record<string, BNote>;
branches!: Record<string, BBranch>;
childParentToBranch!: Record<string, BBranch>;
attributes!: Record<string, BAttribute>;
/** Points from attribute type-name to list of attributes */
attributeIndex!: Record<string, BAttribute[]>;
@ -25,11 +28,8 @@ class Becca {
}
reset() {
/** @type {Object.<String, BNote>} */
this.notes = {};
/** @type {Object.<String, BBranch>} */
this.branches = {};
/** @type {Object.<String, BBranch>} */
this.childParentToBranch = {};
this.attributes = {};
this.attributeIndex = {};

View File

@ -1,12 +1,13 @@
"use strict";
const BNote = require('./bnote.js');
const AbstractBeccaEntity = require('./abstract_becca_entity.js');
const dateUtils = require('../../services/date_utils');
const utils = require('../../services/utils');
const TaskContext = require('../../services/task_context.js');
const cls = require('../../services/cls');
const log = require('../../services/log');
import BNote = require('./bnote.js');
import AbstractBeccaEntity = require('./abstract_becca_entity.js');
import dateUtils = require('../../services/date_utils');
import utils = require('../../services/utils');
import TaskContext = require('../../services/task_context');
import cls = require('../../services/cls');
import log = require('../../services/log');
import { BranchRow } from './rows.js';
/**
* Branch represents a relationship between a child note and its parent note. Trilium allows a note to have multiple
@ -23,7 +24,15 @@ class BBranch extends AbstractBeccaEntity {
// notePosition is not part of hash because it would produce a lot of updates in case of reordering
static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "prefix"]; }
constructor(row) {
branchId?: string;
noteId!: string;
parentNoteId!: string;
prefix!: string;
notePosition!: number;
isExpanded!: boolean;
utcDateModified?: string;
constructor(row: BranchRow) {
super();
if (!row) {
@ -34,7 +43,7 @@ class BBranch extends AbstractBeccaEntity {
this.init();
}
updateFromRow(row) {
updateFromRow(row: BranchRow) {
this.update([
row.branchId,
row.noteId,
@ -46,20 +55,13 @@ class BBranch extends AbstractBeccaEntity {
]);
}
update([branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified]) {
/** @type {string} */
update([branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified]: any) {
this.branchId = branchId;
/** @type {string} */
this.noteId = noteId;
/** @type {string} */
this.parentNoteId = parentNoteId;
/** @type {string|null} */
this.prefix = prefix;
/** @type {int} */
this.notePosition = notePosition;
/** @type {boolean} */
this.isExpanded = !!isExpanded;
/** @type {string} */
this.utcDateModified = utcDateModified;
return this;
@ -138,12 +140,11 @@ class BBranch extends AbstractBeccaEntity {
/**
* Delete a branch. If this is a last note's branch, delete the note as well.
*
* @param {string} [deleteId] - optional delete identified
* @param {TaskContext} [taskContext]
* @param deleteId - optional delete identified
*
* @returns {boolean} - true if note has been deleted, false otherwise
* @returns true if note has been deleted, false otherwise
*/
deleteBranch(deleteId, taskContext) {
deleteBranch(deleteId: string, taskContext: TaskContext): boolean {
if (!deleteId) {
deleteId = utils.randomString(10);
}
@ -261,7 +262,7 @@ class BBranch extends AbstractBeccaEntity {
};
}
createClone(parentNoteId, notePosition) {
createClone(parentNoteId: string, notePosition: number) {
const existingBranch = this.becca.getBranchFromChildAndParent(this.noteId, parentNoteId);
if (existingBranch) {
@ -279,4 +280,4 @@ class BBranch extends AbstractBeccaEntity {
}
}
module.exports = BBranch;
export = BBranch;

View File

@ -72,4 +72,14 @@ export interface AttributeRow {
value: string;
isInheritable: boolean;
utcDateModified: string;
}
export interface BranchRow {
branchId?: string;
noteId: string;
parentNoteId: string;
prefix: string | null;
notePosition: number;
isExpanded: boolean;
utcDateModified?: string;
}

View File

@ -1,12 +1,20 @@
"use strict";
const ws = require('./ws.js');
import ws = require('./ws.js');
// taskId => TaskContext
const taskContexts = {};
const taskContexts: Record<string, TaskContext> = {};
class TaskContext {
constructor(taskId, taskType = null, data = {}) {
private taskId: string;
private taskType: string | null;
private data: {} | null;
private noteDeletionHandlerTriggered: boolean;
private progressCount: number;
private lastSentCountTs: number;
constructor(taskId: string, taskType: string | null = null, data: {} | null = {}) {
this.taskId = taskId;
this.taskType = taskType;
this.data = data;
@ -23,8 +31,7 @@ class TaskContext {
this.increaseProgressCount();
}
/** @returns {TaskContext} */
static getInstance(taskId, taskType, data = null) {
static getInstance(taskId: string, taskType: string, data: {} | null = null): TaskContext {
if (!taskContexts[taskId]) {
taskContexts[taskId] = new TaskContext(taskId, taskType, data);
}
@ -42,31 +49,31 @@ class TaskContext {
type: 'taskProgressCount',
taskId: this.taskId,
taskType: this.taskType,
data: this.data,
data: this.data || undefined,
progressCount: this.progressCount
});
}
}
reportError(message) {
reportError(message: string) {
ws.sendMessageToAllClients({
type: 'taskError',
taskId: this.taskId,
taskType: this.taskType,
data: this.data,
data: this.data || undefined,
message: message
});
}
taskSucceeded(result) {
taskSucceeded(result: string) {
ws.sendMessageToAllClients({
type: 'taskSucceeded',
taskId: this.taskId,
taskType: this.taskType,
data: this.data,
data: this.data || undefined,
result: result
});
}
}
module.exports = TaskContext;
export = TaskContext;

View File

@ -28,12 +28,18 @@ let lastSyncedPush: number | null = null;
interface Message {
type: string;
reason?: string;
data?: {
lastSyncedPush?: number,
entityChanges?: any[]
},
lastSyncedPush?: number
lastSyncedPush?: number,
progressCount?: number;
taskId?: string;
taskType?: string | null;
message?: string;
reason?: string;
result?: string;
}
type SessionParser = (req: IncomingMessage, params: {}, cb: () => void) => void;
@ -252,7 +258,7 @@ function setLastSyncedPush(entityChangeId: number) {
lastSyncedPush = entityChangeId;
}
module.exports = {
export = {
init,
sendMessageToAllClients,
syncPushInProgress,