445 lines
14 KiB
JavaScript
Raw Normal View History

import utils from './utils.js';
2019-10-20 10:00:18 +02:00
import toastService from "./toast.js";
import server from "./server.js";
2020-02-05 22:46:20 +01:00
import LoadResults from "./load_results.js";
import Branch from "../entities/branch.js";
import Attribute from "../entities/attribute.js";
import options from "./options.js";
2021-04-16 23:01:56 +02:00
import froca from "./froca.js";
import noteAttributeCache from "./note_attribute_cache.js";
const messageHandlers = [];
let ws;
let lastAcceptedEntityChangeId = window.glob.maxEntityChangeIdAtLoad;
2021-03-21 22:43:41 +01:00
let lastAcceptedEntityChangeSyncId = window.glob.maxEntityChangeSyncIdAtLoad;
let lastProcessedEntityChangeId = window.glob.maxEntityChangeIdAtLoad;
let lastPingTs;
2021-03-21 22:43:41 +01:00
let frontendUpdateDataQueue = [];
function logError(message) {
console.error(utils.now(), message); // needs to be separate from .trace()
if (ws && ws.readyState === 1) {
ws.send(JSON.stringify({
type: 'log-error',
error: message,
stack: new Error().stack
}));
}
}
2017-12-17 13:46:18 -05:00
window.logError = logError;
function subscribeToMessages(messageHandler) {
messageHandlers.push(messageHandler);
}
2021-03-21 22:43:41 +01:00
// used to serialize frontend update operations
let consumeQueuePromise = null;
// to make sure each change event is processed only once. Not clear if this is still necessary
const processedEntityChangeIds = new Set();
2020-12-14 14:17:51 +01:00
function logRows(entityChanges) {
const filteredRows = entityChanges.filter(row =>
!processedEntityChangeIds.has(row.id)
&& (row.entityName !== 'options' || row.entityId !== 'openTabs'));
if (filteredRows.length > 0) {
2021-03-21 22:43:41 +01:00
console.debug(utils.now(), "Frontend update data: ", filteredRows);
}
}
async function handleMessage(event) {
const message = JSON.parse(event.data);
for (const messageHandler of messageHandlers) {
messageHandler(message);
}
2021-03-21 22:43:41 +01:00
if (message.type === 'frontend-update') {
2021-04-24 11:39:44 +02:00
let {entityChanges} = message.data;
2019-02-10 16:36:25 +01:00
lastPingTs = Date.now();
2020-12-14 14:17:51 +01:00
if (entityChanges.length > 0) {
logRows(entityChanges);
2021-03-21 22:43:41 +01:00
frontendUpdateDataQueue.push(...entityChanges);
2021-03-21 22:43:41 +01:00
// we set lastAcceptedEntityChangeId even before frontend update processing and send ping so that backend can start sending more updates
2020-12-14 14:17:51 +01:00
lastAcceptedEntityChangeId = Math.max(lastAcceptedEntityChangeId, entityChanges[entityChanges.length - 1].id);
2021-03-21 22:43:41 +01:00
const lastSyncEntityChange = entityChanges.slice().reverse().find(ec => ec.isSynced);
if (lastSyncEntityChange) {
lastAcceptedEntityChangeSyncId = Math.max(lastAcceptedEntityChangeSyncId, lastSyncEntityChange.id);
}
2019-12-16 22:47:07 +01:00
sendPing();
// first wait for all the preceding consumers to finish
while (consumeQueuePromise) {
await consumeQueuePromise;
}
2019-12-16 22:00:44 +01:00
try {
// it's my turn so start it up
2021-03-21 22:43:41 +01:00
consumeQueuePromise = consumeFrontendUpdateData();
2019-12-16 22:00:44 +01:00
await consumeQueuePromise;
}
finally {
// finish and set to null to signal somebody else can pick it up
consumeQueuePromise = null;
}
}
}
else if (message.type === 'sync-hash-check-failed') {
2019-10-20 10:00:18 +02:00
toastService.showError("Sync check failed!", 60000);
}
else if (message.type === 'consistency-checks-failed') {
2019-10-20 10:00:18 +02:00
toastService.showError("Consistency checks failed! See logs for details.", 50 * 60000);
}
}
let entityChangeIdReachedListeners = [];
function waitForEntityChangeId(desiredEntityChangeId) {
if (desiredEntityChangeId <= lastProcessedEntityChangeId) {
return Promise.resolve();
}
console.debug(`Waiting for ${desiredEntityChangeId}, last processed is ${lastProcessedEntityChangeId}, last accepted ${lastAcceptedEntityChangeId}`);
return new Promise((res, rej) => {
entityChangeIdReachedListeners.push({
desiredEntityChangeId: desiredEntityChangeId,
resolvePromise: res,
start: Date.now()
})
});
}
function waitForMaxKnownEntityChangeId() {
return waitForEntityChangeId(server.getMaxKnownEntityChangeId());
}
function checkEntityChangeIdListeners() {
entityChangeIdReachedListeners
.filter(l => l.desiredEntityChangeId <= lastProcessedEntityChangeId)
2019-10-28 19:45:36 +01:00
.forEach(l => l.resolvePromise());
entityChangeIdReachedListeners = entityChangeIdReachedListeners
.filter(l => l.desiredEntityChangeId > lastProcessedEntityChangeId);
2019-10-28 19:45:36 +01:00
entityChangeIdReachedListeners.filter(l => Date.now() > l.start - 60000)
.forEach(l => console.log(`Waiting for entityChangeId ${l.desiredEntityChangeId} while last processed is ${lastProcessedEntityChangeId} (last accepted ${lastAcceptedEntityChangeId}) for ${Math.floor((Date.now() - l.start) / 1000)}s`));
2019-10-28 19:45:36 +01:00
}
2021-03-21 22:43:41 +01:00
async function consumeFrontendUpdateData() {
if (frontendUpdateDataQueue.length > 0) {
const allEntityChanges = frontendUpdateDataQueue;
frontendUpdateDataQueue = [];
const nonProcessedEntityChanges = allEntityChanges.filter(ec => !processedEntityChangeIds.has(ec.id));
2019-12-16 22:47:07 +01:00
try {
2020-12-14 14:17:51 +01:00
await utils.timeLimit(processEntityChanges(nonProcessedEntityChanges), 30000);
2019-12-16 22:47:07 +01:00
}
catch (e) {
logError(`Encountered error ${e.message}: ${e.stack}, reloading frontend.`);
2020-09-18 23:22:28 +02:00
if (!glob.isDev && !options.is('debugModeEnabled')) {
// if there's an error in updating the frontend then the easy option to recover is to reload the frontend completely
utils.reloadApp();
}
2020-09-18 23:22:28 +02:00
else {
2020-12-14 14:17:51 +01:00
console.log("nonProcessedEntityChanges causing the timeout", nonProcessedEntityChanges);
2020-09-19 22:47:14 +02:00
alert(`Encountered error "${e.message}", check out the console.`);
2020-09-18 23:22:28 +02:00
}
2019-12-16 22:47:07 +01:00
}
2020-12-14 14:17:51 +01:00
for (const entityChange of nonProcessedEntityChanges) {
processedEntityChangeIds.add(entityChange.id);
}
2020-12-14 14:17:51 +01:00
lastProcessedEntityChangeId = Math.max(lastProcessedEntityChangeId, allEntityChanges[allEntityChanges.length - 1].id);
}
checkEntityChangeIdListeners();
}
function connectWebSocket() {
2019-11-25 21:44:46 +01:00
const loc = window.location;
const webSocketUri = (loc.protocol === "https:" ? "wss:" : "ws:")
+ "//" + loc.host + loc.pathname;
// use wss for secure messaging
2019-11-25 21:44:46 +01:00
const ws = new WebSocket(webSocketUri);
ws.onopen = () => console.debug(utils.now(), `Connected to server ${webSocketUri} with WebSocket`);
ws.onmessage = handleMessage;
2019-07-06 12:03:51 +02:00
// we're not handling ws.onclose here because reconnection is done in sendPing()
return ws;
}
async function sendPing() {
if (Date.now() - lastPingTs > 30000) {
console.log(utils.now(), "Lost websocket connection to the backend. If you keep having this issue repeatedly, you might want to check your reverse proxy (nginx, apache) configuration and allow/unblock WebSocket.");
}
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify({
type: 'ping',
lastEntityChangeId: lastAcceptedEntityChangeId
}));
}
else if (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING) {
console.log(utils.now(), "WS closed or closing, trying to reconnect");
ws = connectWebSocket();
}
}
setTimeout(() => {
ws = connectWebSocket();
2019-02-10 16:36:25 +01:00
lastPingTs = Date.now();
setInterval(sendPing, 1000);
2018-04-05 23:17:19 -04:00
}, 0);
2017-12-01 22:28:22 -05:00
2020-12-14 14:17:51 +01:00
async function processEntityChanges(entityChanges) {
2021-04-16 22:57:37 +02:00
const loadResults = new LoadResults(froca);
2020-02-05 22:46:20 +01:00
for (const ec of entityChanges.filter(ec => ec.entityName === 'notes')) {
2021-08-14 10:13:59 +02:00
try {
processNoteChange(loadResults, ec);
}
catch (e) {
throw new Error(`Can't process note ${JSON.stringify(ec)} with error ${e.message} ${e.stack}`);
2020-02-05 22:46:20 +01:00
}
}
2020-02-05 22:46:20 +01:00
for (const ec of entityChanges.filter(ec => ec.entityName === 'branches')) {
2021-08-14 10:13:59 +02:00
try {
processBranchChange(loadResults, ec);
2020-02-05 22:46:20 +01:00
}
2021-08-14 10:13:59 +02:00
catch (e) {
throw new Error(`Can't process branch ${JSON.stringify(ec)} with error ${e.message} ${e.stack}`);
2020-02-05 22:46:20 +01:00
}
}
2020-02-05 22:46:20 +01:00
for (const ec of entityChanges.filter(ec => ec.entityName === 'note_reordering')) {
2021-08-14 10:13:59 +02:00
try {
processNoteReordering(loadResults, ec);
}
2021-08-14 10:13:59 +02:00
catch (e) {
throw new Error(`Can't process note reordering ${JSON.stringify(ec)} with error ${e.message} ${e.stack}`);
2020-02-05 22:46:20 +01:00
}
}
2020-02-05 22:46:20 +01:00
// missing reloading the relation target note
for (const ec of entityChanges.filter(ec => ec.entityName === 'attributes')) {
2021-08-14 10:13:59 +02:00
try {
processAttributeChange(loadResults, ec);
2020-02-05 22:46:20 +01:00
}
2021-08-14 10:13:59 +02:00
catch (e) {
throw new Error(`Can't process attribute ${JSON.stringify(ec)} with error ${e.message} ${e.stack}`);
2020-02-05 22:46:20 +01:00
}
}
2020-02-05 22:46:20 +01:00
for (const ec of entityChanges.filter(ec => ec.entityName === 'note_contents')) {
2021-04-16 22:57:37 +02:00
delete froca.noteComplementPromises[ec.entityId];
2020-02-05 22:46:20 +01:00
loadResults.addNoteContent(ec.entityId, ec.sourceId);
}
2020-02-05 22:46:20 +01:00
for (const ec of entityChanges.filter(ec => ec.entityName === 'note_revisions')) {
loadResults.addNoteRevision(ec.entityId, ec.noteId, ec.sourceId);
}
2020-02-05 22:46:20 +01:00
for (const ec of entityChanges.filter(ec => ec.entityName === 'options')) {
if (ec.entity.name === 'openTabs') {
continue; // only noise
}
options.set(ec.entity.name, ec.entity.value);
2020-02-05 22:46:20 +01:00
loadResults.addOption(ec.entity.name);
}
2020-02-05 22:46:20 +01:00
const missingNoteIds = [];
for (const {entityName, entity} of entityChanges) {
2021-08-14 10:13:59 +02:00
if (!entity) { // if erased
continue;
}
2021-04-16 22:57:37 +02:00
if (entityName === 'branches' && !(entity.parentNoteId in froca.notes)) {
missingNoteIds.push(entity.parentNoteId);
}
else if (entityName === 'attributes'
&& entity.type === 'relation'
&& entity.name === 'template'
2021-04-16 22:57:37 +02:00
&& !(entity.value in froca.notes)) {
missingNoteIds.push(entity.value);
}
}
if (missingNoteIds.length > 0) {
2021-04-16 22:57:37 +02:00
await froca.reloadNotes(missingNoteIds);
}
if (!loadResults.isEmpty()) {
if (loadResults.hasAttributeRelatedChanges()) {
noteAttributeCache.invalidate();
}
const appContext = (await import("./app_context.js")).default;
2020-03-18 10:08:16 +01:00
await appContext.triggerEvent('entitiesReloaded', {loadResults});
}
2020-02-05 22:46:20 +01:00
}
2021-08-14 10:13:59 +02:00
function processNoteChange(loadResults, ec) {
const note = froca.notes[ec.entityId];
if (!note) {
return;
}
if (ec.isErased || (ec.entity && ec.isDeleted)) {
delete froca.notes[ec.entityId];
return;
}
note.update(ec.entity);
loadResults.addNote(ec.entityId, ec.sourceId);
}
function processBranchChange(loadResults, ec) {
let branch = froca.branches[ec.entityId];
if (ec.isErased || ec.entity?.isDeleted) {
if (branch) {
const childNote = froca.notes[branch.noteId];
const parentNote = froca.notes[branch.parentNoteId];
if (childNote) {
childNote.parents = childNote.parents.filter(parentNoteId => parentNoteId !== branch.parentNoteId);
delete childNote.parentToBranch[branch.parentNoteId];
}
if (parentNote) {
parentNote.children = parentNote.children.filter(childNoteId => childNoteId !== branch.noteId);
delete parentNote.childToBranch[branch.noteId];
}
delete froca.branches[ec.entityId];
}
return;
}
const childNote = froca.notes[ec.entity.noteId];
const parentNote = froca.notes[ec.entity.parentNoteId];
if (branch) {
branch.update(ec.entity);
}
else if (childNote || parentNote) {
froca.branches[branch.branchId] = branch = new Branch(froca, ec.entity);
}
loadResults.addBranch(ec.entityId, ec.sourceId);
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId);
}
if (parentNote) {
parentNote.addChild(branch.noteId, branch.branchId);
}
}
function processNoteReordering(loadResults, ec) {
const parentNoteIdsToSort = new Set();
for (const branchId in ec.positions) {
const branch = froca.branches[branchId];
if (branch) {
branch.notePosition = ec.positions[branchId];
parentNoteIdsToSort.add(branch.parentNoteId);
}
}
for (const parentNoteId of parentNoteIdsToSort) {
const parentNote = froca.notes[parentNoteId];
if (parentNote) {
parentNote.sortChildren();
}
}
loadResults.addNoteReordering(ec.entityId, ec.sourceId);
}
function processAttributeChange(loadResults, ec) {
let attribute = froca.attributes[ec.entityId];
if (ec.isErased || ec.entity?.isDeleted) {
if (attribute) {
const sourceNote = froca.notes[attribute.noteId];
const targetNote = attribute.type === 'relation' && froca.notes[attribute.value];
if (sourceNote) {
sourceNote.attributes = sourceNote.attributes.filter(attributeId => attributeId !== attribute.attributeId);
}
if (targetNote) {
targetNote.targetRelations = targetNote.targetRelations.filter(attributeId => attributeId !== attribute.attributeId);
}
delete froca.attributes[ec.entityId];
}
return;
}
const sourceNote = froca.notes[ec.entity.noteId];
const targetNote = ec.entity.type === 'relation' && froca.notes[ec.entity.value];
if (attribute) {
attribute.update(ec.entity);
loadResults.addAttribute(ec.entityId, ec.sourceId);
} else if (sourceNote || targetNote) {
attribute = new Attribute(froca, ec.entity);
froca.attributes[attribute.attributeId] = attribute;
loadResults.addAttribute(ec.entityId, ec.sourceId);
if (sourceNote && !sourceNote.attributes.includes(attribute.attributeId)) {
sourceNote.attributes.push(attribute.attributeId);
}
if (targetNote && !targetNote.targetRelations.includes(attribute.attributeId)) {
targetNote.targetRelations.push(attribute.attributeId);
}
}
}
export default {
logError,
subscribeToMessages,
waitForEntityChangeId,
2021-03-21 22:43:41 +01:00
waitForMaxKnownEntityChangeId,
getMaxKnownEntityChangeSyncId: () => lastAcceptedEntityChangeSyncId
};