diff --git a/CHANGES.md b/CHANGES.md index ad4df0560..5975a926b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ # Changelog ## 5.0.0 (Unreleased) +- Allow for synchronous events. When a synchronous event is fired, Converse will + wait for all promises returned by the event's handlers to finish before continuing. +- Properly handle message correction being received before the corrected message +- Properly handle message correction being received before the corrected message - Groupchat default configuration now supports `list-multi` fields - Bugfix: Don't set `muc_domain` for roomspanel if `locked_muc_domain` is `true`. - Bugfix: Modal auto-closes when you open it for a second time. @@ -15,12 +19,11 @@ - New config setting [muc_show_join_leave_status](https://conversejs.org/docs/html/configuration.html#muc-show-join-leave-status) - New config option [singleton](https://conversejs.org/docs/html/configuration.html#singleton). By setting this option to `false` and `view_mode` to `'embedded'`, it's now possible to - "embed" the full app and not just a single chat. To embed just a single chat, - it's now necessary to explicitly set `singleton` to `true`. + "embed" the full app and not just a single chat. To embed just a single chat, it's now + necessary to explicitly set `singleton` to `true`. - New event: `chatBoxBlurred`. - New event: [chatBoxBlurred](https://conversejs.org/docs/html/api/-_converse.html#event:chatBoxBlurred) - New event: [chatReconnected](https://conversejs.org/docs/html/api/-_converse.html#event:chatReconnected) -- Properly handle message correction being received before the corrected message - #1296: `embedded` view mode shows `chatbox-navback` arrow in header - #1465: When highlighting a roster contact, they're incorrectly shown as online - #1532: Converse reloads on enter pressed in the filter box diff --git a/src/headless/converse-core.js b/src/headless/converse-core.js index c35bdbf17..e27c598f1 100644 --- a/src/headless/converse-core.js +++ b/src/headless/converse-core.js @@ -1446,10 +1446,23 @@ _converse.api = { * (see [_converse.api.listen](http://localhost:8000/docs/html/api/-_converse.api.listen.html)). * * @method _converse.api.trigger + * @param {string} name - The event name + * @param {...any} [argument] - Argument to be passed to the event handler + * @param {object} [options] + * @param {boolean} [options.synchronous] - Whether the event is synchronous or not. + * When a synchronous event is fired, Converse will wait for all + * promises returned by the event's handlers to finish before continuing. */ - 'trigger' (name) { + async trigger (name) { /* Event emitter and promise resolver */ - _converse.trigger.apply(_converse, arguments); + const args = Array.from(arguments); + const options = args.pop(); + if (options.synchronous) { + const events = _converse._events[name] || []; + await Promise.all(events.map(e => e.callback.call(e.ctx, args))); + } else { + _converse.trigger.apply(_converse, arguments); + } const promise = _converse.promises[name]; if (!_.isUndefined(promise)) { promise.resolve();