Commit da319604 authored by JC Brand's avatar JC Brand

Fixes #1735: Chatboxes stay empty after logging out and in again

The problem was that the `emojisInitialized` promise was re-created upon
logout and then no longer resolved upon subsequent login.

The `_converse.api.promises.add` method now takes another parameter
`replace` which determine whether it should be replaced upon logout or
not.

The `emojisInitialized` promise will now not get replaced.
parent 8b6c902c
...@@ -327,11 +327,19 @@ const PROMISES = [ ...@@ -327,11 +327,19 @@ const PROMISES = [
'statusInitialized' 'statusInitialized'
]; ];
function addPromise (promise) {
/* Private function, used to add a new promise to the ones already function replacePromise (name) {
* available via the `waitUntil` api method. const existing_promise = _converse.promises[name];
*/ if (!existing_promise) {
_converse.promises[promise] = u.getResolveablePromise(); throw new Error(`Tried to replace non-existing promise: ${name}`);
}
if (existing_promise.replace) {
const promise = u.getResolveablePromise();
promise.replace = existing_promise ? existing_promise.replace : replace;
_converse.promises[name] = promise;
} else {
log.debug(`Not replacing promise "${name}"`);
}
} }
_converse.isTestEnv = function () { _converse.isTestEnv = function () {
...@@ -999,7 +1007,7 @@ _converse.initialize = async function (settings, callback) { ...@@ -999,7 +1007,7 @@ _converse.initialize = async function (settings, callback) {
cleanup(); cleanup();
settings = settings !== undefined ? settings : {}; settings = settings !== undefined ? settings : {};
PROMISES.forEach(addPromise); PROMISES.forEach(name => _converse.api.promises.add(name));
if ('onpagehide' in window) { if ('onpagehide' in window) {
// Pagehide gets thrown in more cases than unload. Specifically it // Pagehide gets thrown in more cases than unload. Specifically it
...@@ -1417,7 +1425,7 @@ _converse.api = { ...@@ -1417,7 +1425,7 @@ _converse.api = {
const promise = u.getResolveablePromise(); const promise = u.getResolveablePromise();
const complete = () => { const complete = () => {
// Recreate all the promises // Recreate all the promises
Object.keys(_converse.promises).forEach(addPromise); Object.keys(_converse.promises).forEach(replacePromise);
/** /**
* Triggered once the user has logged out. * Triggered once the user has logged out.
* @event _converse#logout * @event _converse#logout
...@@ -1549,11 +1557,16 @@ _converse.api = { ...@@ -1549,11 +1557,16 @@ _converse.api = {
* *
* @method _converse.api.promises.add * @method _converse.api.promises.add
* @param {string|array} [name|names] The name or an array of names for the promise(s) to be added * @param {string|array} [name|names] The name or an array of names for the promise(s) to be added
* @param {boolean} [replace=true] Whether this promise should be replaced with a new one when the user logs out.
* @example _converse.api.promises.add('foo-completed'); * @example _converse.api.promises.add('foo-completed');
*/ */
add (promises) { add (promises, replace=true) {
promises = Array.isArray(promises) ? promises : [promises]; promises = Array.isArray(promises) ? promises : [promises];
promises.forEach(addPromise); promises.forEach(name => {
const promise = u.getResolveablePromise();
promise.replace = replace;
_converse.promises[name] = promise;
});
} }
}, },
......
...@@ -202,7 +202,7 @@ converse.plugins.add('converse-emoji', { ...@@ -202,7 +202,7 @@ converse.plugins.add('converse-emoji', {
} }
}); });
_converse.api.promises.add(['emojisInitialized']); _converse.api.promises.add('emojisInitialized', false);
twemoji.default.base = _converse.emoji_image_path; twemoji.default.base = _converse.emoji_image_path;
......
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