simplification of sync event API

This commit is contained in:
zadam 2020-02-12 20:31:31 +01:00
parent 81a54cd4a0
commit 25553c9e67
5 changed files with 23 additions and 29 deletions

View File

@ -55,19 +55,23 @@ class AppContext {
this.trigger('initialRenderComplete'); this.trigger('initialRenderComplete');
} }
trigger(name, data, sync = false) { async trigger(name, data) {
this.eventReceived(name, data); this.eventReceived(name, data);
const promises = [];
for (const component of this.components) { for (const component of this.components) {
component.eventReceived(name, data, sync); promises.push(component.eventReceived(name, data));
} }
await Promise.all(promises);
} }
async eventReceived(name, data, sync) { async eventReceived(name, data) {
const fun = this[name + 'Listener']; const fun = this[name + 'Listener'];
if (typeof fun === 'function') { if (typeof fun === 'function') {
await fun.call(this, data, sync); await fun.call(this, data);
} }
} }

View File

@ -38,7 +38,7 @@ class TabContext extends Component {
return; // note is outside of hoisted subtree and user chose not to unhoist return; // note is outside of hoisted subtree and user chose not to unhoist
} }
await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true); await this.trigger('beforeNoteSwitch', {tabId: this.tabId});
utils.closeActiveDialog(); utils.closeActiveDialog();

View File

@ -15,13 +15,11 @@ export default class Component {
this.mutex = new Mutex(); this.mutex = new Mutex();
} }
async eventReceived(name, data, sync = false) { async eventReceived(name, data) {
await this.initialized; await this.initialized;
const fun = this[name + 'Listener']; const fun = this[name + 'Listener'];
let propagateToChildren = true;
const start = Date.now(); const start = Date.now();
if (typeof fun === 'function') { if (typeof fun === 'function') {
@ -30,7 +28,7 @@ export default class Component {
try { try {
release = await this.mutex.acquire(); release = await this.mutex.acquire();
propagateToChildren = await fun.call(this, data) !== false; await fun.call(this, data);
} }
finally { finally {
if (release) { if (release) {
@ -45,28 +43,20 @@ export default class Component {
console.log(`Event ${name} in component ${this.componentId} took ${end-start}ms`); console.log(`Event ${name} in component ${this.componentId} took ${end-start}ms`);
} }
if (propagateToChildren) { await this.triggerChildren(name, data);
const promise = this.triggerChildren(name, data, sync);
if (sync) {
await promise;
}
}
} }
trigger(name, data, sync = false) { async trigger(name, data) {
this.appContext.trigger(name, data, sync); await this.appContext.trigger(name, data);
} }
async triggerChildren(name, data, sync = false) { async triggerChildren(name, data) {
const promises = []; const promises = [];
for (const child of this.children) { for (const child of this.children) {
promises.push(child.eventReceived(name, data, sync)); promises.push(child.eventReceived(name, data));
} }
if (sync) { await Promise.all(promises);
await Promise.all(promises);
}
} }
} }

View File

@ -13,9 +13,9 @@ export default class SidePaneContainer extends FlexContainer {
return super.isEnabled() && options.is(this.side + 'PaneVisible'); return super.isEnabled() && options.is(this.side + 'PaneVisible');
} }
eventReceived(name, data, sync = false) { eventReceived(name, data) {
if (options.is(this.side + 'PaneVisible')) { if (options.is(this.side + 'PaneVisible')) {
super.eventReceived(name, data, sync); super.eventReceived(name, data);
} }
} }

View File

@ -16,12 +16,12 @@ export default class TabCachingWidget extends TabAwareWidget {
return this.$widget = $(`<div class="marker" style="display: none;">`); return this.$widget = $(`<div class="marker" style="display: none;">`);
} }
activeTabChangedListener(param) { async triggerChildren(name, data) {
super.activeTabChangedListener(param);
// stop propagation of the event to the children, individual tab widget should not know about tab switching // stop propagation of the event to the children, individual tab widget should not know about tab switching
// since they are per-tab // since they are per-tab
return false; if (name !== 'activeTabChanged') {
super.triggerChildren(name, data);
}
} }
refreshWithNote() { refreshWithNote() {