Commit 68b839d0 authored by JC Brand's avatar JC Brand

Add `onMultipleEvents` utility method.

And use it instead of listening to multiple Promises.

Promises get resolved once, which means using them doesn't work for
cases where the user logs out, and then in again (because the handlers
don't get called upon 2nd login).
parent a7fa06c1
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
- Avoid `eval` (via `_.template` from lodash). - Avoid `eval` (via `_.template` from lodash).
- Bugfix. Avatars weren't being shown. - Bugfix. Avatars weren't being shown.
- Bugfix. Bookmarks list and open rooms list weren't recreated after logging in for a 2nd time (without reloading the browser).
- Add LibreJS support - Add LibreJS support
## 3.3.3 (2018-02-14) ## 3.3.3 (2018-02-14)
......
...@@ -546,25 +546,17 @@ ...@@ -546,25 +546,17 @@
return; return;
} }
_converse.bookmarks = new _converse.Bookmarks(); _converse.bookmarks = new _converse.Bookmarks();
_converse.bookmarks.fetchBookmarks().then(() => { _converse.bookmarksview = new _converse.BookmarksView({'model': _converse.bookmarks});
_converse.bookmarksview = new _converse.BookmarksView( _converse.bookmarks.fetchBookmarks()
{'model': _converse.bookmarks} .catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
); .then(() => _converse.emit('bookmarksInitialized'));
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR))
.then(() => {
_converse.emit('bookmarksInitialized');
});
}).catch((e) => {
_converse.log(e, Strophe.LogLevel.ERROR);
_converse.emit('bookmarksInitialized');
}); });
}; }
Promise.all([ u.onMultipleEvents([
_converse.api.waitUntil('chatBoxesFetched'), {'object': _converse, 'event': 'chatBoxesFetched'},
_converse.api.waitUntil('roomsPanelRendered') {'object': _converse, 'event': 'roomsPanelRendered'}
]).then(initBookmarks) ], initBookmarks);
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.on('connected', () => { _converse.on('connected', () => {
// Add a handler for bookmarks pushed from other connected clients // Add a handler for bookmarks pushed from other connected clients
......
...@@ -237,18 +237,18 @@ ...@@ -237,18 +237,18 @@
); );
}; };
Promise.all([
_converse.api.waitUntil('chatBoxesFetched'),
_converse.api.waitUntil('roomsPanelRendered')
]).then(() => {
if (_converse.allow_bookmarks) { if (_converse.allow_bookmarks) {
_converse.api.waitUntil('bookmarksInitialized').then( u.onMultipleEvents([
initRoomsListView {'object': _converse, 'event': 'chatBoxesFetched'},
); {'object': _converse, 'event': 'roomsPanelRendered'},
{'object': _converse, 'event': 'bookmarksInitialized'}
], initRoomsListView);
} else { } else {
initRoomsListView(); u.onMultipleEvents([
{'object': _converse, 'event': 'chatBoxesFetched'},
{'object': _converse, 'event': 'roomsPanelRendered'}
], initRoomsListView);
} }
});
_converse.api.listen.on('reconnected', initRoomsListView); _converse.api.listen.on('reconnected', initRoomsListView);
} }
......
...@@ -654,6 +654,28 @@ ...@@ -654,6 +654,28 @@
}); });
}; };
u.onMultipleEvents = function (events=[], callback) {
/* Call the callback once all the events have been triggered
*
* Parameters:
* (Array) events: An array of objects, with keys `object` and
* `event`, representing the event name and the object it's
* triggered upon.
* (Function) callback: The function to call once all events have
* been triggered.
*/
let triggered = [];
function handler (result) {
triggered.push(result)
if (events.length === triggered.length) {
callback(triggered);
triggered = [];
}
}
_.each(events, (map) => map.object.on(map.event, handler));
};
u.safeSave = function (model, attributes) { u.safeSave = function (model, attributes) {
if (u.isPersistableModel(model)) { if (u.isPersistableModel(model)) {
model.save(attributes); model.save(attributes);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment