Browse Source

Allow events to be synchronous

When a synchronous event is fired, Converse will wait for all
promises returned by the event's handlers to finish before continuing.
JC Brand 6 years ago
parent
commit
0688907e0d
2 changed files with 21 additions and 5 deletions
  1. 6 3
      CHANGES.md
  2. 15 2
      src/headless/converse-core.js

+ 6 - 3
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

+ 15 - 2
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();