Skip to content

Commit

Permalink
Ensure the unmount callback is regularlly called whenever a page loos…
Browse files Browse the repository at this point in the history
…es focus
  • Loading branch information
mercihabam authored and josaphatim committed Oct 16, 2024
1 parent 8f73017 commit 1b61f46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
30 changes: 23 additions & 7 deletions modules/core/navigation/navigation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const unMountSubscribers = [];
const unMountSubscribers = {};

let previousLocationSearch = window.location.search;

function trackLocationSearchChanges() {
previousLocationSearch = window.location.search;
}

window.addEventListener('popstate', function(event) {
if (event.state) {
Expand All @@ -7,14 +13,23 @@ window.addEventListener('popstate', function(event) {
const unMountCallback = renderPage(window.location.href);

if (unMountCallback) {
unMountSubscribers.push({ url: window.location.search, callback: unMountCallback });
unMountSubscribers[window.location.search] = unMountCallback;
}


Hm_Folders.hl_selected_menu();

unMountSubscribers[previousLocationSearch]?.();

trackLocationSearchChanges();
});

window.addEventListener('load', function() {
renderPage(window.location.href);
const unMountCallback = renderPage(window.location.href);
history.replaceState({ main: $('main').prop('outerHTML') }, "");

if (unMountCallback) {
unMountSubscribers[window.location.search] = unMountCallback;
}
});


Expand Down Expand Up @@ -42,18 +57,19 @@ async function navigate(url) {
$('main').replaceWith(main);

window.location.next = url;
const previousUrl = window.location.search;

const unMountCallback = renderPage(url);

history.pushState({ main }, "", url);

if (unMountCallback) {
unMountSubscribers.push({ url, callback: unMountCallback });
unMountSubscribers[url] = unMountCallback;
}
Hm_Folders.hl_selected_menu();

unMountSubscribers.find(subscriber => subscriber.url === previousUrl)?.callback();
unMountSubscribers[previousLocationSearch]?.();

trackLocationSearchChanges();
} catch (error) {
throw error;
} finally {
Expand Down
13 changes: 7 additions & 6 deletions modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ var remove_from_cached_imap_pages = function(msg_cache_key) {
async function select_imap_folder(path, reload, processInTheBackground = false) {
const messages = new Hm_MessagesStore(path, Hm_Utils.get_url_page_number());
return await messages.load(reload, processInTheBackground).then(() => {
display_imap_mailbox(messages.rows, messages.links);
display_imap_mailbox(messages.rows, messages.links, path);
});
};

Expand Down Expand Up @@ -487,8 +487,8 @@ $('#imap_filter_form').on('submit', async function(event) {
}
});

var display_imap_mailbox = function(rows, links) {
const detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap');
var display_imap_mailbox = function(rows, links, path = getListPathParam()) {
const detail = Hm_Utils.parse_folder_path(path, 'imap');
const serverIds = [];
if (detail) {
serverIds.push(detail.server_id);
Expand All @@ -504,18 +504,19 @@ var display_imap_mailbox = function(rows, links) {
const messages = Object.values(rows);
messages.forEach(function(item) {
const tr = $(item['0']);
const path = item['1'];
const uid = tr.data('uid');

if (Hm_Utils.get_from_local_storage(getMessageStorageKey(uid))) {
return;
}
preFetchMessageContent(false, uid);
preFetchMessageContent(false, uid, path);
});
}
};

function preFetchMessageContent(msgPart, uid) {
const detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap');
function preFetchMessageContent(msgPart, uid, path) {
const detail = Hm_Utils.parse_folder_path(path, 'imap');
Hm_Ajax.request([
{'name': 'hm_ajax_hook', 'value': 'ajax_imap_message_content'},
{'name': 'imap_msg_uid', 'value': uid},
Expand Down

0 comments on commit 1b61f46

Please sign in to comment.