messaging now uses mutex so each component processes only single message at each time

This commit is contained in:
zadam 2020-02-12 20:07:04 +01:00
parent 1d2fc773c2
commit 81a54cd4a0
5 changed files with 55 additions and 11 deletions

6
package-lock.json generated
View file

@ -8826,9 +8826,9 @@
}
},
"semver": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.1.2.tgz",
"integrity": "sha512-BJs9T/H8sEVHbeigqzIEo57Iu/3DG6c4QoqTfbQB3BPA4zgzAomh/Fk9E7QtjWQ8mx2dgA9YCfSF4y9k9bHNpQ=="
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.1.3.tgz",
"integrity": "sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA=="
},
"semver-compare": {
"version": "1.0.0",

View file

@ -61,7 +61,7 @@
"rimraf": "3.0.2",
"sanitize-filename": "1.6.3",
"sax": "1.2.4",
"semver": "7.1.2",
"semver": "7.1.3",
"serve-favicon": "2.5.0",
"session-file-store": "1.4.0",
"simple-node-logger": "18.12.24",

View file

@ -0,0 +1,29 @@
export default class Mutex {
constructor() {
this.queue = [];
this.pending = false;
}
isLocked() {
return this.pending;
}
acquire() {
const ticket = new Promise(resolve => this.queue.push(resolve));
if (!this.pending) {
this.dispatchNext();
}
return ticket;
}
dispatchNext() {
if (this.queue.length > 0) {
this.pending = true;
this.queue.shift()(this.dispatchNext.bind(this));
} else {
this.pending = false;
}
}
}

View file

@ -1,4 +1,5 @@
import utils from '../services/utils.js';
import Mutex from "../services/mutex.js";
export default class Component {
/** @param {AppContext} appContext */
@ -11,6 +12,7 @@ export default class Component {
/** @type Component[] */
this.children = [];
this.initialized = Promise.resolve();
this.mutex = new Mutex();
}
async eventReceived(name, data, sync = false) {
@ -23,7 +25,18 @@ export default class Component {
const start = Date.now();
if (typeof fun === 'function') {
propagateToChildren = await fun.call(this, data) !== false;
let release;
try {
release = await this.mutex.acquire();
propagateToChildren = await fun.call(this, data) !== false;
}
finally {
if (release) {
release();
}
}
}
const end = Date.now();
@ -46,12 +59,14 @@ export default class Component {
}
async triggerChildren(name, data, sync = false) {
for (const child of this.children) {
let promise = child.eventReceived(name, data, sync);
const promises = [];
if (sync) {
await promise;
}
for (const child of this.children) {
promises.push(child.eventReceived(name, data, sync));
}
if (sync) {
await Promise.all(promises);
}
}
}

View file

@ -372,7 +372,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
async updateNode(node) {
const note = await treeCache.getNote(node.data.noteId);
const note = treeCache.getNoteFromCache(node.data.noteId);
const branch = treeCache.getBranch(node.data.branchId);
node.data.isProtected = note.isProtected;