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

View File

@ -1,12 +1,13 @@
"use strict"; "use strict";
const BNote = require('./bnote.js'); import BNote = require('./bnote.js');
const AbstractBeccaEntity = require('./abstract_becca_entity.js'); import AbstractBeccaEntity = require('./abstract_becca_entity.js');
const dateUtils = require('../../services/date_utils'); import dateUtils = require('../../services/date_utils');
const utils = require('../../services/utils'); import utils = require('../../services/utils');
const TaskContext = require('../../services/task_context.js'); import TaskContext = require('../../services/task_context');
const cls = require('../../services/cls'); import cls = require('../../services/cls');
const log = require('../../services/log'); 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 * 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 // 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"]; } 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(); super();
if (!row) { if (!row) {
@ -34,7 +43,7 @@ class BBranch extends AbstractBeccaEntity {
this.init(); this.init();
} }
updateFromRow(row) { updateFromRow(row: BranchRow) {
this.update([ this.update([
row.branchId, row.branchId,
row.noteId, row.noteId,
@ -46,20 +55,13 @@ class BBranch extends AbstractBeccaEntity {
]); ]);
} }
update([branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified]) { update([branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified]: any) {
/** @type {string} */
this.branchId = branchId; this.branchId = branchId;
/** @type {string} */
this.noteId = noteId; this.noteId = noteId;
/** @type {string} */
this.parentNoteId = parentNoteId; this.parentNoteId = parentNoteId;
/** @type {string|null} */
this.prefix = prefix; this.prefix = prefix;
/** @type {int} */
this.notePosition = notePosition; this.notePosition = notePosition;
/** @type {boolean} */
this.isExpanded = !!isExpanded; this.isExpanded = !!isExpanded;
/** @type {string} */
this.utcDateModified = utcDateModified; this.utcDateModified = utcDateModified;
return this; 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. * Delete a branch. If this is a last note's branch, delete the note as well.
* *
* @param {string} [deleteId] - optional delete identified * @param deleteId - optional delete identified
* @param {TaskContext} [taskContext]
* *
* @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) { if (!deleteId) {
deleteId = utils.randomString(10); 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); const existingBranch = this.becca.getBranchFromChildAndParent(this.noteId, parentNoteId);
if (existingBranch) { 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; value: string;
isInheritable: boolean; isInheritable: boolean;
utcDateModified: string; 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"; "use strict";
const ws = require('./ws.js'); import ws = require('./ws.js');
// taskId => TaskContext // taskId => TaskContext
const taskContexts = {}; const taskContexts: Record<string, TaskContext> = {};
class 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.taskId = taskId;
this.taskType = taskType; this.taskType = taskType;
this.data = data; this.data = data;
@ -23,8 +31,7 @@ class TaskContext {
this.increaseProgressCount(); this.increaseProgressCount();
} }
/** @returns {TaskContext} */ static getInstance(taskId: string, taskType: string, data: {} | null = null): TaskContext {
static getInstance(taskId, taskType, data = null) {
if (!taskContexts[taskId]) { if (!taskContexts[taskId]) {
taskContexts[taskId] = new TaskContext(taskId, taskType, data); taskContexts[taskId] = new TaskContext(taskId, taskType, data);
} }
@ -42,31 +49,31 @@ class TaskContext {
type: 'taskProgressCount', type: 'taskProgressCount',
taskId: this.taskId, taskId: this.taskId,
taskType: this.taskType, taskType: this.taskType,
data: this.data, data: this.data || undefined,
progressCount: this.progressCount progressCount: this.progressCount
}); });
} }
} }
reportError(message) { reportError(message: string) {
ws.sendMessageToAllClients({ ws.sendMessageToAllClients({
type: 'taskError', type: 'taskError',
taskId: this.taskId, taskId: this.taskId,
taskType: this.taskType, taskType: this.taskType,
data: this.data, data: this.data || undefined,
message: message message: message
}); });
} }
taskSucceeded(result) { taskSucceeded(result: string) {
ws.sendMessageToAllClients({ ws.sendMessageToAllClients({
type: 'taskSucceeded', type: 'taskSucceeded',
taskId: this.taskId, taskId: this.taskId,
taskType: this.taskType, taskType: this.taskType,
data: this.data, data: this.data || undefined,
result: result result: result
}); });
} }
} }
module.exports = TaskContext; export = TaskContext;

View File

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