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 @@
- Avoid `eval` (via `_.template` from lodash).
- 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
## 3.3.3 (2018-02-14)
......
......@@ -546,25 +546,17 @@
return;
}
_converse.bookmarks = new _converse.Bookmarks();
_converse.bookmarks.fetchBookmarks().then(() => {
_converse.bookmarksview = new _converse.BookmarksView(
{'model': _converse.bookmarks}
);
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR))
.then(() => {
_converse.emit('bookmarksInitialized');
});
}).catch((e) => {
_converse.log(e, Strophe.LogLevel.ERROR);
_converse.emit('bookmarksInitialized');
_converse.bookmarksview = new _converse.BookmarksView({'model': _converse.bookmarks});
_converse.bookmarks.fetchBookmarks()
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
.then(() => _converse.emit('bookmarksInitialized'));
});
};
}
Promise.all([
_converse.api.waitUntil('chatBoxesFetched'),
_converse.api.waitUntil('roomsPanelRendered')
]).then(initBookmarks)
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
u.onMultipleEvents([
{'object': _converse, 'event': 'chatBoxesFetched'},
{'object': _converse, 'event': 'roomsPanelRendered'}
], initBookmarks);
_converse.on('connected', () => {
// Add a handler for bookmarks pushed from other connected clients
......
......@@ -237,18 +237,18 @@
);
};
Promise.all([
_converse.api.waitUntil('chatBoxesFetched'),
_converse.api.waitUntil('roomsPanelRendered')
]).then(() => {
if (_converse.allow_bookmarks) {
_converse.api.waitUntil('bookmarksInitialized').then(
initRoomsListView
);
} else {
initRoomsListView();
}
});
if (_converse.allow_bookmarks) {
u.onMultipleEvents([
{'object': _converse, 'event': 'chatBoxesFetched'},
{'object': _converse, 'event': 'roomsPanelRendered'},
{'object': _converse, 'event': 'bookmarksInitialized'}
], initRoomsListView);
} else {
u.onMultipleEvents([
{'object': _converse, 'event': 'chatBoxesFetched'},
{'object': _converse, 'event': 'roomsPanelRendered'}
], initRoomsListView);
}
_converse.api.listen.on('reconnected', initRoomsListView);
}
......
......@@ -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) {
if (u.isPersistableModel(model)) {
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